blob: bb4c0000cd03b05d64dad64832fc3ebb5c62b2c1 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Janos Follath23bdca02016-10-07 14:47:14 +010053#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020055#endif
56
Hanno Becker2a43f6f2018-08-10 11:12:52 +010057static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Becker12555c62018-08-16 12:47:53 +010058static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010059
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010060/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010062{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020064 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010066#else
67 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010068#endif
69 return( 0 );
70}
71
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020072/*
73 * Start a timer.
74 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020078 if( ssl->f_set_timer == NULL )
79 return;
80
81 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
82 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083}
84
85/*
86 * Return -1 is timer is expired, 0 if it isn't.
87 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020090 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020091 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020092
93 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020094 {
95 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020096 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020097 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098
99 return( 0 );
100}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100102static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
103 mbedtls_ssl_transform *transform );
104static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
105 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100106
107#define SSL_DONT_FORCE_FLUSH 0
108#define SSL_FORCE_FLUSH 1
109
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100111
112static uint16_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
113{
114 uint16_t mtu = ssl->conf->mtu;
115
116 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
117 return( (int) mtu );
118
119 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
120}
121
Hanno Becker67bc7c32018-08-06 11:33:50 +0100122static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
123{
124 size_t const bytes_written = ssl->out_left;
125 uint16_t const mtu = ssl_get_maximum_datagram_size( ssl );
126
127 /* Double-check that the write-index hasn't gone
128 * past what we can transmit in a single datagram. */
129 if( bytes_written > (size_t) mtu )
130 {
131 /* Should never happen... */
132 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
133 }
134
135 return( (int) ( mtu - bytes_written ) );
136}
137
138static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
139{
140 int ret;
141 size_t remaining, expansion;
142 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
143
144#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
145 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
146
147 if( max_len > mfl )
148 max_len = mfl;
149#endif
150
151 ret = ssl_get_remaining_space_in_datagram( ssl );
152 if( ret < 0 )
153 return( ret );
154 remaining = (size_t) ret;
155
156 ret = mbedtls_ssl_get_record_expansion( ssl );
157 if( ret < 0 )
158 return( ret );
159 expansion = (size_t) ret;
160
161 if( remaining <= expansion )
162 return( 0 );
163
164 remaining -= expansion;
165 if( remaining >= max_len )
166 remaining = max_len;
167
168 return( (int) remaining );
169}
170
Hanno Becker0271f962018-08-16 13:23:47 +0100171static void ssl_buffering_free( mbedtls_ssl_context *ssl );
172
Hanno Beckere605b192018-08-21 15:59:07 +0100173static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
174 uint8_t slot );
175
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200176/*
177 * Double the retransmit timeout value, within the allowed range,
178 * returning -1 if the maximum value has already been reached.
179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200181{
182 uint32_t new_timeout;
183
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200184 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200185 return( -1 );
186
187 new_timeout = 2 * ssl->handshake->retransmit_timeout;
188
189 /* Avoid arithmetic overflow and range overflow */
190 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200191 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200192 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200193 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200194 }
195
196 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200198 ssl->handshake->retransmit_timeout ) );
199
200 return( 0 );
201}
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200204{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200205 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200207 ssl->handshake->retransmit_timeout ) );
208}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200212/*
213 * Convert max_fragment_length codes to length.
214 * RFC 6066 says:
215 * enum{
216 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
217 * } MaxFragmentLength;
218 * and we add 0 -> extension unused
219 */
Angus Grattond8213d02016-05-25 20:56:48 +1000220static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200221{
Angus Grattond8213d02016-05-25 20:56:48 +1000222 switch( mfl )
223 {
224 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
225 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
226 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
227 return 512;
228 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
229 return 1024;
230 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
231 return 2048;
232 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
233 return 4096;
234 default:
235 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
236 }
237}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200239
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200240#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_ssl_session_free( dst );
244 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200247 if( src->peer_cert != NULL )
248 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200249 int ret;
250
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200251 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200252 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200253 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200258 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261 dst->peer_cert = NULL;
262 return( ret );
263 }
264 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200266
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200267#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200268 if( src->ticket != NULL )
269 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200270 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200271 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200272 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200273
274 memcpy( dst->ticket, src->ticket, src->ticket_len );
275 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200276#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200277
278 return( 0 );
279}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200280#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
283int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200284 const unsigned char *key_enc, const unsigned char *key_dec,
285 size_t keylen,
286 const unsigned char *iv_enc, const unsigned char *iv_dec,
287 size_t ivlen,
288 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200289 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
291int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
292int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
293int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
294int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
295#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000296
Paul Bakker5121ce52009-01-03 21:22:43 +0000297/*
298 * Key material generation
299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200301static int ssl3_prf( const unsigned char *secret, size_t slen,
302 const char *label,
303 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000304 unsigned char *dstbuf, size_t dlen )
305{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100306 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000307 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200308 mbedtls_md5_context md5;
309 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000310 unsigned char padding[16];
311 unsigned char sha1sum[20];
312 ((void)label);
313
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200314 mbedtls_md5_init( &md5 );
315 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200316
Paul Bakker5f70b252012-09-13 14:23:06 +0000317 /*
318 * SSLv3:
319 * block =
320 * MD5( secret + SHA1( 'A' + secret + random ) ) +
321 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
322 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
323 * ...
324 */
325 for( i = 0; i < dlen / 16; i++ )
326 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200327 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000328
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100329 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100330 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100331 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100332 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100333 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100334 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100335 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100336 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100337 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100338 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000339
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100340 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100341 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100342 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100343 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100344 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100345 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100346 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100347 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000348 }
349
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100350exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200351 mbedtls_md5_free( &md5 );
352 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000353
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500354 mbedtls_platform_zeroize( padding, sizeof( padding ) );
355 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000356
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100357 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000358}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200362static int tls1_prf( const unsigned char *secret, size_t slen,
363 const char *label,
364 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000365 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000366{
Paul Bakker23986e52011-04-24 08:57:21 +0000367 size_t nb, hs;
368 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200369 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 unsigned char tmp[128];
371 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 const mbedtls_md_info_t *md_info;
373 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100374 int ret;
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
378 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000380
381 hs = ( slen + 1 ) / 2;
382 S1 = secret;
383 S2 = secret + slen - hs;
384
385 nb = strlen( label );
386 memcpy( tmp + 20, label, nb );
387 memcpy( tmp + 20 + nb, random, rlen );
388 nb += rlen;
389
390 /*
391 * First compute P_md5(secret,label+random)[0..dlen]
392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
394 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100397 return( ret );
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
400 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
401 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
403 for( i = 0; i < dlen; i += 16 )
404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_md_hmac_reset ( &md_ctx );
406 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
407 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_md_hmac_reset ( &md_ctx );
410 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
411 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
413 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
414
415 for( j = 0; j < k; j++ )
416 dstbuf[i + j] = h_i[j];
417 }
418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100420
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 /*
422 * XOR out with P_sha1(secret,label+random)[0..dlen]
423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100428 return( ret );
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
431 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
432 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
434 for( i = 0; i < dlen; i += 20 )
435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_md_hmac_reset ( &md_ctx );
437 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
438 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_md_hmac_reset ( &md_ctx );
441 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
442 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443
444 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
445
446 for( j = 0; j < k; j++ )
447 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
448 }
449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100451
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500452 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
453 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000454
455 return( 0 );
456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
460static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100461 const unsigned char *secret, size_t slen,
462 const char *label,
463 const unsigned char *random, size_t rlen,
464 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000465{
466 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100467 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000468 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
470 const mbedtls_md_info_t *md_info;
471 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100472 int ret;
473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
477 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100480
481 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000483
484 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100485 memcpy( tmp + md_len, label, nb );
486 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000487 nb += rlen;
488
489 /*
490 * Compute P_<hash>(secret, label + random)[0..dlen]
491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100493 return( ret );
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
496 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
497 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100498
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100499 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_md_hmac_reset ( &md_ctx );
502 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
503 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_md_hmac_reset ( &md_ctx );
506 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
507 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000508
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100509 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000510
511 for( j = 0; j < k; j++ )
512 dstbuf[i + j] = h_i[j];
513 }
514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100516
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500517 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
518 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000519
520 return( 0 );
521}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100524static int tls_prf_sha256( const unsigned char *secret, size_t slen,
525 const char *label,
526 const unsigned char *random, size_t rlen,
527 unsigned char *dstbuf, size_t dlen )
528{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100530 label, random, rlen, dstbuf, dlen ) );
531}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200535static int tls_prf_sha384( const unsigned char *secret, size_t slen,
536 const char *label,
537 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000538 unsigned char *dstbuf, size_t dlen )
539{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100541 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000542}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543#endif /* MBEDTLS_SHA512_C */
544#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
549 defined(MBEDTLS_SSL_PROTO_TLS1_1)
550static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200551#endif
Paul Bakker380da532012-04-18 16:10:25 +0000552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#if defined(MBEDTLS_SSL_PROTO_SSL3)
554static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
555static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200556#endif
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
559static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
560static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200561#endif
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
564#if defined(MBEDTLS_SHA256_C)
565static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
566static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
567static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200568#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#if defined(MBEDTLS_SHA512_C)
571static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
572static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
573static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100574#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200579 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 unsigned char keyblk[256];
582 unsigned char *key1;
583 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100584 unsigned char *mac_enc;
585 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000586 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200587 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 const mbedtls_cipher_info_t *cipher_info;
589 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_ssl_session *session = ssl->session_negotiate;
592 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
593 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100598 if( cipher_info == NULL )
599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100601 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100603 }
604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100606 if( md_info == NULL )
607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100609 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100611 }
612
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000614 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#if defined(MBEDTLS_SSL_PROTO_SSL3)
617 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000618 {
Paul Bakker48916f92012-09-16 19:57:18 +0000619 handshake->tls_prf = ssl3_prf;
620 handshake->calc_verify = ssl_calc_verify_ssl;
621 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000622 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200623 else
624#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
626 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000627 {
Paul Bakker48916f92012-09-16 19:57:18 +0000628 handshake->tls_prf = tls1_prf;
629 handshake->calc_verify = ssl_calc_verify_tls;
630 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000631 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200632 else
633#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
635#if defined(MBEDTLS_SHA512_C)
636 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
637 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000638 {
Paul Bakker48916f92012-09-16 19:57:18 +0000639 handshake->tls_prf = tls_prf_sha384;
640 handshake->calc_verify = ssl_calc_verify_tls_sha384;
641 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000642 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000643 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200644#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645#if defined(MBEDTLS_SHA256_C)
646 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000647 {
Paul Bakker48916f92012-09-16 19:57:18 +0000648 handshake->tls_prf = tls_prf_sha256;
649 handshake->calc_verify = ssl_calc_verify_tls_sha256;
650 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000651 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200652 else
653#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
657 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200658 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000659
660 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 * SSLv3:
662 * master =
663 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
664 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
665 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200666 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200667 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 * master = PRF( premaster, "master secret", randbytes )[0..47]
669 */
Paul Bakker0a597072012-09-25 21:55:46 +0000670 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000673 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
676 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200677 {
678 unsigned char session_hash[48];
679 size_t hash_len;
680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200682
683 ssl->handshake->calc_verify( ssl, session_hash );
684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
686 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200689 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200691 {
692 hash_len = 48;
693 }
694 else
695#endif
696 hash_len = 32;
697 }
698 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200700 hash_len = 36;
701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200703
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100704 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
705 "extended master secret",
706 session_hash, hash_len,
707 session->master, 48 );
708 if( ret != 0 )
709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100711 return( ret );
712 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200713
714 }
715 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200716#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100717 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
718 "master secret",
719 handshake->randbytes, 64,
720 session->master, 48 );
721 if( ret != 0 )
722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100724 return( ret );
725 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200726
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500727 mbedtls_platform_zeroize( handshake->premaster,
728 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 }
730 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
733 /*
734 * Swap the client and server random values.
735 */
Paul Bakker48916f92012-09-16 19:57:18 +0000736 memcpy( tmp, handshake->randbytes, 64 );
737 memcpy( handshake->randbytes, tmp + 32, 32 );
738 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500739 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
741 /*
742 * SSLv3:
743 * key block =
744 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
745 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
746 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
747 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
748 * ...
749 *
750 * TLSv1:
751 * key block = PRF( master, "key expansion", randbytes )
752 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100753 ret = handshake->tls_prf( session->master, 48, "key expansion",
754 handshake->randbytes, 64, keyblk, 256 );
755 if( ret != 0 )
756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100758 return( ret );
759 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
762 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
763 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
764 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
765 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000766
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500767 mbedtls_platform_zeroize( handshake->randbytes,
768 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
770 /*
771 * Determine the appropriate key, IV and MAC length.
772 */
Paul Bakker68884e32013-01-07 18:20:04 +0100773
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200774 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200777 cipher_info->mode == MBEDTLS_MODE_CCM ||
778 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000779 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200780 size_t taglen, explicit_ivlen;
781
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200782 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000783 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200784
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200785 /* All modes haves 96-bit IVs;
786 * GCM and CCM has 4 implicit and 8 explicit bytes
787 * ChachaPoly has all 12 bytes implicit
788 */
Paul Bakker68884e32013-01-07 18:20:04 +0100789 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200790 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
791 transform->fixed_ivlen = 12;
792 else
793 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200794
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200795 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
796 taglen = transform->ciphersuite_info->flags &
797 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
798
799
800 /* Minimum length of encrypted record */
801 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
802 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100803 }
804 else
805 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200806 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
808 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200811 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100812 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000813
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200814 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000815 mac_key_len = mbedtls_md_get_size( md_info );
816 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200819 /*
820 * If HMAC is to be truncated, we shall keep the leftmost bytes,
821 * (rfc 6066 page 13 or rfc 2104 section 4),
822 * so we only need to adjust the length here.
823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000827
828#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
829 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000830 * HMAC implementation which also truncates the key
831 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000832 mac_key_len = transform->maclen;
833#endif
834 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200836
837 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100838 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000839
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200840 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200842 transform->minlen = transform->maclen;
843 else
Paul Bakker68884e32013-01-07 18:20:04 +0100844 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200845 /*
846 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100847 * 1. if EtM is in use: one block plus MAC
848 * otherwise: * first multiple of blocklen greater than maclen
849 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
852 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100853 {
854 transform->minlen = transform->maclen
855 + cipher_info->block_size;
856 }
857 else
858#endif
859 {
860 transform->minlen = transform->maclen
861 + cipher_info->block_size
862 - transform->maclen % cipher_info->block_size;
863 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
866 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
867 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200868 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100869 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200870#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
872 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
873 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200874 {
875 transform->minlen += transform->ivlen;
876 }
877 else
878#endif
879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
881 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200882 }
Paul Bakker68884e32013-01-07 18:20:04 +0100883 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000884 }
885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000887 transform->keylen, transform->minlen, transform->ivlen,
888 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000889
890 /*
891 * Finally setup the cipher contexts, IVs and MAC secrets.
892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200894 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000895 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000896 key1 = keyblk + mac_key_len * 2;
897 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000898
Paul Bakker68884e32013-01-07 18:20:04 +0100899 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000900 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000901
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000902 /*
903 * This is not used in TLS v1.1.
904 */
Paul Bakker48916f92012-09-16 19:57:18 +0000905 iv_copy_len = ( transform->fixed_ivlen ) ?
906 transform->fixed_ivlen : transform->ivlen;
907 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
908 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000909 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 }
911 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912#endif /* MBEDTLS_SSL_CLI_C */
913#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200914 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000915 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000916 key1 = keyblk + mac_key_len * 2 + transform->keylen;
917 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000918
Hanno Becker81c7b182017-11-09 18:39:33 +0000919 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100920 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000921
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000922 /*
923 * This is not used in TLS v1.1.
924 */
Paul Bakker48916f92012-09-16 19:57:18 +0000925 iv_copy_len = ( transform->fixed_ivlen ) ?
926 transform->fixed_ivlen : transform->ivlen;
927 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
928 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000929 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100931 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
935 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100936 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938#if defined(MBEDTLS_SSL_PROTO_SSL3)
939 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100940 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000941 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
944 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100945 }
946
Hanno Becker81c7b182017-11-09 18:39:33 +0000947 memcpy( transform->mac_enc, mac_enc, mac_key_len );
948 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100949 }
950 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951#endif /* MBEDTLS_SSL_PROTO_SSL3 */
952#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
953 defined(MBEDTLS_SSL_PROTO_TLS1_2)
954 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100955 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100956 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
957 For AEAD-based ciphersuites, there is nothing to do here. */
958 if( mac_key_len != 0 )
959 {
960 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
961 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
962 }
Paul Bakker68884e32013-01-07 18:20:04 +0100963 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200964 else
965#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
968 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200969 }
Paul Bakker68884e32013-01-07 18:20:04 +0100970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
972 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000973 {
974 int ret = 0;
975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100979 transform->iv_enc, transform->iv_dec,
980 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100981 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +0000982 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
985 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000986 }
987 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000989
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200990#if defined(MBEDTLS_SSL_EXPORT_KEYS)
991 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100992 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200993 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
994 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +0000995 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100996 iv_copy_len );
997 }
998#endif
999
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001000 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001001 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001002 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001004 return( ret );
1005 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001006
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001007 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001008 cipher_info ) ) != 0 )
1009 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001011 return( ret );
1012 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001015 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001019 return( ret );
1020 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001023 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001027 return( ret );
1028 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_CIPHER_MODE_CBC)
1031 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1034 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001037 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001038 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1041 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001044 return( ret );
1045 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001048
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001049 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001052 // Initialize compression
1053 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001055 {
Paul Bakker16770332013-10-11 09:59:44 +02001056 if( ssl->compress_buf == NULL )
1057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001059 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001060 if( ssl->compress_buf == NULL )
1061 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001063 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001064 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001065 }
1066 }
1067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001069
Paul Bakker48916f92012-09-16 19:57:18 +00001070 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1071 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001072
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001073 if( deflateInit( &transform->ctx_deflate,
1074 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001075 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1078 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001079 }
1080 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
1085 return( 0 );
1086}
1087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_SSL_PROTO_SSL3)
1089void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001090{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001091 mbedtls_md5_context md5;
1092 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001093 unsigned char pad_1[48];
1094 unsigned char pad_2[48];
1095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001098 mbedtls_md5_init( &md5 );
1099 mbedtls_sha1_init( &sha1 );
1100
1101 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1102 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001103
Paul Bakker380da532012-04-18 16:10:25 +00001104 memset( pad_1, 0x36, 48 );
1105 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001106
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001107 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1108 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1109 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001110
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001111 mbedtls_md5_starts_ret( &md5 );
1112 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1113 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1114 mbedtls_md5_update_ret( &md5, hash, 16 );
1115 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001116
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001117 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1118 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1119 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001120
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001121 mbedtls_sha1_starts_ret( &sha1 );
1122 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1123 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1124 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1125 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001129
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001130 mbedtls_md5_free( &md5 );
1131 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001132
Paul Bakker380da532012-04-18 16:10:25 +00001133 return;
1134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1138void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001139{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001140 mbedtls_md5_context md5;
1141 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001144
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001145 mbedtls_md5_init( &md5 );
1146 mbedtls_sha1_init( &sha1 );
1147
1148 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1149 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001150
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001151 mbedtls_md5_finish_ret( &md5, hash );
1152 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001156
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001157 mbedtls_md5_free( &md5 );
1158 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001159
Paul Bakker380da532012-04-18 16:10:25 +00001160 return;
1161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1165#if defined(MBEDTLS_SHA256_C)
1166void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001167{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001168 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001169
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001170 mbedtls_sha256_init( &sha256 );
1171
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001173
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001174 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001175 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001179
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001180 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001181
Paul Bakker380da532012-04-18 16:10:25 +00001182 return;
1183}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186#if defined(MBEDTLS_SHA512_C)
1187void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001188{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001189 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001190
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001191 mbedtls_sha512_init( &sha512 );
1192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001194
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001195 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001196 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001200
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001201 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001202
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 return;
1204}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205#endif /* MBEDTLS_SHA512_C */
1206#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1209int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001210{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001211 unsigned char *p = ssl->handshake->premaster;
1212 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001213 const unsigned char *psk = ssl->conf->psk;
1214 size_t psk_len = ssl->conf->psk_len;
1215
1216 /* If the psk callback was called, use its result */
1217 if( ssl->handshake->psk != NULL )
1218 {
1219 psk = ssl->handshake->psk;
1220 psk_len = ssl->handshake->psk_len;
1221 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001222
1223 /*
1224 * PMS = struct {
1225 * opaque other_secret<0..2^16-1>;
1226 * opaque psk<0..2^16-1>;
1227 * };
1228 * with "other_secret" depending on the particular key exchange
1229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1231 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001232 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001233 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001235
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001236 *(p++) = (unsigned char)( psk_len >> 8 );
1237 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001238
1239 if( end < p || (size_t)( end - p ) < psk_len )
1240 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1241
1242 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001243 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001244 }
1245 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1247#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1248 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001249 {
1250 /*
1251 * other_secret already set by the ClientKeyExchange message,
1252 * and is 48 bytes long
1253 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001254 if( end - p < 2 )
1255 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1256
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001257 *p++ = 0;
1258 *p++ = 48;
1259 p += 48;
1260 }
1261 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1263#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1264 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001265 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001266 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001267 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001268
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001269 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001271 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001272 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001275 return( ret );
1276 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001277 *(p++) = (unsigned char)( len >> 8 );
1278 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001279 p += len;
1280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001282 }
1283 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1285#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1286 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001287 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001288 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001289 size_t zlen;
1290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001292 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001293 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001296 return( ret );
1297 }
1298
1299 *(p++) = (unsigned char)( zlen >> 8 );
1300 *(p++) = (unsigned char)( zlen );
1301 p += zlen;
1302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001304 }
1305 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1309 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001310 }
1311
1312 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001313 if( end - p < 2 )
1314 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001315
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001316 *(p++) = (unsigned char)( psk_len >> 8 );
1317 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001318
1319 if( end < p || (size_t)( end - p ) < psk_len )
1320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1321
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001322 memcpy( p, psk, psk_len );
1323 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001324
1325 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1326
1327 return( 0 );
1328}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001332/*
1333 * SSLv3.0 MAC functions
1334 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001335#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001336static void ssl_mac( mbedtls_md_context_t *md_ctx,
1337 const unsigned char *secret,
1338 const unsigned char *buf, size_t len,
1339 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001340 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001341{
1342 unsigned char header[11];
1343 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001344 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1346 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001347
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001348 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001350 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001351 else
Paul Bakker68884e32013-01-07 18:20:04 +01001352 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
1354 memcpy( header, ctr, 8 );
1355 header[ 8] = (unsigned char) type;
1356 header[ 9] = (unsigned char)( len >> 8 );
1357 header[10] = (unsigned char)( len );
1358
Paul Bakker68884e32013-01-07 18:20:04 +01001359 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_md_starts( md_ctx );
1361 mbedtls_md_update( md_ctx, secret, md_size );
1362 mbedtls_md_update( md_ctx, padding, padlen );
1363 mbedtls_md_update( md_ctx, header, 11 );
1364 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001365 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001366
Paul Bakker68884e32013-01-07 18:20:04 +01001367 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 mbedtls_md_starts( md_ctx );
1369 mbedtls_md_update( md_ctx, secret, md_size );
1370 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001371 mbedtls_md_update( md_ctx, out, md_size );
1372 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001373}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1377 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001378 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001379#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001380#endif
1381
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001382/* The function below is only used in the Lucky 13 counter-measure in
1383 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1384#if defined(SSL_SOME_MODES_USE_MAC) && \
1385 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1386 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1387 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1388/* This function makes sure every byte in the memory region is accessed
1389 * (in ascending addresses order) */
1390static void ssl_read_memory( unsigned char *p, size_t len )
1391{
1392 unsigned char acc = 0;
1393 volatile unsigned char force;
1394
1395 for( ; len != 0; p++, len-- )
1396 acc ^= *p;
1397
1398 force = acc;
1399 (void) force;
1400}
1401#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1402
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001403/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001407{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001409 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001413 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1416 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001417 }
1418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001422 ssl->out_msg, ssl->out_msglen );
1423
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001425 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001427#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 if( mode == MBEDTLS_MODE_STREAM ||
1429 ( mode == MBEDTLS_MODE_CBC
1430#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1431 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001432#endif
1433 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#if defined(MBEDTLS_SSL_PROTO_SSL3)
1436 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001437 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001438 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001439
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001440 ssl_mac( &ssl->transform_out->md_ctx_enc,
1441 ssl->transform_out->mac_enc,
1442 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001443 ssl->out_ctr, ssl->out_msgtype,
1444 mac );
1445
1446 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001447 }
1448 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001449#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1451 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1452 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001453 {
Hanno Becker992b6872017-11-09 18:57:39 +00001454 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1457 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1458 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1459 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001460 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001461 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001463
1464 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001465 }
1466 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001467#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1470 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001471 }
1472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001474 ssl->out_msg + ssl->out_msglen,
1475 ssl->transform_out->maclen );
1476
1477 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001478 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001479 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001480#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001481
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001482 /*
1483 * Encrypt
1484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1486 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001487 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001488 int ret;
1489 size_t olen = 0;
1490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 "including %d bytes of padding",
1493 ssl->out_msglen, 0 ) );
1494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001496 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001497 ssl->transform_out->ivlen,
1498 ssl->out_msg, ssl->out_msglen,
1499 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001502 return( ret );
1503 }
1504
1505 if( ssl->out_msglen != olen )
1506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1508 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001509 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 }
Paul Bakker68884e32013-01-07 18:20:04 +01001511 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001513#if defined(MBEDTLS_GCM_C) || \
1514 defined(MBEDTLS_CCM_C) || \
1515 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001517 mode == MBEDTLS_MODE_CCM ||
1518 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001519 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001520 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001521 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001522 unsigned char *enc_msg;
1523 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001524 unsigned char iv[12];
1525 mbedtls_ssl_transform *transform = ssl->transform_out;
1526 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001528 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001529
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001530 /*
1531 * Prepare additional authenticated data
1532 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001533 memcpy( add_data, ssl->out_ctr, 8 );
1534 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001536 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001537 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1538 add_data[12] = ssl->out_msglen & 0xFF;
1539
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001540 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001541
Paul Bakker68884e32013-01-07 18:20:04 +01001542 /*
1543 * Generate IV
1544 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001545 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1546 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001547 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001548 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1549 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1550 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1551
1552 }
1553 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1554 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001555 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001556 unsigned char i;
1557
1558 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1559
1560 for( i = 0; i < 8; i++ )
1561 iv[i+4] ^= ssl->out_ctr[i];
1562 }
1563 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001564 {
1565 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1567 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001568 }
1569
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001570 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1571 iv, transform->ivlen );
1572 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1573 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001574
Paul Bakker68884e32013-01-07 18:20:04 +01001575 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001576 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001577 */
1578 enc_msg = ssl->out_msg;
1579 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001580 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001583 "including 0 bytes of padding",
1584 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001585
Paul Bakker68884e32013-01-07 18:20:04 +01001586 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001587 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001588 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001589 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1590 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001591 add_data, 13,
1592 enc_msg, enc_msglen,
1593 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001594 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001597 return( ret );
1598 }
1599
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001600 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001604 }
1605
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001606 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001607 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001610 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001611 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1613#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001614 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001616 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001617 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001618 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001619 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001620
Paul Bakker48916f92012-09-16 19:57:18 +00001621 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1622 ssl->transform_out->ivlen;
1623 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001624 padlen = 0;
1625
1626 for( i = 0; i <= padlen; i++ )
1627 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1628
1629 ssl->out_msglen += padlen + 1;
1630
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001631 enc_msglen = ssl->out_msglen;
1632 enc_msg = ssl->out_msg;
1633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001635 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001636 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1637 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001640 {
1641 /*
1642 * Generate IV
1643 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001644 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001645 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001646 if( ret != 0 )
1647 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001648
Paul Bakker92be97b2013-01-02 17:30:03 +01001649 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001650 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001651
1652 /*
1653 * Fix pointer positions and message length with added IV
1654 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001655 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001656 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001657 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001658 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001662 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001663 ssl->out_msglen, ssl->transform_out->ivlen,
1664 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001667 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001668 ssl->transform_out->ivlen,
1669 enc_msg, enc_msglen,
1670 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001673 return( ret );
1674 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001675
Paul Bakkercca5b812013-08-31 17:40:26 +02001676 if( enc_msglen != olen )
1677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1679 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001680 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1683 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001684 {
1685 /*
1686 * Save IV in SSL3 and TLS1
1687 */
1688 memcpy( ssl->transform_out->iv_enc,
1689 ssl->transform_out->cipher_ctx_enc.iv,
1690 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001691 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001692#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001695 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001696 {
1697 /*
1698 * MAC(MAC_write_key, seq_num +
1699 * TLSCipherText.type +
1700 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001701 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001702 * IV + // except for TLS 1.0
1703 * ENC(content + padding + padding_length));
1704 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001705 unsigned char pseudo_hdr[13];
1706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001708
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001709 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1710 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001711 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1712 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1717 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001718 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001720 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001722
1723 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001724 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001725 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001727 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001728 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001730 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1733 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001734 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001735
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001736 /* Make extra sure authentication was performed, exactly once */
1737 if( auth_done != 1 )
1738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1740 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001741 }
1742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001744
1745 return( 0 );
1746}
1747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001749{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001751 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001752#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001753 size_t padlen = 0, correct = 1;
1754#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001757
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001758 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1761 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001762 }
1763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001765
Paul Bakker48916f92012-09-16 19:57:18 +00001766 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001769 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 }
1772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1774 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001775 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001776 int ret;
1777 size_t olen = 0;
1778
Paul Bakker68884e32013-01-07 18:20:04 +01001779 padlen = 0;
1780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001782 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001783 ssl->transform_in->ivlen,
1784 ssl->in_msg, ssl->in_msglen,
1785 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001788 return( ret );
1789 }
1790
1791 if( ssl->in_msglen != olen )
1792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001795 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001796 }
Paul Bakker68884e32013-01-07 18:20:04 +01001797 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001799#if defined(MBEDTLS_GCM_C) || \
1800 defined(MBEDTLS_CCM_C) || \
1801 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001803 mode == MBEDTLS_MODE_CCM ||
1804 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001805 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001806 int ret;
1807 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001808 unsigned char *dec_msg;
1809 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001810 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001811 unsigned char iv[12];
1812 mbedtls_ssl_transform *transform = ssl->transform_in;
1813 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001815 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001816
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001817 /*
1818 * Compute and update sizes
1819 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001820 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001823 "+ taglen (%d)", ssl->in_msglen,
1824 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001826 }
1827 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1828
Paul Bakker68884e32013-01-07 18:20:04 +01001829 dec_msg = ssl->in_msg;
1830 dec_msg_result = ssl->in_msg;
1831 ssl->in_msglen = dec_msglen;
1832
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001833 /*
1834 * Prepare additional authenticated data
1835 */
Paul Bakker68884e32013-01-07 18:20:04 +01001836 memcpy( add_data, ssl->in_ctr, 8 );
1837 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001839 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001840 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1841 add_data[12] = ssl->in_msglen & 0xFF;
1842
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001843 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001844
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001845 /*
1846 * Prepare IV
1847 */
1848 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1849 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001850 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001851 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1852 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001853
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001854 }
1855 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1856 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001857 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001858 unsigned char i;
1859
1860 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1861
1862 for( i = 0; i < 8; i++ )
1863 iv[i+4] ^= ssl->in_ctr[i];
1864 }
1865 else
1866 {
1867 /* Reminder if we ever add an AEAD mode with a different size */
1868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1869 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1870 }
1871
1872 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001874
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001875 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001876 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001879 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001880 add_data, 13,
1881 dec_msg, dec_msglen,
1882 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001883 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1888 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001889
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001890 return( ret );
1891 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001892 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001893
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001894 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1897 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001898 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001899 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001900 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1902#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001903 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001905 {
Paul Bakker45829992013-01-03 14:52:21 +01001906 /*
1907 * Decrypt and check the padding
1908 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001909 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001910 unsigned char *dec_msg;
1911 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001912 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001913 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001914 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001915
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 /*
Paul Bakker45829992013-01-03 14:52:21 +01001917 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1920 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001921 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001922#endif
Paul Bakker45829992013-01-03 14:52:21 +01001923
1924 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1925 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001928 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1929 ssl->transform_in->ivlen,
1930 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001932 }
1933
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001934 dec_msglen = ssl->in_msglen;
1935 dec_msg = ssl->in_msg;
1936 dec_msg_result = ssl->in_msg;
1937
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001938 /*
1939 * Authenticate before decrypt if enabled
1940 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1942 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001943 {
Hanno Becker992b6872017-11-09 18:57:39 +00001944 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001945 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001948
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001949 dec_msglen -= ssl->transform_in->maclen;
1950 ssl->in_msglen -= ssl->transform_in->maclen;
1951
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001952 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1953 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1954 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1955 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1960 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001961 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001962 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001966 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001967 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001968 ssl->transform_in->maclen );
1969
Hanno Becker992b6872017-11-09 18:57:39 +00001970 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1971 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001976 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001977 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001978 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001980
1981 /*
1982 * Check length sanity
1983 */
1984 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001987 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001989 }
1990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001992 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001993 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001996 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001997 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00001998 dec_msglen -= ssl->transform_in->ivlen;
1999 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002000
Paul Bakker48916f92012-09-16 19:57:18 +00002001 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002002 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002003 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002007 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002008 ssl->transform_in->ivlen,
2009 dec_msg, dec_msglen,
2010 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002013 return( ret );
2014 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002015
Paul Bakkercca5b812013-08-31 17:40:26 +02002016 if( dec_msglen != olen )
2017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002020 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2023 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002024 {
2025 /*
2026 * Save IV in SSL3 and TLS1
2027 */
2028 memcpy( ssl->transform_in->iv_dec,
2029 ssl->transform_in->cipher_ctx_dec.iv,
2030 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002032#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002033
2034 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002035
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002036 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002037 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#if defined(MBEDTLS_SSL_DEBUG_ALL)
2040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002041 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002042#endif
Paul Bakker45829992013-01-03 14:52:21 +01002043 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002044 correct = 0;
2045 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047#if defined(MBEDTLS_SSL_PROTO_SSL3)
2048 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 {
Paul Bakker48916f92012-09-16 19:57:18 +00002050 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052#if defined(MBEDTLS_SSL_DEBUG_ALL)
2053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002055 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002056#endif
Paul Bakker45829992013-01-03 14:52:21 +01002057 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 }
2059 }
2060 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2062#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2063 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2064 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 {
2066 /*
Paul Bakker45829992013-01-03 14:52:21 +01002067 * TLSv1+: always check the padding up to the first failure
2068 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002070 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002071 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002072 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002073
Paul Bakker956c9e02013-12-19 14:42:28 +01002074 /*
2075 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002076 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002077 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002078 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002079 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002080 *
2081 * In both cases we reset padding_idx to a safe value (0) to
2082 * prevent out-of-buffer reads.
2083 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002084 correct &= ( padlen <= ssl->in_msglen );
2085 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002086 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002087
2088 padding_idx *= correct;
2089
Angus Grattonb512bc12018-06-19 15:57:50 +10002090 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002091 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002092 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002093 pad_count += real_count *
2094 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2095 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002096
2097 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002100 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002102#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002103 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002105 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2107 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2110 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002111 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002112
2113 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002115 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002117 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002121 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002123#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002126#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002127
2128 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002129 * Authenticate if not done yet.
2130 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002132#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002133 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002134 {
Hanno Becker992b6872017-11-09 18:57:39 +00002135 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002136
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002137 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002138
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002139 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2140 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142#if defined(MBEDTLS_SSL_PROTO_SSL3)
2143 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002144 {
2145 ssl_mac( &ssl->transform_in->md_ctx_dec,
2146 ssl->transform_in->mac_dec,
2147 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002148 ssl->in_ctr, ssl->in_msgtype,
2149 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002150 }
2151 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2153#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2154 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2155 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002156 {
2157 /*
2158 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002159 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002160 *
2161 * Known timing attacks:
2162 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2163 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002164 * To compensate for different timings for the MAC calculation
2165 * depending on how much padding was removed (which is determined
2166 * by padlen), process extra_run more blocks through the hash
2167 * function.
2168 *
2169 * The formula in the paper is
2170 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2171 * where L1 is the size of the header plus the decrypted message
2172 * plus CBC padding and L2 is the size of the header plus the
2173 * decrypted message. This is for an underlying hash function
2174 * with 64-byte blocks.
2175 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2176 * correctly. We round down instead of up, so -56 is the correct
2177 * value for our calculations instead of -55.
2178 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002179 * Repeat the formula rather than defining a block_size variable.
2180 * This avoids requiring division by a variable at runtime
2181 * (which would be marginally less efficient and would require
2182 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002183 */
2184 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002185
2186 /*
2187 * The next two sizes are the minimum and maximum values of
2188 * in_msglen over all padlen values.
2189 *
2190 * They're independent of padlen, since we previously did
2191 * in_msglen -= padlen.
2192 *
2193 * Note that max_len + maclen is never more than the buffer
2194 * length, as we previously did in_msglen -= maclen too.
2195 */
2196 const size_t max_len = ssl->in_msglen + padlen;
2197 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2198
Gilles Peskine20b44082018-05-29 14:06:49 +02002199 switch( ssl->transform_in->ciphersuite_info->mac )
2200 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002201#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2202 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002203 case MBEDTLS_MD_MD5:
2204 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002205 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002206 /* 8 bytes of message size, 64-byte compression blocks */
2207 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2208 ( 13 + ssl->in_msglen + 8 ) / 64;
2209 break;
2210#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002211#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002212 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002213 /* 16 bytes of message size, 128-byte compression blocks */
2214 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2215 ( 13 + ssl->in_msglen + 16 ) / 128;
2216 break;
2217#endif
2218 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2221 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002222
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002223 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2226 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2227 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2228 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002229 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002230 /* Make sure we access everything even when padlen > 0. This
2231 * makes the synchronisation requirements for just-in-time
2232 * Prime+Probe attacks much tighter and hopefully impractical. */
2233 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002234 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002235
2236 /* Call mbedtls_md_process at least once due to cache attacks
2237 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002238 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002242
2243 /* Make sure we access all the memory that could contain the MAC,
2244 * before we check it in the next code block. This makes the
2245 * synchronisation requirements for just-in-time Prime+Probe
2246 * attacks much tighter and hopefully impractical. */
2247 ssl_read_memory( ssl->in_msg + min_len,
2248 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002249 }
2250 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2252 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2255 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002256 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002258#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002259 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2260 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2261 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002262#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
Hanno Becker992b6872017-11-09 18:57:39 +00002264 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2265 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267#if defined(MBEDTLS_SSL_DEBUG_ALL)
2268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002269#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002270 correct = 0;
2271 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002272 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002273
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002274 /*
2275 * Finally check the correct flag
2276 */
2277 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002279 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002280#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002281
2282 /* Make extra sure authentication was performed, exactly once */
2283 if( auth_done != 1 )
2284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002287 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002288
2289 if( ssl->in_msglen == 0 )
2290 {
Angus Gratton34817922018-06-19 15:58:22 +10002291#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2292 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2293 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2294 {
2295 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2297 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2298 }
2299#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2300
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 ssl->nb_zero++;
2302
2303 /*
2304 * Three or more empty messages may be a DoS attack
2305 * (excessive CPU consumption).
2306 */
2307 if( ssl->nb_zero > 3 )
2308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002310 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002312 }
2313 }
2314 else
2315 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002318 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002319 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002320 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002321 }
2322 else
2323#endif
2324 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002325 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002326 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2327 if( ++ssl->in_ctr[i - 1] != 0 )
2328 break;
2329
2330 /* The loop goes to its end iff the counter is wrapping */
2331 if( i == ssl_ep_len( ssl ) )
2332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2334 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002335 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002336 }
2337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002339
2340 return( 0 );
2341}
2342
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002343#undef MAC_NONE
2344#undef MAC_PLAINTEXT
2345#undef MAC_CIPHERTEXT
2346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002348/*
2349 * Compression/decompression functions
2350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002352{
2353 int ret;
2354 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002355 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002356 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002357 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002360
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002361 if( len_pre == 0 )
2362 return( 0 );
2363
Paul Bakker2770fbd2012-07-03 13:30:23 +00002364 memcpy( msg_pre, ssl->out_msg, len_pre );
2365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002367 ssl->out_msglen ) );
2368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002370 ssl->out_msg, ssl->out_msglen );
2371
Paul Bakker48916f92012-09-16 19:57:18 +00002372 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2373 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2374 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002375 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002376
Paul Bakker48916f92012-09-16 19:57:18 +00002377 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002378 if( ret != Z_OK )
2379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2381 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002382 }
2383
Angus Grattond8213d02016-05-25 20:56:48 +10002384 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002385 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002388 ssl->out_msglen ) );
2389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002391 ssl->out_msg, ssl->out_msglen );
2392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002394
2395 return( 0 );
2396}
2397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002399{
2400 int ret;
2401 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002402 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002403 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002404 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002407
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002408 if( len_pre == 0 )
2409 return( 0 );
2410
Paul Bakker2770fbd2012-07-03 13:30:23 +00002411 memcpy( msg_pre, ssl->in_msg, len_pre );
2412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002414 ssl->in_msglen ) );
2415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002417 ssl->in_msg, ssl->in_msglen );
2418
Paul Bakker48916f92012-09-16 19:57:18 +00002419 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2420 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2421 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002422 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002423 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002424
Paul Bakker48916f92012-09-16 19:57:18 +00002425 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002426 if( ret != Z_OK )
2427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2429 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002430 }
2431
Angus Grattond8213d02016-05-25 20:56:48 +10002432 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002433 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002436 ssl->in_msglen ) );
2437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002439 ssl->in_msg, ssl->in_msglen );
2440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002442
2443 return( 0 );
2444}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2448static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450#if defined(MBEDTLS_SSL_PROTO_DTLS)
2451static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002452{
2453 /* If renegotiation is not enforced, retransmit until we would reach max
2454 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002455 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002456 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002457 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002458 unsigned char doublings = 1;
2459
2460 while( ratio != 0 )
2461 {
2462 ++doublings;
2463 ratio >>= 1;
2464 }
2465
2466 if( ++ssl->renego_records_seen > doublings )
2467 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002469 return( 0 );
2470 }
2471 }
2472
2473 return( ssl_write_hello_request( ssl ) );
2474}
2475#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002477
Paul Bakker5121ce52009-01-03 21:22:43 +00002478/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002479 * Fill the input message buffer by appending data to it.
2480 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002481 *
2482 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2483 * available (from this read and/or a previous one). Otherwise, an error code
2484 * is returned (possibly EOF or WANT_READ).
2485 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002486 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2487 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2488 * since we always read a whole datagram at once.
2489 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002490 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002491 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002494{
Paul Bakker23986e52011-04-24 08:57:21 +00002495 int ret;
2496 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002500 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002503 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002505 }
2506
Angus Grattond8213d02016-05-25 20:56:48 +10002507 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002511 }
2512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002514 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002516 uint32_t timeout;
2517
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002518 /* Just to be sure */
2519 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2520 {
2521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2522 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2524 }
2525
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002526 /*
2527 * The point is, we need to always read a full datagram at once, so we
2528 * sometimes read more then requested, and handle the additional data.
2529 * It could be the rest of the current record (while fetching the
2530 * header) and/or some other records in the same datagram.
2531 */
2532
2533 /*
2534 * Move to the next record in the already read datagram if applicable
2535 */
2536 if( ssl->next_record_offset != 0 )
2537 {
2538 if( ssl->in_left < ssl->next_record_offset )
2539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002542 }
2543
2544 ssl->in_left -= ssl->next_record_offset;
2545
2546 if( ssl->in_left != 0 )
2547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002549 ssl->next_record_offset ) );
2550 memmove( ssl->in_hdr,
2551 ssl->in_hdr + ssl->next_record_offset,
2552 ssl->in_left );
2553 }
2554
2555 ssl->next_record_offset = 0;
2556 }
2557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002560
2561 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002562 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002563 */
2564 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002567 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002568 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002569
2570 /*
2571 * A record can't be split accross datagrams. If we need to read but
2572 * are not at the beginning of a new record, the caller did something
2573 * wrong.
2574 */
2575 if( ssl->in_left != 0 )
2576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002579 }
2580
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002581 /*
2582 * Don't even try to read if time's out already.
2583 * This avoids by-passing the timer when repeatedly receiving messages
2584 * that will end up being dropped.
2585 */
2586 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002587 {
2588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002589 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002590 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002591 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002592 {
Angus Grattond8213d02016-05-25 20:56:48 +10002593 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002596 timeout = ssl->handshake->retransmit_timeout;
2597 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002598 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002601
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002602 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002603 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2604 timeout );
2605 else
2606 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002609
2610 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002612 }
2613
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002614 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002617 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002620 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002621 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002624 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002625 }
2626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002630 return( ret );
2631 }
2632
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002633 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002634 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002636 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002638 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002639 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002642 return( ret );
2643 }
2644
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002645 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002646 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002648 }
2649
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 if( ret < 0 )
2651 return( ret );
2652
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002653 ssl->in_left = ret;
2654 }
2655 else
2656#endif
2657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002659 ssl->in_left, nb_want ) );
2660
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002661 while( ssl->in_left < nb_want )
2662 {
2663 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002664
2665 if( ssl_check_timer( ssl ) != 0 )
2666 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2667 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002668 {
2669 if( ssl->f_recv_timeout != NULL )
2670 {
2671 ret = ssl->f_recv_timeout( ssl->p_bio,
2672 ssl->in_hdr + ssl->in_left, len,
2673 ssl->conf->read_timeout );
2674 }
2675 else
2676 {
2677 ret = ssl->f_recv( ssl->p_bio,
2678 ssl->in_hdr + ssl->in_left, len );
2679 }
2680 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002683 ssl->in_left, nb_want ) );
2684 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002685
2686 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002688
2689 if( ret < 0 )
2690 return( ret );
2691
mohammad160352aecb92018-03-28 23:41:40 -07002692 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002693 {
Darryl Green11999bb2018-03-13 15:22:58 +00002694 MBEDTLS_SSL_DEBUG_MSG( 1,
2695 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002696 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002697 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2698 }
2699
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002700 ssl->in_left += ret;
2701 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 }
2703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
2706 return( 0 );
2707}
2708
2709/*
2710 * Flush any data not yet written
2711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002713{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002714 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002715 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002719 if( ssl->f_send == NULL )
2720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002722 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002724 }
2725
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002726 /* Avoid incrementing counter if data is flushed */
2727 if( ssl->out_left == 0 )
2728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002730 return( 0 );
2731 }
2732
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 while( ssl->out_left > 0 )
2734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2736 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
Hanno Becker2b1e3542018-08-06 11:19:13 +01002738 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002739 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
2743 if( ret <= 0 )
2744 return( ret );
2745
mohammad160352aecb92018-03-28 23:41:40 -07002746 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002747 {
Darryl Green11999bb2018-03-13 15:22:58 +00002748 MBEDTLS_SSL_DEBUG_MSG( 1,
2749 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002750 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002751 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2752 }
2753
Paul Bakker5121ce52009-01-03 21:22:43 +00002754 ssl->out_left -= ret;
2755 }
2756
Hanno Becker2b1e3542018-08-06 11:19:13 +01002757#if defined(MBEDTLS_SSL_PROTO_DTLS)
2758 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2759 {
2760 ssl->out_hdr = ssl->out_buf;
2761 }
2762 else
2763#endif
2764 {
2765 ssl->out_hdr = ssl->out_buf + 8;
2766 }
2767 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002770
2771 return( 0 );
2772}
2773
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002774/*
2775 * Functions to handle the DTLS retransmission state machine
2776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002778/*
2779 * Append current handshake message to current outgoing flight
2780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002782{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2785 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2786 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002787
2788 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002789 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002790 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002793 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002794 }
2795
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002796 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002797 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002800 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002801 }
2802
2803 /* Copy current handshake message with headers */
2804 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2805 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002806 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002807 msg->next = NULL;
2808
2809 /* Append to the current flight */
2810 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002811 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002812 else
2813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002815 while( cur->next != NULL )
2816 cur = cur->next;
2817 cur->next = msg;
2818 }
2819
Hanno Becker3b235902018-08-06 09:54:53 +01002820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002821 return( 0 );
2822}
2823
2824/*
2825 * Free the current flight of handshake messages
2826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002828{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 mbedtls_ssl_flight_item *cur = flight;
2830 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002831
2832 while( cur != NULL )
2833 {
2834 next = cur->next;
2835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 mbedtls_free( cur->p );
2837 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002838
2839 cur = next;
2840 }
2841}
2842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2844static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002845#endif
2846
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002847/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002848 * Swap transform_out and out_ctr with the alternative ones
2849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002851{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002853 unsigned char tmp_out_ctr[8];
2854
2855 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002858 return;
2859 }
2860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002862
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002863 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002864 tmp_transform = ssl->transform_out;
2865 ssl->transform_out = ssl->handshake->alt_transform_out;
2866 ssl->handshake->alt_transform_out = tmp_transform;
2867
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002868 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002869 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2870 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002871 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002872
2873 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002874 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2877 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2882 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002883 }
2884 }
2885#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002886}
2887
2888/*
2889 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002890 */
2891int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2892{
2893 int ret = 0;
2894
2895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2896
2897 ret = mbedtls_ssl_flight_transmit( ssl );
2898
2899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2900
2901 return( ret );
2902}
2903
2904/*
2905 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002906 *
2907 * Need to remember the current message in case flush_output returns
2908 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002909 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002910 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002911int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002912{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002913 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002917 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002919
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002920 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002921 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002922 ssl_swap_epochs( ssl );
2923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002925 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002926
2927 while( ssl->handshake->cur_msg != NULL )
2928 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002929 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002930 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002931
Hanno Beckere1dcb032018-08-17 16:47:58 +01002932 int const is_finished =
2933 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2934 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2935
Hanno Becker04da1892018-08-14 13:22:10 +01002936 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2937 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2938
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002939 /* Swap epochs before sending Finished: we can't do it after
2940 * sending ChangeCipherSpec, in case write returns WANT_READ.
2941 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002942 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002943 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002945 ssl_swap_epochs( ssl );
2946 }
2947
Hanno Becker67bc7c32018-08-06 11:33:50 +01002948 ret = ssl_get_remaining_payload_in_datagram( ssl );
2949 if( ret < 0 )
2950 return( ret );
2951 max_frag_len = (size_t) ret;
2952
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002953 /* CCS is copied as is, while HS messages may need fragmentation */
2954 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2955 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002956 if( max_frag_len == 0 )
2957 {
2958 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2959 return( ret );
2960
2961 continue;
2962 }
2963
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002964 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002965 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002966 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002967
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002968 /* Update position inside current message */
2969 ssl->handshake->cur_msg_p += cur->len;
2970 }
2971 else
2972 {
2973 const unsigned char * const p = ssl->handshake->cur_msg_p;
2974 const size_t hs_len = cur->len - 12;
2975 const size_t frag_off = p - ( cur->p + 12 );
2976 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002977 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002978
Hanno Beckere1dcb032018-08-17 16:47:58 +01002979 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002980 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002981 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002982 ssl_swap_epochs( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002983
2984 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2985 return( ret );
2986
2987 continue;
2988 }
2989 max_hs_frag_len = max_frag_len - 12;
2990
2991 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2992 max_hs_frag_len : rem_len;
2993
2994 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002995 {
2996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002997 (unsigned) cur_hs_frag_len,
2998 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002999 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003000
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003001 /* Messages are stored with handshake headers as if not fragmented,
3002 * copy beginning of headers then fill fragmentation fields.
3003 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3004 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003005
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003006 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3007 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3008 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3009
Hanno Becker67bc7c32018-08-06 11:33:50 +01003010 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3011 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3012 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003013
3014 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3015
Hanno Becker67bc7c32018-08-06 11:33:50 +01003016 /* Copy the handshame message content and set records fields */
3017 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3018 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003019 ssl->out_msgtype = cur->type;
3020
3021 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003022 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003023 }
3024
3025 /* If done with the current message move to the next one if any */
3026 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3027 {
3028 if( cur->next != NULL )
3029 {
3030 ssl->handshake->cur_msg = cur->next;
3031 ssl->handshake->cur_msg_p = cur->next->p + 12;
3032 }
3033 else
3034 {
3035 ssl->handshake->cur_msg = NULL;
3036 ssl->handshake->cur_msg_p = NULL;
3037 }
3038 }
3039
3040 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003041 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003044 return( ret );
3045 }
3046 }
3047
Hanno Becker67bc7c32018-08-06 11:33:50 +01003048 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3049 return( ret );
3050
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003051 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3053 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003054 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003057 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3058 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003059
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003061
3062 return( 0 );
3063}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003064
3065/*
3066 * To be called when the last message of an incoming flight is received.
3067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003069{
3070 /* We won't need to resend that one any more */
3071 ssl_flight_free( ssl->handshake->flight );
3072 ssl->handshake->flight = NULL;
3073 ssl->handshake->cur_msg = NULL;
3074
3075 /* The next incoming flight will start with this msg_seq */
3076 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3077
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003078 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003079 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003080
Hanno Becker0271f962018-08-16 13:23:47 +01003081 /* Clear future message buffering structure. */
3082 ssl_buffering_free( ssl );
3083
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003084 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003085 ssl_set_timer( ssl, 0 );
3086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3088 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003091 }
3092 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003094}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003095
3096/*
3097 * To be called when the last message of an outgoing flight is send.
3098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003100{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003101 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003102 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3105 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003108 }
3109 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003111}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003113
Paul Bakker5121ce52009-01-03 21:22:43 +00003114/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003115 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003116 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003117
3118/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003119 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003120 *
3121 * - fill in handshake headers
3122 * - update handshake checksum
3123 * - DTLS: save message for resending
3124 * - then pass to the record layer
3125 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003126 * DTLS: except for HelloRequest, messages are only queued, and will only be
3127 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003128 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003129 * Inputs:
3130 * - ssl->out_msglen: 4 + actual handshake message len
3131 * (4 is the size of handshake headers for TLS)
3132 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3133 * - ssl->out_msg + 4: the handshake message body
3134 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003135 * Ouputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003136 * - ssl->out_msglen: the length of the record contents
3137 * (including handshake headers but excluding record headers)
3138 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003139 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003140int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003141{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003142 int ret;
3143 const size_t hs_len = ssl->out_msglen - 4;
3144 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003145
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3147
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003148 /*
3149 * Sanity checks
3150 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003151 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3152 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3153 {
3154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3156 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003157
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003158 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3159 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3160 ssl->handshake == NULL )
3161 {
3162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3163 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3164 }
3165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003167 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003168 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003170 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3172 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003173 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003174#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003175
Hanno Beckerb50a2532018-08-06 11:52:54 +01003176 /* Double-check that we did not exceed the bounds
3177 * of the outgoing record buffer.
3178 * This should never fail as the various message
3179 * writing functions must obey the bounds of the
3180 * outgoing record buffer, but better be safe.
3181 *
3182 * Note: We deliberately do not check for the MTU or MFL here.
3183 */
3184 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3185 {
3186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3187 "size %u, maximum %u",
3188 (unsigned) ssl->out_msglen,
3189 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3190 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3191 }
3192
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003193 /*
3194 * Fill handshake headers
3195 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003197 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003198 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3199 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3200 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003201
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003202 /*
3203 * DTLS has additional fields in the Handshake layer,
3204 * between the length field and the actual payload:
3205 * uint16 message_seq;
3206 * uint24 fragment_offset;
3207 * uint24 fragment_length;
3208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003210 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003211 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003212 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003213 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003214 {
3215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3216 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003217 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003218 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003219 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3220 }
3221
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003222 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003223 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003224
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003225 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003226 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003227 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003228 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3229 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3230 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003231 }
3232 else
3233 {
3234 ssl->out_msg[4] = 0;
3235 ssl->out_msg[5] = 0;
3236 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003237
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003238 /* Handshake hashes are computed without fragmentation,
3239 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003240 memset( ssl->out_msg + 6, 0x00, 3 );
3241 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003242 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003244
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003245 /* Update running hashes of hanshake messages seen */
3246 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3247 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003248 }
3249
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003250 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003252 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003253 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003254 {
3255 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003258 return( ret );
3259 }
3260 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003261 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003262#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003263 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003264 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003265 {
3266 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3267 return( ret );
3268 }
3269 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003270
3271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3272
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003273 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003274}
3275
3276/*
3277 * Record layer functions
3278 */
3279
3280/*
3281 * Write current record.
3282 *
3283 * Uses:
3284 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3285 * - ssl->out_msglen: length of the record content (excl headers)
3286 * - ssl->out_msg: record content
3287 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003288int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003289{
3290 int ret, done = 0;
3291 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003292 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003293
3294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003297 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003299 {
3300 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003303 return( ret );
3304 }
3305
3306 len = ssl->out_msglen;
3307 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3311 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 ret = mbedtls_ssl_hw_record_write( ssl );
3316 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3319 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003320 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003321
3322 if( ret == 0 )
3323 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003324 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003326 if( !done )
3327 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003328 unsigned i;
3329 size_t protected_record_size;
3330
Paul Bakker05ef8352012-05-08 09:17:57 +00003331 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003333 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003334
Hanno Becker19859472018-08-06 09:40:20 +01003335 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003336 ssl->out_len[0] = (unsigned char)( len >> 8 );
3337 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003338
Paul Bakker48916f92012-09-16 19:57:18 +00003339 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003340 {
3341 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003344 return( ret );
3345 }
3346
3347 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003348 ssl->out_len[0] = (unsigned char)( len >> 8 );
3349 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003350 }
3351
Hanno Becker2b1e3542018-08-06 11:19:13 +01003352 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3353
3354#if defined(MBEDTLS_SSL_PROTO_DTLS)
3355 /* In case of DTLS, double-check that we don't exceed
3356 * the remaining space in the datagram. */
3357 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3358 {
3359 ret = ssl_get_maximum_datagram_size( ssl );
3360 if( ret < 0 )
3361 return( ret );
3362
3363 if( protected_record_size > (size_t) ret )
3364 {
3365 /* Should never happen */
3366 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3367 }
3368 }
3369#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Becker2b1e3542018-08-06 11:19:13 +01003372 "version = [%d:%d], msglen = %d",
3373 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2], len ) );
3374
Paul Bakker05ef8352012-05-08 09:17:57 +00003375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Becker2b1e3542018-08-06 11:19:13 +01003377 ssl->out_hdr, protected_record_size );
3378
3379 ssl->out_left += protected_record_size;
3380 ssl->out_hdr += protected_record_size;
3381 ssl_update_out_pointers( ssl, ssl->transform_out );
3382
Hanno Becker04484622018-08-06 09:49:38 +01003383 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3384 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3385 break;
3386
3387 /* The loop goes to its end iff the counter is wrapping */
3388 if( i == ssl_ep_len( ssl ) )
3389 {
3390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3391 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3392 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003393 }
3394
Hanno Becker67bc7c32018-08-06 11:33:50 +01003395#if defined(MBEDTLS_SSL_PROTO_DTLS)
3396 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3397 {
3398 size_t remaining = ssl_get_remaining_payload_in_datagram( ssl );
3399 if( remaining == 0 )
3400 flush = SSL_FORCE_FLUSH;
3401 else
3402 {
3403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Stil %u bytes available in current datagram", (unsigned) remaining ) );
3404 }
3405 }
3406#endif /* MBEDTLS_SSL_PROTO_DTLS */
3407
3408 if( ( flush == SSL_FORCE_FLUSH ) &&
3409 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003412 return( ret );
3413 }
3414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003416
3417 return( 0 );
3418}
3419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003421
3422static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3423{
3424 if( ssl->in_msglen < ssl->in_hslen ||
3425 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3426 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3427 {
3428 return( 1 );
3429 }
3430 return( 0 );
3431}
Hanno Becker44650b72018-08-16 12:51:11 +01003432
3433static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context *ssl )
3434{
3435 return( ( ssl->in_msg[9] << 16 ) |
3436 ( ssl->in_msg[10] << 8 ) |
3437 ssl->in_msg[11] );
3438}
3439
3440static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context *ssl )
3441{
3442 return( ( ssl->in_msg[6] << 16 ) |
3443 ( ssl->in_msg[7] << 8 ) |
3444 ssl->in_msg[8] );
3445}
3446
3447static int ssl_check_hs_header( mbedtls_ssl_context *ssl )
3448{
3449 uint32_t msg_len, frag_off, frag_len;
3450
3451 msg_len = ssl_get_hs_total_len( ssl );
3452 frag_off = ssl_get_hs_frag_off( ssl );
3453 frag_len = ssl_get_hs_frag_len( ssl );
3454
3455 if( frag_off > msg_len )
3456 return( -1 );
3457
3458 if( frag_len > msg_len - frag_off )
3459 return( -1 );
3460
3461 if( frag_len + 12 > ssl->in_msglen )
3462 return( -1 );
3463
3464 return( 0 );
3465}
3466
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003467/*
3468 * Mark bits in bitmask (used for DTLS HS reassembly)
3469 */
3470static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3471{
3472 unsigned int start_bits, end_bits;
3473
3474 start_bits = 8 - ( offset % 8 );
3475 if( start_bits != 8 )
3476 {
3477 size_t first_byte_idx = offset / 8;
3478
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003479 /* Special case */
3480 if( len <= start_bits )
3481 {
3482 for( ; len != 0; len-- )
3483 mask[first_byte_idx] |= 1 << ( start_bits - len );
3484
3485 /* Avoid potential issues with offset or len becoming invalid */
3486 return;
3487 }
3488
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003489 offset += start_bits; /* Now offset % 8 == 0 */
3490 len -= start_bits;
3491
3492 for( ; start_bits != 0; start_bits-- )
3493 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3494 }
3495
3496 end_bits = len % 8;
3497 if( end_bits != 0 )
3498 {
3499 size_t last_byte_idx = ( offset + len ) / 8;
3500
3501 len -= end_bits; /* Now len % 8 == 0 */
3502
3503 for( ; end_bits != 0; end_bits-- )
3504 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3505 }
3506
3507 memset( mask + offset / 8, 0xFF, len / 8 );
3508}
3509
3510/*
3511 * Check that bitmask is full
3512 */
3513static int ssl_bitmask_check( unsigned char *mask, size_t len )
3514{
3515 size_t i;
3516
3517 for( i = 0; i < len / 8; i++ )
3518 if( mask[i] != 0xFF )
3519 return( -1 );
3520
3521 for( i = 0; i < len % 8; i++ )
3522 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3523 return( -1 );
3524
3525 return( 0 );
3526}
3527
Hanno Becker56e205e2018-08-16 09:06:12 +01003528/* msg_len does not include the handshake header */
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003529static size_t ssl_get_reassembly_buffer_size( unsigned msg_len,
3530 unsigned add_bitmap )
Hanno Becker56e205e2018-08-16 09:06:12 +01003531{
3532 size_t alloc_len;
Hanno Becker56e205e2018-08-16 09:06:12 +01003533
3534 alloc_len = 12; /* Handshake header */
3535 alloc_len += msg_len; /* Content buffer */
Hanno Beckerd07df862018-08-16 09:14:58 +01003536
3537 if( add_bitmap )
3538 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Hanno Becker56e205e2018-08-16 09:06:12 +01003539
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003540 return( alloc_len );
Hanno Becker56e205e2018-08-16 09:06:12 +01003541}
3542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003543#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003544
Hanno Becker12555c62018-08-16 12:47:53 +01003545static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context *ssl )
3546{
3547 return( ( ssl->in_msg[1] << 16 ) |
3548 ( ssl->in_msg[2] << 8 ) |
3549 ssl->in_msg[3] );
3550}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003551
Simon Butcher99000142016-10-13 17:21:01 +01003552int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003553{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003557 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003559 }
3560
Hanno Becker12555c62018-08-16 12:47:53 +01003561 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003564 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003565 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003568 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003569 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003570 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003571 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003572
Hanno Becker44650b72018-08-16 12:51:11 +01003573 if( ssl_check_hs_header( ssl ) != 0 )
3574 {
3575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3576 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3577 }
3578
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003579 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003580 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3581 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3582 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3583 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003584 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003585 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3586 {
3587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3588 recv_msg_seq,
3589 ssl->handshake->in_msg_seq ) );
3590 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3591 }
3592
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003593 /* Retransmit only on last message from previous flight, to avoid
3594 * too many retransmissions.
3595 * Besides, No sane server ever retransmits HelloVerifyRequest */
3596 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003600 "message_seq = %d, start_of_flight = %d",
3601 recv_msg_seq,
3602 ssl->handshake->in_flight_start_seq ) );
3603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003607 return( ret );
3608 }
3609 }
3610 else
3611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003613 "message_seq = %d, expected = %d",
3614 recv_msg_seq,
3615 ssl->handshake->in_msg_seq ) );
3616 }
3617
Hanno Becker90333da2017-10-10 11:27:13 +01003618 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003619 }
3620 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003621
Hanno Becker6d97ef52018-08-16 13:09:04 +01003622 /* Message reassembly is handled alongside buffering of future
3623 * messages; the commonality is that both handshake fragments and
3624 * future messages cannot be forwarded immediately to the handshake
3625 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003626 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003629 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003630 }
3631 }
3632 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003634 /* With TLS we don't handle fragmentation (for now) */
3635 if( ssl->in_msglen < ssl->in_hslen )
3636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3638 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003639 }
3640
Simon Butcher99000142016-10-13 17:21:01 +01003641 return( 0 );
3642}
3643
3644void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3645{
Hanno Becker0271f962018-08-16 13:23:47 +01003646 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003647
Hanno Becker0271f962018-08-16 13:23:47 +01003648 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003649 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003650 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003651 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003652
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003653 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003655 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003656 ssl->handshake != NULL )
3657 {
Hanno Becker0271f962018-08-16 13:23:47 +01003658 unsigned offset;
3659 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003660
Hanno Becker0271f962018-08-16 13:23:47 +01003661 /* Increment handshake sequence number */
3662 hs->in_msg_seq++;
3663
3664 /*
3665 * Clear up handshake buffering and reassembly structure.
3666 */
3667
3668 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003669 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003670
3671 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003672 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3673 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003674 offset++, hs_buf++ )
3675 {
3676 *hs_buf = *(hs_buf + 1);
3677 }
3678
3679 /* Create a fresh last entry */
3680 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003681 }
3682#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003683}
3684
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003685/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003686 * DTLS anti-replay: RFC 6347 4.1.2.6
3687 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003688 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3689 * Bit n is set iff record number in_window_top - n has been seen.
3690 *
3691 * Usually, in_window_top is the last record number seen and the lsb of
3692 * in_window is set. The only exception is the initial state (record number 0
3693 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3696static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003697{
3698 ssl->in_window_top = 0;
3699 ssl->in_window = 0;
3700}
3701
3702static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3703{
3704 return( ( (uint64_t) buf[0] << 40 ) |
3705 ( (uint64_t) buf[1] << 32 ) |
3706 ( (uint64_t) buf[2] << 24 ) |
3707 ( (uint64_t) buf[3] << 16 ) |
3708 ( (uint64_t) buf[4] << 8 ) |
3709 ( (uint64_t) buf[5] ) );
3710}
3711
3712/*
3713 * Return 0 if sequence number is acceptable, -1 otherwise
3714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003716{
3717 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3718 uint64_t bit;
3719
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003720 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003721 return( 0 );
3722
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003723 if( rec_seqnum > ssl->in_window_top )
3724 return( 0 );
3725
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003726 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003727
3728 if( bit >= 64 )
3729 return( -1 );
3730
3731 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3732 return( -1 );
3733
3734 return( 0 );
3735}
3736
3737/*
3738 * Update replay window on new validated record
3739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003741{
3742 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3743
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003744 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003745 return;
3746
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003747 if( rec_seqnum > ssl->in_window_top )
3748 {
3749 /* Update window_top and the contents of the window */
3750 uint64_t shift = rec_seqnum - ssl->in_window_top;
3751
3752 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003753 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003754 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003755 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003756 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003757 ssl->in_window |= 1;
3758 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003759
3760 ssl->in_window_top = rec_seqnum;
3761 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003762 else
3763 {
3764 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003765 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003766
3767 if( bit < 64 ) /* Always true, but be extra sure */
3768 ssl->in_window |= (uint64_t) 1 << bit;
3769 }
3770}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003772
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003773#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003774/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003775static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3776
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003777/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003778 * Without any SSL context, check if a datagram looks like a ClientHello with
3779 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003780 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003781 *
3782 * - if cookie is valid, return 0
3783 * - if ClientHello looks superficially valid but cookie is not,
3784 * fill obuf and set olen, then
3785 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3786 * - otherwise return a specific error code
3787 */
3788static int ssl_check_dtls_clihlo_cookie(
3789 mbedtls_ssl_cookie_write_t *f_cookie_write,
3790 mbedtls_ssl_cookie_check_t *f_cookie_check,
3791 void *p_cookie,
3792 const unsigned char *cli_id, size_t cli_id_len,
3793 const unsigned char *in, size_t in_len,
3794 unsigned char *obuf, size_t buf_len, size_t *olen )
3795{
3796 size_t sid_len, cookie_len;
3797 unsigned char *p;
3798
3799 if( f_cookie_write == NULL || f_cookie_check == NULL )
3800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3801
3802 /*
3803 * Structure of ClientHello with record and handshake headers,
3804 * and expected values. We don't need to check a lot, more checks will be
3805 * done when actually parsing the ClientHello - skipping those checks
3806 * avoids code duplication and does not make cookie forging any easier.
3807 *
3808 * 0-0 ContentType type; copied, must be handshake
3809 * 1-2 ProtocolVersion version; copied
3810 * 3-4 uint16 epoch; copied, must be 0
3811 * 5-10 uint48 sequence_number; copied
3812 * 11-12 uint16 length; (ignored)
3813 *
3814 * 13-13 HandshakeType msg_type; (ignored)
3815 * 14-16 uint24 length; (ignored)
3816 * 17-18 uint16 message_seq; copied
3817 * 19-21 uint24 fragment_offset; copied, must be 0
3818 * 22-24 uint24 fragment_length; (ignored)
3819 *
3820 * 25-26 ProtocolVersion client_version; (ignored)
3821 * 27-58 Random random; (ignored)
3822 * 59-xx SessionID session_id; 1 byte len + sid_len content
3823 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3824 * ...
3825 *
3826 * Minimum length is 61 bytes.
3827 */
3828 if( in_len < 61 ||
3829 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3830 in[3] != 0 || in[4] != 0 ||
3831 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3832 {
3833 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3834 }
3835
3836 sid_len = in[59];
3837 if( sid_len > in_len - 61 )
3838 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3839
3840 cookie_len = in[60 + sid_len];
3841 if( cookie_len > in_len - 60 )
3842 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3843
3844 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3845 cli_id, cli_id_len ) == 0 )
3846 {
3847 /* Valid cookie */
3848 return( 0 );
3849 }
3850
3851 /*
3852 * If we get here, we've got an invalid cookie, let's prepare HVR.
3853 *
3854 * 0-0 ContentType type; copied
3855 * 1-2 ProtocolVersion version; copied
3856 * 3-4 uint16 epoch; copied
3857 * 5-10 uint48 sequence_number; copied
3858 * 11-12 uint16 length; olen - 13
3859 *
3860 * 13-13 HandshakeType msg_type; hello_verify_request
3861 * 14-16 uint24 length; olen - 25
3862 * 17-18 uint16 message_seq; copied
3863 * 19-21 uint24 fragment_offset; copied
3864 * 22-24 uint24 fragment_length; olen - 25
3865 *
3866 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3867 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3868 *
3869 * Minimum length is 28.
3870 */
3871 if( buf_len < 28 )
3872 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3873
3874 /* Copy most fields and adapt others */
3875 memcpy( obuf, in, 25 );
3876 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3877 obuf[25] = 0xfe;
3878 obuf[26] = 0xff;
3879
3880 /* Generate and write actual cookie */
3881 p = obuf + 28;
3882 if( f_cookie_write( p_cookie,
3883 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3884 {
3885 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3886 }
3887
3888 *olen = p - obuf;
3889
3890 /* Go back and fill length fields */
3891 obuf[27] = (unsigned char)( *olen - 28 );
3892
3893 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3894 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3895 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3896
3897 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3898 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3899
3900 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3901}
3902
3903/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003904 * Handle possible client reconnect with the same UDP quadruplet
3905 * (RFC 6347 Section 4.2.8).
3906 *
3907 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3908 * that looks like a ClientHello.
3909 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003910 * - if the input looks like a ClientHello without cookies,
3911 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003912 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003913 * - if the input looks like a ClientHello with a valid cookie,
3914 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003915 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003916 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003917 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003918 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003919 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3920 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003921 */
3922static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3923{
3924 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003925 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003926
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003927 ret = ssl_check_dtls_clihlo_cookie(
3928 ssl->conf->f_cookie_write,
3929 ssl->conf->f_cookie_check,
3930 ssl->conf->p_cookie,
3931 ssl->cli_id, ssl->cli_id_len,
3932 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003933 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003934
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003935 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3936
3937 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003938 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003939 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003940 * If the error is permanent we'll catch it later,
3941 * if it's not, then hopefully it'll work next time. */
3942 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3943
3944 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003945 }
3946
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003947 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003948 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003949 /* Got a valid cookie, partially reset context */
3950 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3951 {
3952 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3953 return( ret );
3954 }
3955
3956 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003957 }
3958
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003959 return( ret );
3960}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003961#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003962
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003963/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003964 * ContentType type;
3965 * ProtocolVersion version;
3966 * uint16 epoch; // DTLS only
3967 * uint48 sequence_number; // DTLS only
3968 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003969 *
3970 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003971 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003972 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3973 *
3974 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003975 * 1. proceed with the record if this function returns 0
3976 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3977 * 3. return CLIENT_RECONNECT if this function return that value
3978 * 4. drop the whole datagram if this function returns anything else.
3979 * Point 2 is needed when the peer is resending, and we have already received
3980 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003983{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003984 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003987
Paul Bakker5121ce52009-01-03 21:22:43 +00003988 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003989 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003990 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003993 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003994 ssl->in_msgtype,
3995 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003996
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003997 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3999 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4000 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4001 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004004
4005#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004006 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4007 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004008 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4009#endif /* MBEDTLS_SSL_PROTO_DTLS */
4010 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4011 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004013 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004014 }
4015
4016 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004017 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4020 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004021 }
4022
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004023 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4026 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004027 }
4028
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004029 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004030 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004031 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4034 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004035 }
4036
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004037 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004038 * DTLS-related tests.
4039 * Check epoch before checking length constraint because
4040 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4041 * message gets duplicated before the corresponding Finished message,
4042 * the second ChangeCipherSpec should be discarded because it belongs
4043 * to an old epoch, but not because its length is shorter than
4044 * the minimum record length for packets using the new record transform.
4045 * Note that these two kinds of failures are handled differently,
4046 * as an unexpected record is silently skipped but an invalid
4047 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004048 */
4049#if defined(MBEDTLS_SSL_PROTO_DTLS)
4050 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4051 {
4052 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4053
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004054 /* Check epoch (and sequence number) with DTLS */
4055 if( rec_epoch != ssl->in_epoch )
4056 {
4057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4058 "expected %d, received %d",
4059 ssl->in_epoch, rec_epoch ) );
4060
4061#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4062 /*
4063 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4064 * access the first byte of record content (handshake type), as we
4065 * have an active transform (possibly iv_len != 0), so use the
4066 * fact that the record header len is 13 instead.
4067 */
4068 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4069 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4070 rec_epoch == 0 &&
4071 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4072 ssl->in_left > 13 &&
4073 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4074 {
4075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4076 "from the same port" ) );
4077 return( ssl_handle_possible_reconnect( ssl ) );
4078 }
4079 else
4080#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004081 {
4082 /* Consider buffering the record. */
4083 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4084 {
4085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4086 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4087 }
4088
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004089 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004090 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004091 }
4092
4093#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4094 /* Replay detection only works for the current epoch */
4095 if( rec_epoch == ssl->in_epoch &&
4096 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4097 {
4098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4099 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4100 }
4101#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004102
Hanno Becker52c6dc62017-05-26 16:07:36 +01004103 /* Drop unexpected ApplicationData records,
4104 * except at the beginning of renegotiations */
4105 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4106 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4107#if defined(MBEDTLS_SSL_RENEGOTIATION)
4108 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4109 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4110#endif
4111 )
4112 {
4113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4114 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4115 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004116 }
4117#endif /* MBEDTLS_SSL_PROTO_DTLS */
4118
Hanno Becker52c6dc62017-05-26 16:07:36 +01004119
4120 /* Check length against bounds of the current transform and version */
4121 if( ssl->transform_in == NULL )
4122 {
4123 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004124 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004125 {
4126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4127 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4128 }
4129 }
4130 else
4131 {
4132 if( ssl->in_msglen < ssl->transform_in->minlen )
4133 {
4134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4135 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4136 }
4137
4138#if defined(MBEDTLS_SSL_PROTO_SSL3)
4139 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004140 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004141 {
4142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4143 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4144 }
4145#endif
4146#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4147 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4148 /*
4149 * TLS encrypted messages can have up to 256 bytes of padding
4150 */
4151 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4152 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004153 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004154 {
4155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4156 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4157 }
4158#endif
4159 }
4160
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004161 return( 0 );
4162}
Paul Bakker5121ce52009-01-03 21:22:43 +00004163
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004164/*
4165 * If applicable, decrypt (and decompress) record content
4166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004168{
4169 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004171 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4172 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004174#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4175 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179 ret = mbedtls_ssl_hw_record_read( ssl );
4180 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4183 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004184 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004185
4186 if( ret == 0 )
4187 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004188 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004189#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004190 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004191 {
4192 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004194 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004195 return( ret );
4196 }
4197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004199 ssl->in_msg, ssl->in_msglen );
4200
Angus Grattond8213d02016-05-25 20:56:48 +10004201 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4204 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004205 }
4206 }
4207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004208#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004209 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004210 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004211 {
4212 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004215 return( ret );
4216 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004217 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004221 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004224 }
4225#endif
4226
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004227 return( 0 );
4228}
4229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004230static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004231
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004232/*
4233 * Read a record.
4234 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004235 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4236 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4237 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004238 */
Hanno Becker1097b342018-08-15 14:09:41 +01004239
4240/* Helper functions for mbedtls_ssl_read_record(). */
4241static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004242static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4243static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004244
Hanno Becker40f50842018-08-15 14:48:01 +01004245#if defined(MBEDTLS_SSL_PROTO_DTLS)
4246static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01004247static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01004248static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01004249static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01004250static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl );
4251#endif /* MBEDTLS_SSL_PROTO_DTLS */
4252
Hanno Becker327c93b2018-08-15 13:56:18 +01004253int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004254 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004255{
4256 int ret;
4257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004259
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004260 if( ssl->keep_current_message == 0 )
4261 {
4262 do {
Simon Butcher99000142016-10-13 17:21:01 +01004263
Hanno Becker26994592018-08-15 14:14:59 +01004264 ret = ssl_consume_current_message( ssl );
4265 if( ret != 0 )
4266 return( ret );
4267
Hanno Beckere74d5562018-08-15 14:26:08 +01004268 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004269 {
Hanno Becker40f50842018-08-15 14:48:01 +01004270#if defined(MBEDTLS_SSL_PROTO_DTLS)
4271 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004272
Hanno Becker40f50842018-08-15 14:48:01 +01004273 /* We only check for buffered messages if the
4274 * current datagram is fully consumed. */
4275 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4276 ssl_another_record_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004277 {
Hanno Becker40f50842018-08-15 14:48:01 +01004278 if( ssl_load_buffered_message( ssl ) == 0 )
4279 have_buffered = 1;
4280 }
4281
4282 if( have_buffered == 0 )
4283#endif /* MBEDTLS_SSL_PROTO_DTLS */
4284 {
4285 ret = ssl_get_next_record( ssl );
4286 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4287 continue;
4288
4289 if( ret != 0 )
4290 {
4291 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4292 return( ret );
4293 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004294 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004295 }
4296
4297 ret = mbedtls_ssl_handle_message_type( ssl );
4298
Hanno Becker40f50842018-08-15 14:48:01 +01004299#if defined(MBEDTLS_SSL_PROTO_DTLS)
4300 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4301 {
4302 /* Buffer future message */
4303 ret = ssl_buffer_message( ssl );
4304 if( ret != 0 )
4305 return( ret );
4306
4307 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4308 }
4309#endif /* MBEDTLS_SSL_PROTO_DTLS */
4310
Hanno Becker90333da2017-10-10 11:27:13 +01004311 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4312 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004313
4314 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004315 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004316 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004317 return( ret );
4318 }
4319
Hanno Becker327c93b2018-08-15 13:56:18 +01004320 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004321 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004322 {
4323 mbedtls_ssl_update_handshake_status( ssl );
4324 }
Simon Butcher99000142016-10-13 17:21:01 +01004325 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004326 else
Simon Butcher99000142016-10-13 17:21:01 +01004327 {
Hanno Becker02f59072018-08-15 14:00:24 +01004328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004329 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004330 }
4331
4332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4333
4334 return( 0 );
4335}
4336
Hanno Becker40f50842018-08-15 14:48:01 +01004337#if defined(MBEDTLS_SSL_PROTO_DTLS)
4338static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl )
4339{
4340 if( ssl->in_left > ssl->next_record_offset )
4341 return( 1 );
4342
4343 return( 0 );
4344}
4345
4346static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4347{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004348 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004349 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004350 int ret = 0;
4351
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004352 if( hs == NULL )
4353 return( -1 );
4354
Hanno Beckere00ae372018-08-20 09:39:42 +01004355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4356
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004357 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4358 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4359 {
4360 /* Check if we have seen a ChangeCipherSpec before.
4361 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004362 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004363 {
4364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4365 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004366 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004367 }
4368
4369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Inject buffered CCS message" ) );
4370 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4371 ssl->in_msglen = 1;
4372 ssl->in_msg[0] = 1;
4373
4374 /* As long as they are equal, the exact value doesn't matter. */
4375 ssl->in_left = 0;
4376 ssl->next_record_offset = 0;
4377
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004378 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004379 goto exit;
4380 }
Hanno Becker37f95322018-08-16 13:55:32 +01004381
4382 /* Debug only */
4383 {
4384 unsigned offset;
4385 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4386 {
4387 hs_buf = &hs->buffering.hs[offset];
4388 if( hs_buf->is_valid == 1 )
4389 {
4390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4391 hs->in_msg_seq + offset,
4392 hs_buf->is_complete ? "fully" : "partitially" ) );
4393 }
4394 }
4395 }
4396
4397 /* Check if we have buffered and/or fully reassembled the
4398 * next handshake message. */
4399 hs_buf = &hs->buffering.hs[0];
4400 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4401 {
4402 /* Synthesize a record containing the buffered HS message. */
4403 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4404 ( hs_buf->data[2] << 8 ) |
4405 hs_buf->data[3];
4406
4407 /* Double-check that we haven't accidentally buffered
4408 * a message that doesn't fit into the input buffer. */
4409 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4410 {
4411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4412 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4413 }
4414
4415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4416 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4417 hs_buf->data, msg_len + 12 );
4418
4419 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4420 ssl->in_hslen = msg_len + 12;
4421 ssl->in_msglen = msg_len + 12;
4422 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4423
4424 ret = 0;
4425 goto exit;
4426 }
4427 else
4428 {
4429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4430 hs->in_msg_seq ) );
4431 }
4432
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004433 ret = -1;
4434
4435exit:
4436
4437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4438 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004439}
4440
4441static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4442{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004443 int ret = 0;
4444 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4445
4446 if( hs == NULL )
4447 return( 0 );
4448
4449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4450
4451 switch( ssl->in_msgtype )
4452 {
4453 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004455
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004456 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004457 break;
4458
4459 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004460 {
4461 unsigned recv_msg_seq_offset;
4462 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4463 mbedtls_ssl_hs_buffer *hs_buf;
4464 size_t msg_len = ssl->in_hslen - 12;
4465
4466 /* We should never receive an old handshake
4467 * message - double-check nonetheless. */
4468 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4469 {
4470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4471 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4472 }
4473
4474 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4475 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4476 {
4477 /* Silently ignore -- message too far in the future */
4478 MBEDTLS_SSL_DEBUG_MSG( 2,
4479 ( "Ignore future HS message with sequence number %u, "
4480 "buffering window %u - %u",
4481 recv_msg_seq, ssl->handshake->in_msg_seq,
4482 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4483
4484 goto exit;
4485 }
4486
4487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4488 recv_msg_seq, recv_msg_seq_offset ) );
4489
4490 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4491
4492 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004493 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004494 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004495 size_t reassembly_buf_sz;
4496
Hanno Becker37f95322018-08-16 13:55:32 +01004497 hs_buf->is_fragmented =
4498 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4499
4500 /* We copy the message back into the input buffer
4501 * after reassembly, so check that it's not too large.
4502 * This is an implementation-specific limitation
4503 * and not one from the standard, hence it is not
4504 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004505 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004506 {
4507 /* Ignore message */
4508 goto exit;
4509 }
4510
Hanno Beckere0b150f2018-08-21 15:51:03 +01004511 /* Check if we have enough space to buffer the message. */
4512 if( hs->buffering.total_bytes_buffered >
4513 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4514 {
4515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4517 }
4518
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004519 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4520 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004521
4522 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4523 hs->buffering.total_bytes_buffered ) )
4524 {
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004525 int offset;
4526
Hanno Beckere0b150f2018-08-21 15:51:03 +01004527 if( recv_msg_seq_offset > 0 )
4528 {
4529 /* If we can't buffer a future message because
4530 * of space limitations -- ignore. */
4531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
4532 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4533 (unsigned) hs->buffering.total_bytes_buffered ) );
4534 goto exit;
4535 }
Hanno Beckere1801392018-08-21 16:51:05 +01004536 else
4537 {
4538 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",
4539 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4540 (unsigned) hs->buffering.total_bytes_buffered ) );
4541 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004542
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004543 /* We don't have enough space to buffer the next expected
4544 * handshake message. Remove buffers used for future msgs
4545 * to gain space, starting with the most distant one. */
4546 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4547 offset >= 0; offset-- )
4548 {
4549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4550 offset ) );
4551
4552 ssl_buffering_free_slot( ssl, offset );
4553
4554 /* Check if we have enough space available now. */
4555 if( reassembly_buf_sz <=
4556 ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4557 hs->buffering.total_bytes_buffered ) )
4558 {
4559 break;
4560 }
4561 }
4562
4563 if( offset == -1 )
4564 {
4565 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 +01004566 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4567 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004568 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4569 goto exit;
4570 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004571 }
4572
4573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4574 msg_len ) );
4575
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004576 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4577 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004578 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004579 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004580 goto exit;
4581 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004582 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004583
4584 /* Prepare final header: copy msg_type, length and message_seq,
4585 * then add standardised fragment_offset and fragment_length */
4586 memcpy( hs_buf->data, ssl->in_msg, 6 );
4587 memset( hs_buf->data + 6, 0, 3 );
4588 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4589
4590 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004591
4592 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004593 }
4594 else
4595 {
4596 /* Make sure msg_type and length are consistent */
4597 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4598 {
4599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4600 /* Ignore */
4601 goto exit;
4602 }
4603 }
4604
Hanno Becker4422bbb2018-08-20 09:40:19 +01004605 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004606 {
4607 size_t frag_len, frag_off;
4608 unsigned char * const msg = hs_buf->data + 12;
4609
4610 /*
4611 * Check and copy current fragment
4612 */
4613
4614 /* Validation of header fields already done in
4615 * mbedtls_ssl_prepare_handshake_record(). */
4616 frag_off = ssl_get_hs_frag_off( ssl );
4617 frag_len = ssl_get_hs_frag_len( ssl );
4618
4619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4620 frag_off, frag_len ) );
4621 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4622
4623 if( hs_buf->is_fragmented )
4624 {
4625 unsigned char * const bitmask = msg + msg_len;
4626 ssl_bitmask_set( bitmask, frag_off, frag_len );
4627 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4628 msg_len ) == 0 );
4629 }
4630 else
4631 {
4632 hs_buf->is_complete = 1;
4633 }
4634
4635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4636 hs_buf->is_complete ? "" : "not yet " ) );
4637 }
4638
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004639 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004640 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004641
4642 default:
4643 break;
4644 }
4645
4646exit:
4647
4648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4649 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004650}
4651#endif /* MBEDTLS_SSL_PROTO_DTLS */
4652
Hanno Becker1097b342018-08-15 14:09:41 +01004653static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004654{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004655 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004656 * Consume last content-layer message and potentially
4657 * update in_msglen which keeps track of the contents'
4658 * consumption state.
4659 *
4660 * (1) Handshake messages:
4661 * Remove last handshake message, move content
4662 * and adapt in_msglen.
4663 *
4664 * (2) Alert messages:
4665 * Consume whole record content, in_msglen = 0.
4666 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004667 * (3) Change cipher spec:
4668 * Consume whole record content, in_msglen = 0.
4669 *
4670 * (4) Application data:
4671 * Don't do anything - the record layer provides
4672 * the application data as a stream transport
4673 * and consumes through mbedtls_ssl_read only.
4674 *
4675 */
4676
4677 /* Case (1): Handshake messages */
4678 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004679 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004680 /* Hard assertion to be sure that no application data
4681 * is in flight, as corrupting ssl->in_msglen during
4682 * ssl->in_offt != NULL is fatal. */
4683 if( ssl->in_offt != NULL )
4684 {
4685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4686 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4687 }
4688
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004689 /*
4690 * Get next Handshake message in the current record
4691 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004692
Hanno Becker4a810fb2017-05-24 16:27:30 +01004693 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004694 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004695 * current handshake content: If DTLS handshake
4696 * fragmentation is used, that's the fragment
4697 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004698 * size here is faulty and should be changed at
4699 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004700 * (2) While it doesn't seem to cause problems, one
4701 * has to be very careful not to assume that in_hslen
4702 * is always <= in_msglen in a sensible communication.
4703 * Again, it's wrong for DTLS handshake fragmentation.
4704 * The following check is therefore mandatory, and
4705 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004706 * Additionally, ssl->in_hslen might be arbitrarily out of
4707 * bounds after handling a DTLS message with an unexpected
4708 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004709 */
4710 if( ssl->in_hslen < ssl->in_msglen )
4711 {
4712 ssl->in_msglen -= ssl->in_hslen;
4713 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4714 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004715
Hanno Becker4a810fb2017-05-24 16:27:30 +01004716 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4717 ssl->in_msg, ssl->in_msglen );
4718 }
4719 else
4720 {
4721 ssl->in_msglen = 0;
4722 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004723
Hanno Becker4a810fb2017-05-24 16:27:30 +01004724 ssl->in_hslen = 0;
4725 }
4726 /* Case (4): Application data */
4727 else if( ssl->in_offt != NULL )
4728 {
4729 return( 0 );
4730 }
4731 /* Everything else (CCS & Alerts) */
4732 else
4733 {
4734 ssl->in_msglen = 0;
4735 }
4736
Hanno Becker1097b342018-08-15 14:09:41 +01004737 return( 0 );
4738}
4739
Hanno Beckere74d5562018-08-15 14:26:08 +01004740static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4741{
4742 if( ssl->in_msglen > 0 )
4743 return( 1 );
4744
4745 return( 0 );
4746}
4747
Hanno Becker5f066e72018-08-16 14:56:31 +01004748#if defined(MBEDTLS_SSL_PROTO_DTLS)
4749
4750static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4751{
4752 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4753 if( hs == NULL )
4754 return;
4755
4756 mbedtls_free( hs->buffering.future_record.data );
4757 hs->buffering.future_record.data = NULL;
4758}
4759
4760static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4761{
4762 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4763 unsigned char * rec;
4764 size_t rec_len;
4765 unsigned rec_epoch;
4766
4767 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4768 return( 0 );
4769
4770 if( hs == NULL )
4771 return( 0 );
4772
Hanno Becker5f066e72018-08-16 14:56:31 +01004773 rec = hs->buffering.future_record.data;
4774 rec_len = hs->buffering.future_record.len;
4775 rec_epoch = hs->buffering.future_record.epoch;
4776
4777 if( rec == NULL )
4778 return( 0 );
4779
Hanno Becker4cb782d2018-08-20 11:19:05 +01004780 /* Only consider loading future records if the
4781 * input buffer is empty. */
4782 if( ssl_another_record_in_datagram( ssl ) == 1 )
4783 return( 0 );
4784
Hanno Becker5f066e72018-08-16 14:56:31 +01004785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4786
4787 if( rec_epoch != ssl->in_epoch )
4788 {
4789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4790 goto exit;
4791 }
4792
4793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4794
4795 /* Double-check that the record is not too large */
4796 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4797 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4798 {
4799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4800 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4801 }
4802
4803 memcpy( ssl->in_hdr, rec, rec_len );
4804 ssl->in_left = rec_len;
4805 ssl->next_record_offset = 0;
4806
4807 ssl_free_buffered_record( ssl );
4808
4809exit:
4810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4811 return( 0 );
4812}
4813
4814static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4815{
4816 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4817 size_t const rec_hdr_len = 13;
4818
4819 /* Don't buffer future records outside handshakes. */
4820 if( hs == NULL )
4821 return( 0 );
4822
4823 /* Only buffer handshake records (we are only interested
4824 * in Finished messages). */
4825 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4826 return( 0 );
4827
4828 /* Don't buffer more than one future epoch record. */
4829 if( hs->buffering.future_record.data != NULL )
4830 return( 0 );
4831
4832 /* Buffer record */
4833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4834 ssl->in_epoch + 1 ) );
4835 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4836 rec_hdr_len + ssl->in_msglen );
4837
4838 /* ssl_parse_record_header() only considers records
4839 * of the next epoch as candidates for buffering. */
4840 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
4841 hs->buffering.future_record.len = rec_hdr_len + ssl->in_msglen;
4842
4843 hs->buffering.future_record.data =
4844 mbedtls_calloc( 1, hs->buffering.future_record.len );
4845 if( hs->buffering.future_record.data == NULL )
4846 {
4847 /* If we run out of RAM trying to buffer a
4848 * record from the next epoch, just ignore. */
4849 return( 0 );
4850 }
4851
4852 memcpy( hs->buffering.future_record.data,
4853 ssl->in_hdr, rec_hdr_len + ssl->in_msglen );
4854
4855 return( 0 );
4856}
4857
4858#endif /* MBEDTLS_SSL_PROTO_DTLS */
4859
Hanno Beckere74d5562018-08-15 14:26:08 +01004860static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004861{
4862 int ret;
4863
Hanno Becker5f066e72018-08-16 14:56:31 +01004864#if defined(MBEDTLS_SSL_PROTO_DTLS)
4865 /* We might have buffered a future record; if so,
4866 * and if the epoch matches now, load it.
4867 * On success, this call will set ssl->in_left to
4868 * the length of the buffered record, so that
4869 * the calls to ssl_fetch_input() below will
4870 * essentially be no-ops. */
4871 ret = ssl_load_buffered_record( ssl );
4872 if( ret != 0 )
4873 return( ret );
4874#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004876 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004878 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004879 return( ret );
4880 }
4881
4882 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004884#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004885 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4886 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004887 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004888 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4889 {
4890 ret = ssl_buffer_future_record( ssl );
4891 if( ret != 0 )
4892 return( ret );
4893
4894 /* Fall through to handling of unexpected records */
4895 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4896 }
4897
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004898 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4899 {
4900 /* Skip unexpected record (but not whole datagram) */
4901 ssl->next_record_offset = ssl->in_msglen
4902 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004903
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4905 "(header)" ) );
4906 }
4907 else
4908 {
4909 /* Skip invalid record and the rest of the datagram */
4910 ssl->next_record_offset = 0;
4911 ssl->in_left = 0;
4912
4913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4914 "(header)" ) );
4915 }
4916
4917 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004918 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004919 }
4920#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004921 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004922 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004923
4924 /*
4925 * Read and optionally decrypt the message contents
4926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004927 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4928 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004930 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004931 return( ret );
4932 }
4933
4934 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004935#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004936 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004938 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004939 if( ssl->next_record_offset < ssl->in_left )
4940 {
4941 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4942 }
4943 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004944 else
4945#endif
4946 ssl->in_left = 0;
4947
4948 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004951 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004952 {
4953 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4955 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004956 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004957 /* Except when waiting for Finished as a bad mac here
4958 * probably means something went wrong in the handshake
4959 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4960 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4961 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4962 {
4963#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4964 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4965 {
4966 mbedtls_ssl_send_alert_message( ssl,
4967 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4968 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4969 }
4970#endif
4971 return( ret );
4972 }
4973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004975 if( ssl->conf->badmac_limit != 0 &&
4976 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4979 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004980 }
4981#endif
4982
Hanno Becker4a810fb2017-05-24 16:27:30 +01004983 /* As above, invalid records cause
4984 * dismissal of the whole datagram. */
4985
4986 ssl->next_record_offset = 0;
4987 ssl->in_left = 0;
4988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004990 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004991 }
4992
4993 return( ret );
4994 }
4995 else
4996#endif
4997 {
4998 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5000 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005002 mbedtls_ssl_send_alert_message( ssl,
5003 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5004 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005005 }
5006#endif
5007 return( ret );
5008 }
5009 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005010
Simon Butcher99000142016-10-13 17:21:01 +01005011 return( 0 );
5012}
5013
5014int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5015{
5016 int ret;
5017
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005018 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005019 * Handle particular types of records
5020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005021 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005022 {
Simon Butcher99000142016-10-13 17:21:01 +01005023 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5024 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005025 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005026 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005027 }
5028
Hanno Beckere678eaa2018-08-21 14:57:46 +01005029 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005030 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005031 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005032 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5034 ssl->in_msglen ) );
5035 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005036 }
5037
Hanno Beckere678eaa2018-08-21 14:57:46 +01005038 if( ssl->in_msg[0] != 1 )
5039 {
5040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5041 ssl->in_msg[0] ) );
5042 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5043 }
5044
5045#if defined(MBEDTLS_SSL_PROTO_DTLS)
5046 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5047 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5048 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5049 {
5050 if( ssl->handshake == NULL )
5051 {
5052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5053 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5054 }
5055
5056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5057 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5058 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005059#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005060 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005063 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005064 if( ssl->in_msglen != 2 )
5065 {
5066 /* Note: Standard allows for more than one 2 byte alert
5067 to be packed in a single message, but Mbed TLS doesn't
5068 currently support this. */
5069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5070 ssl->in_msglen ) );
5071 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5072 }
5073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005075 ssl->in_msg[0], ssl->in_msg[1] ) );
5076
5077 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005078 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005080 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005083 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005085 }
5086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005087 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5088 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5091 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005092 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005093
5094#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5095 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5096 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5097 {
Hanno Becker90333da2017-10-10 11:27:13 +01005098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005099 /* Will be handled when trying to parse ServerHello */
5100 return( 0 );
5101 }
5102#endif
5103
5104#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5105 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5106 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5107 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5108 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5109 {
5110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5111 /* Will be handled in mbedtls_ssl_parse_certificate() */
5112 return( 0 );
5113 }
5114#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5115
5116 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005117 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005118 }
5119
Hanno Beckerc76c6192017-06-06 10:03:17 +01005120#if defined(MBEDTLS_SSL_PROTO_DTLS)
5121 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5122 ssl->handshake != NULL &&
5123 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5124 {
5125 ssl_handshake_wrapup_free_hs_transform( ssl );
5126 }
5127#endif
5128
Paul Bakker5121ce52009-01-03 21:22:43 +00005129 return( 0 );
5130}
5131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005133{
5134 int ret;
5135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005136 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5137 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5138 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005139 {
5140 return( ret );
5141 }
5142
5143 return( 0 );
5144}
5145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005146int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005147 unsigned char level,
5148 unsigned char message )
5149{
5150 int ret;
5151
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005152 if( ssl == NULL || ssl->conf == NULL )
5153 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005156 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005158 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005159 ssl->out_msglen = 2;
5160 ssl->out_msg[0] = level;
5161 ssl->out_msg[1] = message;
5162
Hanno Becker67bc7c32018-08-06 11:33:50 +01005163 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005165 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005166 return( ret );
5167 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005169
5170 return( 0 );
5171}
5172
Paul Bakker5121ce52009-01-03 21:22:43 +00005173/*
5174 * Handshake functions
5175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005176#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5177 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5178 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5179 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5180 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5181 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5182 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005183/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005184int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005185{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005186 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005190 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5191 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005192 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5193 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005196 ssl->state++;
5197 return( 0 );
5198 }
5199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005202}
5203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005204int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005205{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005206 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005210 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5211 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005212 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5213 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005216 ssl->state++;
5217 return( 0 );
5218 }
5219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5221 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005222}
Gilles Peskinef9828522017-05-03 12:28:43 +02005223
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005224#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005225/* Some certificate support -> implement write and parse */
5226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005227int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005228{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005230 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005231 const mbedtls_x509_crt *crt;
5232 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005236 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5237 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005238 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5239 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005242 ssl->state++;
5243 return( 0 );
5244 }
5245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005246#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005247 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005248 {
5249 if( ssl->client_auth == 0 )
5250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005252 ssl->state++;
5253 return( 0 );
5254 }
5255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005256#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005257 /*
5258 * If using SSLv3 and got no cert, send an Alert message
5259 * (otherwise an empty Certificate message will be sent).
5260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005261 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5262 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005263 {
5264 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005265 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5266 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5267 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005270 goto write_msg;
5271 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005272#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005273 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274#endif /* MBEDTLS_SSL_CLI_C */
5275#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005276 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005278 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5281 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005282 }
5283 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005284#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005286 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005287
5288 /*
5289 * 0 . 0 handshake type
5290 * 1 . 3 handshake length
5291 * 4 . 6 length of all certs
5292 * 7 . 9 length of cert. 1
5293 * 10 . n-1 peer certificate
5294 * n . n+2 length of cert. 2
5295 * n+3 . ... upper level cert, etc.
5296 */
5297 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005298 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005299
Paul Bakker29087132010-03-21 21:03:34 +00005300 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005301 {
5302 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005303 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005306 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005307 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005308 }
5309
5310 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5311 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5312 ssl->out_msg[i + 2] = (unsigned char)( n );
5313
5314 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5315 i += n; crt = crt->next;
5316 }
5317
5318 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5319 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5320 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5321
5322 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5324 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005325
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005326#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005327write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005328#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005329
5330 ssl->state++;
5331
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005332 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005333 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005335 return( ret );
5336 }
5337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005339
Paul Bakkered27a042013-04-18 22:46:23 +02005340 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005341}
5342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005344{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00005346 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005348 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005349 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5354 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005355 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5356 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005359 ssl->state++;
5360 return( 0 );
5361 }
5362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005363#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005364 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005365 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5366 {
5367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5368 ssl->state++;
5369 return( 0 );
5370 }
5371
5372#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5373 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
5374 authmode = ssl->handshake->sni_authmode;
5375#endif
5376
5377 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5378 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005379 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005380 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005382 ssl->state++;
5383 return( 0 );
5384 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005385#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005386
Hanno Becker327c93b2018-08-15 13:56:18 +01005387 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005388 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005389 /* mbedtls_ssl_read_record may have sent an alert already. We
5390 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005392 return( ret );
5393 }
5394
5395 ssl->state++;
5396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397#if defined(MBEDTLS_SSL_SRV_C)
5398#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005399 /*
5400 * Check if the client sent an empty certificate
5401 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005402 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005404 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005405 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5407 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5408 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005411
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005412 /* The client was asked for a certificate but didn't send
5413 one. The client should know what's going on, so we
5414 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005415 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005416 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005417 return( 0 );
5418 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005420 }
5421 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5425 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005426 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005429 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5430 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5431 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5432 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005435
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005436 /* The client was asked for a certificate but didn't send
5437 one. The client should know what's going on, so we
5438 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005439 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005440 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005441 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005442 else
5443 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005444 }
5445 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5447 MBEDTLS_SSL_PROTO_TLS1_2 */
5448#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005450 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005453 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5454 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005456 }
5457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5459 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005462 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5463 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005465 }
5466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005468
Paul Bakker5121ce52009-01-03 21:22:43 +00005469 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005471 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005472 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005473
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005474 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005478 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5479 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005481 }
5482
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005483 /* In case we tried to reuse a session but it failed */
5484 if( ssl->session_negotiate->peer_cert != NULL )
5485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5487 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005488 }
5489
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005490 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005491 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005492 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005495 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5496 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005497 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005498 }
5499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005500 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005501
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005502 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005503
5504 while( i < ssl->in_hslen )
5505 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005506 if ( i + 3 > ssl->in_hslen ) {
5507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5508 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5509 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5510 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5511 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005512 if( ssl->in_msg[i] != 0 )
5513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005515 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5516 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005517 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005518 }
5519
5520 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5521 | (unsigned int) ssl->in_msg[i + 2];
5522 i += 3;
5523
5524 if( n < 128 || i + n > ssl->in_hslen )
5525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005527 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5528 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005530 }
5531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005532 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005533 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005534 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005535 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005536 case 0: /*ok*/
5537 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5538 /* Ignore certificate with an unknown algorithm: maybe a
5539 prior certificate was already trusted. */
5540 break;
5541
5542 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5543 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5544 goto crt_parse_der_failed;
5545
5546 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5547 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5548 goto crt_parse_der_failed;
5549
5550 default:
5551 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5552 crt_parse_der_failed:
5553 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005554 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005555 return( ret );
5556 }
5557
5558 i += n;
5559 }
5560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005562
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005563 /*
5564 * On client, make sure the server cert doesn't change during renego to
5565 * avoid "triple handshake" attack: https://secure-resumption.com/
5566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005567#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005568 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005569 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005570 {
5571 if( ssl->session->peer_cert == NULL )
5572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005574 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5575 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005576 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005577 }
5578
5579 if( ssl->session->peer_cert->raw.len !=
5580 ssl->session_negotiate->peer_cert->raw.len ||
5581 memcmp( ssl->session->peer_cert->raw.p,
5582 ssl->session_negotiate->peer_cert->raw.p,
5583 ssl->session->peer_cert->raw.len ) != 0 )
5584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005586 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5587 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005589 }
5590 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005591#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005592
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005593 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005594 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005595 mbedtls_x509_crt *ca_chain;
5596 mbedtls_x509_crl *ca_crl;
5597
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005598#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005599 if( ssl->handshake->sni_ca_chain != NULL )
5600 {
5601 ca_chain = ssl->handshake->sni_ca_chain;
5602 ca_crl = ssl->handshake->sni_ca_crl;
5603 }
5604 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005605#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005606 {
5607 ca_chain = ssl->conf->ca_chain;
5608 ca_crl = ssl->conf->ca_crl;
5609 }
5610
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005611 /*
5612 * Main check: verify certificate
5613 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005614 ret = mbedtls_x509_crt_verify_with_profile(
5615 ssl->session_negotiate->peer_cert,
5616 ca_chain, ca_crl,
5617 ssl->conf->cert_profile,
5618 ssl->hostname,
5619 &ssl->session_negotiate->verify_result,
5620 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005621
5622 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005625 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005626
5627 /*
5628 * Secondary checks: always done, but change 'ret' only if it was 0
5629 */
5630
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005631#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005634
5635 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005637 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005638 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005639 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005642 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005644 }
5645 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005646#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005649 ciphersuite_info,
5650 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005651 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005654 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005656 }
5657
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005658 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5659 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5660 * with details encoded in the verification flags. All other kinds
5661 * of error codes, including those from the user provided f_vrfy
5662 * functions, are treated as fatal and lead to a failure of
5663 * ssl_parse_certificate even if verification was optional. */
5664 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5665 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5666 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5667 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005668 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005669 }
5670
5671 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5672 {
5673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5674 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5675 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005676
5677 if( ret != 0 )
5678 {
5679 /* The certificate may have been rejected for several reasons.
5680 Pick one and send the corresponding alert. Which alert to send
5681 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005682 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5683 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5684 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5685 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5686 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5687 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5688 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5689 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5690 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5691 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5692 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5693 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5694 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5695 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5696 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5697 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5698 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5699 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5700 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5701 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005702 else
5703 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005704 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5705 alert );
5706 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005707
Hanno Beckere6706e62017-05-15 16:05:15 +01005708#if defined(MBEDTLS_DEBUG_C)
5709 if( ssl->session_negotiate->verify_result != 0 )
5710 {
5711 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5712 ssl->session_negotiate->verify_result ) );
5713 }
5714 else
5715 {
5716 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5717 }
5718#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005719 }
5720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005722
5723 return( ret );
5724}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5726 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5727 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5728 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5729 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5730 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5731 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005734{
5735 int ret;
5736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005739 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005740 ssl->out_msglen = 1;
5741 ssl->out_msg[0] = 1;
5742
Paul Bakker5121ce52009-01-03 21:22:43 +00005743 ssl->state++;
5744
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005745 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005746 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005747 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005748 return( ret );
5749 }
5750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005752
5753 return( 0 );
5754}
5755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005756int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005757{
5758 int ret;
5759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005761
Hanno Becker327c93b2018-08-15 13:56:18 +01005762 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005764 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005765 return( ret );
5766 }
5767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005771 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5772 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005774 }
5775
Hanno Beckere678eaa2018-08-21 14:57:46 +01005776 /* CCS records are only accepted if they have length 1 and content '1',
5777 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005778
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005779 /*
5780 * Switch to our negotiated transform and session parameters for inbound
5781 * data.
5782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005783 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005784 ssl->transform_in = ssl->transform_negotiate;
5785 ssl->session_in = ssl->session_negotiate;
5786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005787#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005788 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005791 ssl_dtls_replay_reset( ssl );
5792#endif
5793
5794 /* Increment epoch */
5795 if( ++ssl->in_epoch == 0 )
5796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005798 /* This is highly unlikely to happen for legitimate reasons, so
5799 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005801 }
5802 }
5803 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005805 memset( ssl->in_ctr, 0, 8 );
5806
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005807 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005809#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5810 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005815 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5816 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005818 }
5819 }
5820#endif
5821
Paul Bakker5121ce52009-01-03 21:22:43 +00005822 ssl->state++;
5823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005825
5826 return( 0 );
5827}
5828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005829void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5830 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005831{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005832 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5835 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5836 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005837 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005838 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005839#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5841#if defined(MBEDTLS_SHA512_C)
5842 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005843 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5844 else
5845#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846#if defined(MBEDTLS_SHA256_C)
5847 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005848 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005849 else
5850#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005854 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005855 }
Paul Bakker380da532012-04-18 16:10:25 +00005856}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005858void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005859{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005860#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5861 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005862 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5863 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005864#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5866#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005867 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005868#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005870 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005871#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005873}
5874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005875static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005876 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005877{
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)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005880 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5881 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005882#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5884#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005885 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005886#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005888 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005889#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005890#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005891}
5892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005893#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5894 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5895static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005896 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005897{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005898 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5899 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005900}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005901#endif
Paul Bakker380da532012-04-18 16:10:25 +00005902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005903#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5904#if defined(MBEDTLS_SHA256_C)
5905static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005906 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005907{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005908 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005909}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005910#endif
Paul Bakker380da532012-04-18 16:10:25 +00005911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912#if defined(MBEDTLS_SHA512_C)
5913static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005914 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005915{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005916 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005917}
Paul Bakker769075d2012-11-24 11:26:46 +01005918#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005919#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005921#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005922static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005923 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005924{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005925 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005926 mbedtls_md5_context md5;
5927 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005928
Paul Bakker5121ce52009-01-03 21:22:43 +00005929 unsigned char padbuf[48];
5930 unsigned char md5sum[16];
5931 unsigned char sha1sum[20];
5932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005933 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005934 if( !session )
5935 session = ssl->session;
5936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005938
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005939 mbedtls_md5_init( &md5 );
5940 mbedtls_sha1_init( &sha1 );
5941
5942 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5943 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005944
5945 /*
5946 * SSLv3:
5947 * hash =
5948 * MD5( master + pad2 +
5949 * MD5( handshake + sender + master + pad1 ) )
5950 * + SHA1( master + pad2 +
5951 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005952 */
5953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005954#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005955 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5956 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005957#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005959#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005960 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5961 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005962#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005965 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005966
Paul Bakker1ef83d62012-04-11 12:09:53 +00005967 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005968
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005969 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5970 mbedtls_md5_update_ret( &md5, session->master, 48 );
5971 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5972 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005973
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005974 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5975 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5976 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5977 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005978
Paul Bakker1ef83d62012-04-11 12:09:53 +00005979 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005980
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005981 mbedtls_md5_starts_ret( &md5 );
5982 mbedtls_md5_update_ret( &md5, session->master, 48 );
5983 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5984 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5985 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005986
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005987 mbedtls_sha1_starts_ret( &sha1 );
5988 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5989 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5990 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5991 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005994
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005995 mbedtls_md5_free( &md5 );
5996 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005997
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005998 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5999 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6000 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006004#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006006#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006007static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006009{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006010 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006011 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006012 mbedtls_md5_context md5;
6013 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006014 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006017 if( !session )
6018 session = ssl->session;
6019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006021
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006022 mbedtls_md5_init( &md5 );
6023 mbedtls_sha1_init( &sha1 );
6024
6025 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6026 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006027
Paul Bakker1ef83d62012-04-11 12:09:53 +00006028 /*
6029 * TLSv1:
6030 * hash = PRF( master, finished_label,
6031 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6032 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006035 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6036 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006037#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006040 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6041 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006042#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006045 ? "client finished"
6046 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006047
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006048 mbedtls_md5_finish_ret( &md5, padbuf );
6049 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006050
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006051 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006052 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006055
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006056 mbedtls_md5_free( &md5 );
6057 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006058
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006059 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006062}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6066#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006067static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006069{
6070 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006071 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006072 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006073 unsigned char padbuf[32];
6074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006076 if( !session )
6077 session = ssl->session;
6078
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006079 mbedtls_sha256_init( &sha256 );
6080
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006082
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006083 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006084
6085 /*
6086 * TLSv1.2:
6087 * hash = PRF( master, finished_label,
6088 * Hash( handshake ) )[0.11]
6089 */
6090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006091#if !defined(MBEDTLS_SHA256_ALT)
6092 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006093 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006094#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006097 ? "client finished"
6098 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006099
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006100 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006101
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006102 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006103 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006106
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006107 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006108
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006109 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006112}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006116static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006117 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006118{
6119 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006120 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006121 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006122 unsigned char padbuf[48];
6123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006124 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006125 if( !session )
6126 session = ssl->session;
6127
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006128 mbedtls_sha512_init( &sha512 );
6129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006131
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006132 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006133
6134 /*
6135 * TLSv1.2:
6136 * hash = PRF( master, finished_label,
6137 * Hash( handshake ) )[0.11]
6138 */
6139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006141 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6142 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006143#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006146 ? "client finished"
6147 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006148
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006149 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006150
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006151 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006152 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006155
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006156 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006157
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006158 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006162#endif /* MBEDTLS_SHA512_C */
6163#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006165static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006166{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006168
6169 /*
6170 * Free our handshake params
6171 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006172 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006173 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006174 ssl->handshake = NULL;
6175
6176 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006177 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006178 */
6179 if( ssl->transform )
6180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006181 mbedtls_ssl_transform_free( ssl->transform );
6182 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006183 }
6184 ssl->transform = ssl->transform_negotiate;
6185 ssl->transform_negotiate = NULL;
6186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006188}
6189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006191{
6192 int resume = ssl->handshake->resume;
6193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196#if defined(MBEDTLS_SSL_RENEGOTIATION)
6197 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006200 ssl->renego_records_seen = 0;
6201 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006202#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006203
6204 /*
6205 * Free the previous session and switch in the current one
6206 */
Paul Bakker0a597072012-09-25 21:55:46 +00006207 if( ssl->session )
6208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006210 /* RFC 7366 3.1: keep the EtM state */
6211 ssl->session_negotiate->encrypt_then_mac =
6212 ssl->session->encrypt_then_mac;
6213#endif
6214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 mbedtls_ssl_session_free( ssl->session );
6216 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006217 }
6218 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006219 ssl->session_negotiate = NULL;
6220
Paul Bakker0a597072012-09-25 21:55:46 +00006221 /*
6222 * Add cache entry
6223 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006224 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006225 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006226 resume == 0 )
6227 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006228 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006230 }
Paul Bakker0a597072012-09-25 21:55:46 +00006231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006233 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006234 ssl->handshake->flight != NULL )
6235 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006236 /* Cancel handshake timer */
6237 ssl_set_timer( ssl, 0 );
6238
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006239 /* Keep last flight around in case we need to resend it:
6240 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006241 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006242 }
6243 else
6244#endif
6245 ssl_handshake_wrapup_free_hs_transform( ssl );
6246
Paul Bakker48916f92012-09-16 19:57:18 +00006247 ssl->state++;
6248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006250}
6251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006253{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006254 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006257
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006258 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006259
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006260 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006261
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006262 /*
6263 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6264 * may define some other value. Currently (early 2016), no defined
6265 * ciphersuite does this (and this is unlikely to change as activity has
6266 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006271 ssl->verify_data_len = hash_len;
6272 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006273#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006274
Paul Bakker5121ce52009-01-03 21:22:43 +00006275 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6277 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006278
6279 /*
6280 * In case of session resuming, invert the client and server
6281 * ChangeCipherSpec messages order.
6282 */
Paul Bakker0a597072012-09-25 21:55:46 +00006283 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006286 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006287 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006288#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006290 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006292#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006293 }
6294 else
6295 ssl->state++;
6296
Paul Bakker48916f92012-09-16 19:57:18 +00006297 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006298 * Switch to our negotiated transform and session parameters for outbound
6299 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006304 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006305 {
6306 unsigned char i;
6307
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006308 /* Remember current epoch settings for resending */
6309 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006310 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006311
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006312 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006313 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006314
6315 /* Increment epoch */
6316 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006317 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006318 break;
6319
6320 /* The loop goes to its end iff the counter is wrapping */
6321 if( i == 0 )
6322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6324 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006325 }
6326 }
6327 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006328#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006329 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006330
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006331 ssl->transform_out = ssl->transform_negotiate;
6332 ssl->session_out = ssl->session_negotiate;
6333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6335 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6340 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006341 }
6342 }
6343#endif
6344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006345#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006346 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006348#endif
6349
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006350 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006351 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006352 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006353 return( ret );
6354 }
6355
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006356#if defined(MBEDTLS_SSL_PROTO_DTLS)
6357 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6358 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6359 {
6360 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6361 return( ret );
6362 }
6363#endif
6364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006366
6367 return( 0 );
6368}
6369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006370#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006371#define SSL_MAX_HASH_LEN 36
6372#else
6373#define SSL_MAX_HASH_LEN 12
6374#endif
6375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006376int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006377{
Paul Bakker23986e52011-04-24 08:57:21 +00006378 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006379 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006380 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006383
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006384 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006385
Hanno Becker327c93b2018-08-15 13:56:18 +01006386 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006388 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006389 return( ret );
6390 }
6391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006392 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006395 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6396 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006398 }
6399
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006400 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006401#if defined(MBEDTLS_SSL_PROTO_SSL3)
6402 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006403 hash_len = 36;
6404 else
6405#endif
6406 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006408 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6409 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006412 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6413 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006415 }
6416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006417 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006418 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006421 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6422 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006423 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006424 }
6425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006427 ssl->verify_data_len = hash_len;
6428 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006429#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006430
Paul Bakker0a597072012-09-25 21:55:46 +00006431 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006433#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006434 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006435 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006436#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006437#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006438 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006439 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006440#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006441 }
6442 else
6443 ssl->state++;
6444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006446 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006448#endif
6449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006451
6452 return( 0 );
6453}
6454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006456{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006459#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6460 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6461 mbedtls_md5_init( &handshake->fin_md5 );
6462 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006463 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6464 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006465#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6467#if defined(MBEDTLS_SHA256_C)
6468 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006469 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006470#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471#if defined(MBEDTLS_SHA512_C)
6472 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006473 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006474#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006476
6477 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006478
6479#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6480 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6481 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6482#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484#if defined(MBEDTLS_DHM_C)
6485 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006486#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487#if defined(MBEDTLS_ECDH_C)
6488 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006489#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006490#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006491 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006492#if defined(MBEDTLS_SSL_CLI_C)
6493 handshake->ecjpake_cache = NULL;
6494 handshake->ecjpake_cache_len = 0;
6495#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006496#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006497
6498#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6499 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6500#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006501}
6502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006504{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006505 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6508 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006510 mbedtls_md_init( &transform->md_ctx_enc );
6511 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006512}
6513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006514void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006515{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006516 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006517}
6518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006520{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006521 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006522 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006524 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006526 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006527 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006528
6529 /*
6530 * Either the pointers are now NULL or cleared properly and can be freed.
6531 * Now allocate missing structures.
6532 */
6533 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006534 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006535 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006536 }
Paul Bakker48916f92012-09-16 19:57:18 +00006537
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006538 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006539 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006540 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006541 }
Paul Bakker48916f92012-09-16 19:57:18 +00006542
Paul Bakker82788fb2014-10-20 13:59:19 +02006543 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006544 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006545 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006546 }
Paul Bakker48916f92012-09-16 19:57:18 +00006547
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006548 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006549 if( ssl->handshake == NULL ||
6550 ssl->transform_negotiate == NULL ||
6551 ssl->session_negotiate == NULL )
6552 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555 mbedtls_free( ssl->handshake );
6556 mbedtls_free( ssl->transform_negotiate );
6557 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006558
6559 ssl->handshake = NULL;
6560 ssl->transform_negotiate = NULL;
6561 ssl->session_negotiate = NULL;
6562
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006563 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006564 }
6565
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006566 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006568 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006569 ssl_handshake_params_init( ssl->handshake );
6570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006571#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006572 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6573 {
6574 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006575
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006576 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6577 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6578 else
6579 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006580
6581 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006582 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006583#endif
6584
Paul Bakker48916f92012-09-16 19:57:18 +00006585 return( 0 );
6586}
6587
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006588#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006589/* Dummy cookie callbacks for defaults */
6590static int ssl_cookie_write_dummy( void *ctx,
6591 unsigned char **p, unsigned char *end,
6592 const unsigned char *cli_id, size_t cli_id_len )
6593{
6594 ((void) ctx);
6595 ((void) p);
6596 ((void) end);
6597 ((void) cli_id);
6598 ((void) cli_id_len);
6599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006600 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006601}
6602
6603static int ssl_cookie_check_dummy( void *ctx,
6604 const unsigned char *cookie, size_t cookie_len,
6605 const unsigned char *cli_id, size_t cli_id_len )
6606{
6607 ((void) ctx);
6608 ((void) cookie);
6609 ((void) cookie_len);
6610 ((void) cli_id);
6611 ((void) cli_id_len);
6612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006614}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006615#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006616
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006617/* Once ssl->out_hdr as the address of the beginning of the
6618 * next outgoing record is set, deduce the other pointers.
6619 *
6620 * Note: For TLS, we save the implicit record sequence number
6621 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6622 * and the caller has to make sure there's space for this.
6623 */
6624
6625static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6626 mbedtls_ssl_transform *transform )
6627{
6628#if defined(MBEDTLS_SSL_PROTO_DTLS)
6629 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6630 {
6631 ssl->out_ctr = ssl->out_hdr + 3;
6632 ssl->out_len = ssl->out_hdr + 11;
6633 ssl->out_iv = ssl->out_hdr + 13;
6634 }
6635 else
6636#endif
6637 {
6638 ssl->out_ctr = ssl->out_hdr - 8;
6639 ssl->out_len = ssl->out_hdr + 3;
6640 ssl->out_iv = ssl->out_hdr + 5;
6641 }
6642
6643 /* Adjust out_msg to make space for explicit IV, if used. */
6644 if( transform != NULL &&
6645 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6646 {
6647 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6648 }
6649 else
6650 ssl->out_msg = ssl->out_iv;
6651}
6652
6653/* Once ssl->in_hdr as the address of the beginning of the
6654 * next incoming record is set, deduce the other pointers.
6655 *
6656 * Note: For TLS, we save the implicit record sequence number
6657 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6658 * and the caller has to make sure there's space for this.
6659 */
6660
6661static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6662 mbedtls_ssl_transform *transform )
6663{
6664#if defined(MBEDTLS_SSL_PROTO_DTLS)
6665 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6666 {
6667 ssl->in_ctr = ssl->in_hdr + 3;
6668 ssl->in_len = ssl->in_hdr + 11;
6669 ssl->in_iv = ssl->in_hdr + 13;
6670 }
6671 else
6672#endif
6673 {
6674 ssl->in_ctr = ssl->in_hdr - 8;
6675 ssl->in_len = ssl->in_hdr + 3;
6676 ssl->in_iv = ssl->in_hdr + 5;
6677 }
6678
6679 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6680 if( transform != NULL &&
6681 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6682 {
6683 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6684 }
6685 else
6686 ssl->in_msg = ssl->in_iv;
6687}
6688
Paul Bakker5121ce52009-01-03 21:22:43 +00006689/*
6690 * Initialize an SSL context
6691 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006692void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6693{
6694 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6695}
6696
6697/*
6698 * Setup an SSL context
6699 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006700
6701static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6702{
6703 /* Set the incoming and outgoing record pointers. */
6704#if defined(MBEDTLS_SSL_PROTO_DTLS)
6705 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6706 {
6707 ssl->out_hdr = ssl->out_buf;
6708 ssl->in_hdr = ssl->in_buf;
6709 }
6710 else
6711#endif /* MBEDTLS_SSL_PROTO_DTLS */
6712 {
6713 ssl->out_hdr = ssl->out_buf + 8;
6714 ssl->in_hdr = ssl->in_buf + 8;
6715 }
6716
6717 /* Derive other internal pointers. */
6718 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6719 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6720}
6721
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006722int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006723 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006724{
Paul Bakker48916f92012-09-16 19:57:18 +00006725 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006726
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006727 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006728
6729 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006730 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006731 */
Angus Grattond8213d02016-05-25 20:56:48 +10006732 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6733 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006734 {
Angus Grattond8213d02016-05-25 20:56:48 +10006735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6736 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6737 }
6738
6739 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6740 if( ssl->out_buf == NULL )
6741 {
6742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006744 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006745 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006746 }
6747
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006748 ssl_reset_in_out_pointers( ssl );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006749
Paul Bakker48916f92012-09-16 19:57:18 +00006750 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6751 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006752
6753 return( 0 );
6754}
6755
6756/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006757 * Reset an initialized and used SSL context for re-use while retaining
6758 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006759 *
6760 * If partial is non-zero, keep data in the input buffer and client ID.
6761 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006762 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006763static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006764{
Paul Bakker48916f92012-09-16 19:57:18 +00006765 int ret;
6766
Hanno Becker7e772132018-08-10 12:38:21 +01006767#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6768 !defined(MBEDTLS_SSL_SRV_C)
6769 ((void) partial);
6770#endif
6771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006773
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006774 /* Cancel any possibly running timer */
6775 ssl_set_timer( ssl, 0 );
6776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777#if defined(MBEDTLS_SSL_RENEGOTIATION)
6778 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006779 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006780
6781 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6783 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006784#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006786
Paul Bakker7eb013f2011-10-06 12:37:39 +00006787 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006788 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006789
6790 ssl->in_msgtype = 0;
6791 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006793 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006794 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006795#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006797 ssl_dtls_replay_reset( ssl );
6798#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006799
6800 ssl->in_hslen = 0;
6801 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006802
6803 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006804
6805 ssl->out_msgtype = 0;
6806 ssl->out_msglen = 0;
6807 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6809 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006810 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006811#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006812
Hanno Becker19859472018-08-06 09:40:20 +01006813 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6814
Paul Bakker48916f92012-09-16 19:57:18 +00006815 ssl->transform_in = NULL;
6816 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006817
Hanno Becker78640902018-08-13 16:35:15 +01006818 ssl->session_in = NULL;
6819 ssl->session_out = NULL;
6820
Angus Grattond8213d02016-05-25 20:56:48 +10006821 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006822
6823#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006824 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006825#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6826 {
6827 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006828 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006829 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6832 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6835 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6838 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006839 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006840 }
6841#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006842
Paul Bakker48916f92012-09-16 19:57:18 +00006843 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845 mbedtls_ssl_transform_free( ssl->transform );
6846 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006847 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006848 }
Paul Bakker48916f92012-09-16 19:57:18 +00006849
Paul Bakkerc0463502013-02-14 11:19:38 +01006850 if( ssl->session )
6851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 mbedtls_ssl_session_free( ssl->session );
6853 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006854 ssl->session = NULL;
6855 }
6856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006858 ssl->alpn_chosen = NULL;
6859#endif
6860
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006861#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006862#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006863 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006864#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006865 {
6866 mbedtls_free( ssl->cli_id );
6867 ssl->cli_id = NULL;
6868 ssl->cli_id_len = 0;
6869 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006870#endif
6871
Paul Bakker48916f92012-09-16 19:57:18 +00006872 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6873 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006874
6875 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006876}
6877
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006878/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006879 * Reset an initialized and used SSL context for re-use while retaining
6880 * all application-set variables, function pointers and data.
6881 */
6882int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6883{
6884 return( ssl_session_reset_int( ssl, 0 ) );
6885}
6886
6887/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006888 * SSL set accessors
6889 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006890void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006891{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006892 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006893}
6894
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006895void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006896{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006897 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006898}
6899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006900#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006901void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006902{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006903 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006904}
6905#endif
6906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006908void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006909{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006910 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006911}
6912#endif
6913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01006915
6916void mbedtls_ssl_conf_datagram_packing( mbedtls_ssl_context *ssl,
6917 unsigned allow_packing )
6918{
6919 ssl->disable_datagram_packing = !allow_packing;
6920}
6921
6922void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
6923 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006924{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006925 conf->hs_timeout_min = min;
6926 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006927}
6928#endif
6929
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006930void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006931{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006932 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006933}
6934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006936void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006937 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006938 void *p_vrfy )
6939{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006940 conf->f_vrfy = f_vrfy;
6941 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006942}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006944
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006945void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006946 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006947 void *p_rng )
6948{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006949 conf->f_rng = f_rng;
6950 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006951}
6952
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006953void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006954 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006955 void *p_dbg )
6956{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006957 conf->f_dbg = f_dbg;
6958 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006959}
6960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006962 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006963 mbedtls_ssl_send_t *f_send,
6964 mbedtls_ssl_recv_t *f_recv,
6965 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006966{
6967 ssl->p_bio = p_bio;
6968 ssl->f_send = f_send;
6969 ssl->f_recv = f_recv;
6970 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006971}
6972
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006973void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006974{
6975 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006976}
6977
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006978void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6979 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006980 mbedtls_ssl_set_timer_t *f_set_timer,
6981 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006982{
6983 ssl->p_timer = p_timer;
6984 ssl->f_set_timer = f_set_timer;
6985 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006986
6987 /* Make sure we start with no timer running */
6988 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006989}
6990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006992void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006993 void *p_cache,
6994 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6995 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006996{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006997 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006998 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006999 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007000}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003#if defined(MBEDTLS_SSL_CLI_C)
7004int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007005{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007006 int ret;
7007
7008 if( ssl == NULL ||
7009 session == NULL ||
7010 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007011 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007014 }
7015
7016 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7017 return( ret );
7018
Paul Bakker0a597072012-09-25 21:55:46 +00007019 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007020
7021 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007024
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007025void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007026 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007027{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007028 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7029 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7030 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7031 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007032}
7033
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007034void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007035 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007036 int major, int minor )
7037{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007039 return;
7040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007042 return;
7043
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007044 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007045}
7046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007048void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007049 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007050{
7051 conf->cert_profile = profile;
7052}
7053
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007054/* Append a new keycert entry to a (possibly empty) list */
7055static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7056 mbedtls_x509_crt *cert,
7057 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007058{
niisato8ee24222018-06-25 19:05:48 +09007059 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007060
niisato8ee24222018-06-25 19:05:48 +09007061 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7062 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007063 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007064
niisato8ee24222018-06-25 19:05:48 +09007065 new_cert->cert = cert;
7066 new_cert->key = key;
7067 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007068
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007069 /* Update head is the list was null, else add to the end */
7070 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007071 {
niisato8ee24222018-06-25 19:05:48 +09007072 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007073 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007074 else
7075 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007076 mbedtls_ssl_key_cert *cur = *head;
7077 while( cur->next != NULL )
7078 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007079 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007080 }
7081
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007082 return( 0 );
7083}
7084
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007085int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007086 mbedtls_x509_crt *own_cert,
7087 mbedtls_pk_context *pk_key )
7088{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007089 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007090}
7091
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007092void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007093 mbedtls_x509_crt *ca_chain,
7094 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007095{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007096 conf->ca_chain = ca_chain;
7097 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007100
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007101#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7102int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7103 mbedtls_x509_crt *own_cert,
7104 mbedtls_pk_context *pk_key )
7105{
7106 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7107 own_cert, pk_key ) );
7108}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007109
7110void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7111 mbedtls_x509_crt *ca_chain,
7112 mbedtls_x509_crl *ca_crl )
7113{
7114 ssl->handshake->sni_ca_chain = ca_chain;
7115 ssl->handshake->sni_ca_crl = ca_crl;
7116}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007117
7118void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7119 int authmode )
7120{
7121 ssl->handshake->sni_authmode = authmode;
7122}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007123#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7124
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007125#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007126/*
7127 * Set EC J-PAKE password for current handshake
7128 */
7129int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7130 const unsigned char *pw,
7131 size_t pw_len )
7132{
7133 mbedtls_ecjpake_role role;
7134
Janos Follath8eb64132016-06-03 15:40:57 +01007135 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7137
7138 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7139 role = MBEDTLS_ECJPAKE_SERVER;
7140 else
7141 role = MBEDTLS_ECJPAKE_CLIENT;
7142
7143 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7144 role,
7145 MBEDTLS_MD_SHA256,
7146 MBEDTLS_ECP_DP_SECP256R1,
7147 pw, pw_len ) );
7148}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007149#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007152int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007153 const unsigned char *psk, size_t psk_len,
7154 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007155{
Paul Bakker6db455e2013-09-18 17:29:31 +02007156 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007161
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007162 /* Identity len will be encoded on two bytes */
7163 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007164 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007165 {
7166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7167 }
7168
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007169 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007170 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007171 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007172
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007173 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007174 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007175 conf->psk_len = 0;
7176 }
7177 if( conf->psk_identity != NULL )
7178 {
7179 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007180 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007181 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007182 }
7183
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007184 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7185 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007186 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007187 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007188 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007189 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007190 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007191 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007192 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007193
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007194 conf->psk_len = psk_len;
7195 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007196
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007197 memcpy( conf->psk, psk, conf->psk_len );
7198 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007199
7200 return( 0 );
7201}
7202
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007203int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7204 const unsigned char *psk, size_t psk_len )
7205{
7206 if( psk == NULL || ssl->handshake == NULL )
7207 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7208
7209 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7210 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7211
7212 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007213 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007214 mbedtls_platform_zeroize( ssl->handshake->psk,
7215 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007216 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007217 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007218 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007219
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007220 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007221 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007222
7223 ssl->handshake->psk_len = psk_len;
7224 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7225
7226 return( 0 );
7227}
7228
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007229void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007231 size_t),
7232 void *p_psk )
7233{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007234 conf->f_psk = f_psk;
7235 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007236}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007237#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007238
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007239#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007240
7241#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007242int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007243{
7244 int ret;
7245
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007246 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7247 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7248 {
7249 mbedtls_mpi_free( &conf->dhm_P );
7250 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007251 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007252 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007253
7254 return( 0 );
7255}
Hanno Becker470a8c42017-10-04 15:28:46 +01007256#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007257
Hanno Beckera90658f2017-10-04 15:29:08 +01007258int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7259 const unsigned char *dhm_P, size_t P_len,
7260 const unsigned char *dhm_G, size_t G_len )
7261{
7262 int ret;
7263
7264 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7265 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7266 {
7267 mbedtls_mpi_free( &conf->dhm_P );
7268 mbedtls_mpi_free( &conf->dhm_G );
7269 return( ret );
7270 }
7271
7272 return( 0 );
7273}
Paul Bakker5121ce52009-01-03 21:22:43 +00007274
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007275int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007276{
7277 int ret;
7278
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007279 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7280 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7281 {
7282 mbedtls_mpi_free( &conf->dhm_P );
7283 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007284 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007285 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007286
7287 return( 0 );
7288}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007289#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007290
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007291#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7292/*
7293 * Set the minimum length for Diffie-Hellman parameters
7294 */
7295void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7296 unsigned int bitlen )
7297{
7298 conf->dhm_min_bitlen = bitlen;
7299}
7300#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7301
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007302#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007303/*
7304 * Set allowed/preferred hashes for handshake signatures
7305 */
7306void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7307 const int *hashes )
7308{
7309 conf->sig_hashes = hashes;
7310}
Hanno Becker947194e2017-04-07 13:25:49 +01007311#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007312
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007313#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007314/*
7315 * Set the allowed elliptic curves
7316 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007317void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007318 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007319{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007320 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007321}
Hanno Becker947194e2017-04-07 13:25:49 +01007322#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007323
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007324#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007326{
Hanno Becker947194e2017-04-07 13:25:49 +01007327 /* Initialize to suppress unnecessary compiler warning */
7328 size_t hostname_len = 0;
7329
7330 /* Check if new hostname is valid before
7331 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007332 if( hostname != NULL )
7333 {
7334 hostname_len = strlen( hostname );
7335
7336 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7337 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7338 }
7339
7340 /* Now it's clear that we will overwrite the old hostname,
7341 * so we can free it safely */
7342
7343 if( ssl->hostname != NULL )
7344 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007345 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007346 mbedtls_free( ssl->hostname );
7347 }
7348
7349 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007350
Paul Bakker5121ce52009-01-03 21:22:43 +00007351 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007352 {
7353 ssl->hostname = NULL;
7354 }
7355 else
7356 {
7357 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007358 if( ssl->hostname == NULL )
7359 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007360
Hanno Becker947194e2017-04-07 13:25:49 +01007361 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007362
Hanno Becker947194e2017-04-07 13:25:49 +01007363 ssl->hostname[hostname_len] = '\0';
7364 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007365
7366 return( 0 );
7367}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007368#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007369
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007370#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007371void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007373 const unsigned char *, size_t),
7374 void *p_sni )
7375{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007376 conf->f_sni = f_sni;
7377 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007378}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007382int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007383{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007384 size_t cur_len, tot_len;
7385 const char **p;
7386
7387 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007388 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7389 * MUST NOT be truncated."
7390 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007391 */
7392 tot_len = 0;
7393 for( p = protos; *p != NULL; p++ )
7394 {
7395 cur_len = strlen( *p );
7396 tot_len += cur_len;
7397
7398 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007400 }
7401
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007402 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007403
7404 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007405}
7406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007408{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007409 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007410}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007412
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007413void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007414{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007415 conf->max_major_ver = major;
7416 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007417}
7418
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007419void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007420{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007421 conf->min_major_ver = major;
7422 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007423}
7424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007425#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007426void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007427{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007428 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007429}
7430#endif
7431
Janos Follath088ce432017-04-10 12:42:31 +01007432#if defined(MBEDTLS_SSL_SRV_C)
7433void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7434 char cert_req_ca_list )
7435{
7436 conf->cert_req_ca_list = cert_req_ca_list;
7437}
7438#endif
7439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007441void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007442{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007443 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007444}
7445#endif
7446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007448void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007449{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007450 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007451}
7452#endif
7453
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007454#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007455void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007456{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007457 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007458}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007459#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007460
Manuel Pégourié-Gonnard0b1d9b22017-09-21 13:15:27 +02007461#if defined(MBEDTLS_SSL_PROTO_DTLS)
7462void mbedtls_ssl_conf_mtu( mbedtls_ssl_config *conf, uint16_t mtu )
7463{
7464 conf->mtu = mtu;
7465}
7466#endif
7467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007469int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007470{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007472 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007475 }
7476
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007477 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007478
7479 return( 0 );
7480}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007484void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007485{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007486 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007490#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007491void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007492{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007493 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007494}
7495#endif
7496
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007497void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007498{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007499 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007500}
7501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007503void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007504{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007505 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007506}
7507
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007508void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007509{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007510 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007511}
7512
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007513void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007514 const unsigned char period[8] )
7515{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007516 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007521#if defined(MBEDTLS_SSL_CLI_C)
7522void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007523{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007524 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007525}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007526#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007527
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007528#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007529void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7530 mbedtls_ssl_ticket_write_t *f_ticket_write,
7531 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7532 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007533{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007534 conf->f_ticket_write = f_ticket_write;
7535 conf->f_ticket_parse = f_ticket_parse;
7536 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007537}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007538#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007540
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007541#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7542void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7543 mbedtls_ssl_export_keys_t *f_export_keys,
7544 void *p_export_keys )
7545{
7546 conf->f_export_keys = f_export_keys;
7547 conf->p_export_keys = p_export_keys;
7548}
7549#endif
7550
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007551#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007552void mbedtls_ssl_conf_async_private_cb(
7553 mbedtls_ssl_config *conf,
7554 mbedtls_ssl_async_sign_t *f_async_sign,
7555 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7556 mbedtls_ssl_async_resume_t *f_async_resume,
7557 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007558 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007559{
7560 conf->f_async_sign_start = f_async_sign;
7561 conf->f_async_decrypt_start = f_async_decrypt;
7562 conf->f_async_resume = f_async_resume;
7563 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007564 conf->p_async_config_data = async_config_data;
7565}
7566
Gilles Peskine8f97af72018-04-26 11:46:10 +02007567void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7568{
7569 return( conf->p_async_config_data );
7570}
7571
Gilles Peskine1febfef2018-04-30 11:54:39 +02007572void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007573{
7574 if( ssl->handshake == NULL )
7575 return( NULL );
7576 else
7577 return( ssl->handshake->user_async_ctx );
7578}
7579
Gilles Peskine1febfef2018-04-30 11:54:39 +02007580void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007581 void *ctx )
7582{
7583 if( ssl->handshake != NULL )
7584 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007585}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007586#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007587
Paul Bakker5121ce52009-01-03 21:22:43 +00007588/*
7589 * SSL get accessors
7590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007592{
7593 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7594}
7595
Hanno Becker8b170a02017-10-10 11:51:19 +01007596int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7597{
7598 /*
7599 * Case A: We're currently holding back
7600 * a message for further processing.
7601 */
7602
7603 if( ssl->keep_current_message == 1 )
7604 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007605 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007606 return( 1 );
7607 }
7608
7609 /*
7610 * Case B: Further records are pending in the current datagram.
7611 */
7612
7613#if defined(MBEDTLS_SSL_PROTO_DTLS)
7614 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7615 ssl->in_left > ssl->next_record_offset )
7616 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007618 return( 1 );
7619 }
7620#endif /* MBEDTLS_SSL_PROTO_DTLS */
7621
7622 /*
7623 * Case C: A handshake message is being processed.
7624 */
7625
Hanno Becker8b170a02017-10-10 11:51:19 +01007626 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7627 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007628 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007629 return( 1 );
7630 }
7631
7632 /*
7633 * Case D: An application data message is being processed
7634 */
7635 if( ssl->in_offt != NULL )
7636 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007638 return( 1 );
7639 }
7640
7641 /*
7642 * In all other cases, the rest of the message can be dropped.
7643 * As in ssl_read_record_layer, this needs to be adapted if
7644 * we implement support for multiple alerts in single records.
7645 */
7646
7647 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7648 return( 0 );
7649}
7650
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007651uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007652{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007653 if( ssl->session != NULL )
7654 return( ssl->session->verify_result );
7655
7656 if( ssl->session_negotiate != NULL )
7657 return( ssl->session_negotiate->verify_result );
7658
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007659 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007660}
7661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007663{
Paul Bakker926c8e42013-03-06 10:23:34 +01007664 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007665 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007667 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007668}
7669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007670const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007671{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007673 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007674 {
7675 switch( ssl->minor_ver )
7676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007678 return( "DTLSv1.0" );
7679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007680 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007681 return( "DTLSv1.2" );
7682
7683 default:
7684 return( "unknown (DTLS)" );
7685 }
7686 }
7687#endif
7688
Paul Bakker43ca69c2011-01-15 17:35:19 +00007689 switch( ssl->minor_ver )
7690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007692 return( "SSLv3.0" );
7693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007695 return( "TLSv1.0" );
7696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007697 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007698 return( "TLSv1.1" );
7699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007701 return( "TLSv1.2" );
7702
Paul Bakker43ca69c2011-01-15 17:35:19 +00007703 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007704 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007705 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007706}
7707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007709{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007710 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007712 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007713
Hanno Becker78640902018-08-13 16:35:15 +01007714 if( transform == NULL )
7715 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717#if defined(MBEDTLS_ZLIB_SUPPORT)
7718 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7719 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007720 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007721#endif
7722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725 case MBEDTLS_MODE_GCM:
7726 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007727 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007729 transform_expansion = transform->minlen;
7730 break;
7731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007732 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007733
7734 block_size = mbedtls_cipher_get_block_size(
7735 &transform->cipher_ctx_enc );
7736
7737#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7738 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7739 {
7740 /* Expansion due to addition of
7741 * - MAC
7742 * - CBC padding (theoretically up to 256 bytes, but
7743 * we never use more than block_size)
7744 * - explicit IV
7745 */
7746 transform_expansion = transform->maclen + 2 * block_size;
7747 }
7748 else
7749#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7750 {
7751 /* No explicit IV prior to TLS 1.1. */
7752 transform_expansion = transform->maclen + block_size;
7753 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007754 break;
7755
7756 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007759 }
7760
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007761 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007762}
7763
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007764#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7765size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7766{
7767 size_t max_len;
7768
7769 /*
7770 * Assume mfl_code is correct since it was checked when set
7771 */
Angus Grattond8213d02016-05-25 20:56:48 +10007772 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007773
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007774 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007775 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007776 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007777 {
Angus Grattond8213d02016-05-25 20:56:48 +10007778 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007779 }
7780
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007781 /* During a handshake, use the value being negotiated */
7782 if( ssl->session_negotiate != NULL &&
7783 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7784 {
7785 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7786 }
7787
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007788 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007789}
7790#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7791
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007792int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7793{
7794 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7795
7796#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7797 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7798
7799 if( max_len > mfl )
7800 max_len = mfl;
7801#endif
7802
7803#if defined(MBEDTLS_SSL_PROTO_DTLS)
7804 if( ssl->conf->mtu != 0 )
7805 {
7806 const size_t mtu = ssl->conf->mtu;
7807 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7808 const size_t overhead = (size_t) ret;
7809
7810 if( ret < 0 )
7811 return( ret );
7812
7813 if( mtu <= overhead )
7814 {
7815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7816 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7817 }
7818
7819 if( max_len > mtu - overhead )
7820 max_len = mtu - overhead;
7821 }
7822#endif
7823
Hanno Becker0defedb2018-08-10 12:35:02 +01007824#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7825 !defined(MBEDTLS_SSL_PROTO_DTLS)
7826 ((void) ssl);
7827#endif
7828
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007829 return( (int) max_len );
7830}
7831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832#if defined(MBEDTLS_X509_CRT_PARSE_C)
7833const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007834{
7835 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007836 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007837
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007838 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007839}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842#if defined(MBEDTLS_SSL_CLI_C)
7843int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007844{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007845 if( ssl == NULL ||
7846 dst == NULL ||
7847 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007848 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007851 }
7852
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007853 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007854}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007856
Paul Bakker5121ce52009-01-03 21:22:43 +00007857/*
Paul Bakker1961b702013-01-25 14:49:24 +01007858 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007861{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007862 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007863
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007864 if( ssl == NULL || ssl->conf == NULL )
7865 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007868 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007870#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007872 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007874#endif
7875
Paul Bakker1961b702013-01-25 14:49:24 +01007876 return( ret );
7877}
7878
7879/*
7880 * Perform the SSL handshake
7881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007883{
7884 int ret = 0;
7885
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007886 if( ssl == NULL || ssl->conf == NULL )
7887 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007894
7895 if( ret != 0 )
7896 break;
7897 }
7898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007900
7901 return( ret );
7902}
7903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904#if defined(MBEDTLS_SSL_RENEGOTIATION)
7905#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007906/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007907 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007908 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007909static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007910{
7911 int ret;
7912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007914
7915 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7917 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007918
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007919 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007920 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007922 return( ret );
7923 }
7924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007926
7927 return( 0 );
7928}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007930
7931/*
7932 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007933 * - any side: calling mbedtls_ssl_renegotiate(),
7934 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7935 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007936 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007937 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007939 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007941{
7942 int ret;
7943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007945
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007946 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7947 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007948
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007949 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7950 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007952 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007954 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007955 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007956 ssl->handshake->out_msg_seq = 1;
7957 else
7958 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007959 }
7960#endif
7961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7963 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007968 return( ret );
7969 }
7970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007972
7973 return( 0 );
7974}
7975
7976/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007977 * Renegotiate current connection on client,
7978 * or request renegotiation on server
7979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007981{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007983
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007984 if( ssl == NULL || ssl->conf == NULL )
7985 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007988 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007989 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007991 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7992 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007995
7996 /* Did we already try/start sending HelloRequest? */
7997 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007999
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008000 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008001 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008004#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008005 /*
8006 * On client, either start the renegotiation process or,
8007 * if already in progress, continue the handshake
8008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8012 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008013
8014 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008017 return( ret );
8018 }
8019 }
8020 else
8021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008025 return( ret );
8026 }
8027 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008028#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008029
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008030 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008031}
8032
8033/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008034 * Check record counters and renegotiate if they're above the limit.
8035 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008036static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008037{
Andres AG2196c7f2016-12-15 17:01:16 +00008038 size_t ep_len = ssl_ep_len( ssl );
8039 int in_ctr_cmp;
8040 int out_ctr_cmp;
8041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008042 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8043 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008044 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008045 {
8046 return( 0 );
8047 }
8048
Andres AG2196c7f2016-12-15 17:01:16 +00008049 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8050 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008051 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008052 ssl->conf->renego_period + ep_len, 8 - ep_len );
8053
8054 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008055 {
8056 return( 0 );
8057 }
8058
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008061}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008062#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008063
8064/*
8065 * Receive application data decrypted from the SSL layer
8066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008068{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008069 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008070 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008071
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008072 if( ssl == NULL || ssl->conf == NULL )
8073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008077#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008078 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008081 return( ret );
8082
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008083 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008084 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008085 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008086 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008087 return( ret );
8088 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008089 }
8090#endif
8091
Hanno Becker4a810fb2017-05-24 16:27:30 +01008092 /*
8093 * Check if renegotiation is necessary and/or handshake is
8094 * in process. If yes, perform/continue, and fall through
8095 * if an unexpected packet is received while the client
8096 * is waiting for the ServerHello.
8097 *
8098 * (There is no equivalent to the last condition on
8099 * the server-side as it is not treated as within
8100 * a handshake while waiting for the ClientHello
8101 * after a renegotiation request.)
8102 */
8103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008104#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008105 ret = ssl_check_ctr_renegotiate( ssl );
8106 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8107 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008109 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008110 return( ret );
8111 }
8112#endif
8113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008116 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008117 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8118 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008121 return( ret );
8122 }
8123 }
8124
Hanno Beckere41158b2017-10-23 13:30:32 +01008125 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008126 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008127 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008128 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008129 if( ssl->f_get_timer != NULL &&
8130 ssl->f_get_timer( ssl->p_timer ) == -1 )
8131 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008132 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008133 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008134
Hanno Becker327c93b2018-08-15 13:56:18 +01008135 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008136 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008137 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8138 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008139
Hanno Becker4a810fb2017-05-24 16:27:30 +01008140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8141 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008142 }
8143
8144 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008146 {
8147 /*
8148 * OpenSSL sends empty messages to randomize the IV
8149 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008150 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008153 return( 0 );
8154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008156 return( ret );
8157 }
8158 }
8159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008160 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008163
Hanno Becker4a810fb2017-05-24 16:27:30 +01008164 /*
8165 * - For client-side, expect SERVER_HELLO_REQUEST.
8166 * - For server-side, expect CLIENT_HELLO.
8167 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8168 */
8169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008170#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008171 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008173 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008176
8177 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008178#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008179 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008180 {
8181 continue;
8182 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008183#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008185 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008186#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008187
Hanno Becker4a810fb2017-05-24 16:27:30 +01008188#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008189 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008193
8194 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008195#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008196 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008197 {
8198 continue;
8199 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008200#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008202 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008203#endif /* MBEDTLS_SSL_SRV_C */
8204
Hanno Becker21df7f92017-10-17 11:03:26 +01008205#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008206 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008207 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8208 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8209 ssl->conf->allow_legacy_renegotiation ==
8210 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8211 {
8212 /*
8213 * Accept renegotiation request
8214 */
Paul Bakker48916f92012-09-16 19:57:18 +00008215
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008216 /* DTLS clients need to know renego is server-initiated */
8217#if defined(MBEDTLS_SSL_PROTO_DTLS)
8218 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8219 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8220 {
8221 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8222 }
8223#endif
8224 ret = ssl_start_renegotiation( ssl );
8225 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8226 ret != 0 )
8227 {
8228 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8229 return( ret );
8230 }
8231 }
8232 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008233#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008234 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008235 /*
8236 * Refuse renegotiation
8237 */
8238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008241#if defined(MBEDTLS_SSL_PROTO_SSL3)
8242 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008243 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008244 /* SSLv3 does not have a "no_renegotiation" warning, so
8245 we send a fatal alert and abort the connection. */
8246 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8247 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8248 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008249 }
8250 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008251#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8252#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8253 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8254 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008256 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8257 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8258 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008259 {
8260 return( ret );
8261 }
Paul Bakker48916f92012-09-16 19:57:18 +00008262 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008263 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008264#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8265 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8268 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008269 }
Paul Bakker48916f92012-09-16 19:57:18 +00008270 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008271
Hanno Becker90333da2017-10-10 11:27:13 +01008272 /* At this point, we don't know whether the renegotiation has been
8273 * completed or not. The cases to consider are the following:
8274 * 1) The renegotiation is complete. In this case, no new record
8275 * has been read yet.
8276 * 2) The renegotiation is incomplete because the client received
8277 * an application data record while awaiting the ServerHello.
8278 * 3) The renegotiation is incomplete because the client received
8279 * a non-handshake, non-application data message while awaiting
8280 * the ServerHello.
8281 * In each of these case, looping will be the proper action:
8282 * - For 1), the next iteration will read a new record and check
8283 * if it's application data.
8284 * - For 2), the loop condition isn't satisfied as application data
8285 * is present, hence continue is the same as break
8286 * - For 3), the loop condition is satisfied and read_record
8287 * will re-deliver the message that was held back by the client
8288 * when expecting the ServerHello.
8289 */
8290 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008291 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008292#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008294 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008295 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008296 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008297 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008300 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008301 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008302 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008303 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008304 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8308 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008311 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008312 }
8313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008314 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8317 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008318 }
8319
8320 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008321
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008322 /* We're going to return something now, cancel timer,
8323 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008325 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008326
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008327#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008328 /* If we requested renego but received AppData, resend HelloRequest.
8329 * Do it now, after setting in_offt, to avoid taking this branch
8330 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008332 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008334 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008335 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008338 return( ret );
8339 }
8340 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008342#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008343 }
8344
8345 n = ( len < ssl->in_msglen )
8346 ? len : ssl->in_msglen;
8347
8348 memcpy( buf, ssl->in_offt, n );
8349 ssl->in_msglen -= n;
8350
8351 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008352 {
8353 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008354 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008355 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008356 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008357 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008358 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008359 /* more data available */
8360 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008361 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008364
Paul Bakker23986e52011-04-24 08:57:21 +00008365 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008366}
8367
8368/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008369 * Send application data to be encrypted by the SSL layer, taking care of max
8370 * fragment length and buffer size.
8371 *
8372 * According to RFC 5246 Section 6.2.1:
8373 *
8374 * Zero-length fragments of Application data MAY be sent as they are
8375 * potentially useful as a traffic analysis countermeasure.
8376 *
8377 * Therefore, it is possible that the input message length is 0 and the
8378 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008379 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008380static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008381 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008382{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008383 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8384 const size_t max_len = (size_t) ret;
8385
8386 if( ret < 0 )
8387 {
8388 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8389 return( ret );
8390 }
8391
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008392 if( len > max_len )
8393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008394#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008395 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008398 "maximum fragment length: %d > %d",
8399 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008400 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008401 }
8402 else
8403#endif
8404 len = max_len;
8405 }
Paul Bakker887bd502011-06-08 13:10:54 +00008406
Paul Bakker5121ce52009-01-03 21:22:43 +00008407 if( ssl->out_left != 0 )
8408 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008409 /*
8410 * The user has previously tried to send the data and
8411 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8412 * written. In this case, we expect the high-level write function
8413 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008417 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008418 return( ret );
8419 }
8420 }
Paul Bakker887bd502011-06-08 13:10:54 +00008421 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008422 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008423 /*
8424 * The user is trying to send a message the first time, so we need to
8425 * copy the data into the internal buffers and setup the data structure
8426 * to keep track of partial writes
8427 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008428 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008429 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008430 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008431
Hanno Becker67bc7c32018-08-06 11:33:50 +01008432 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008435 return( ret );
8436 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008437 }
8438
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008439 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008440}
8441
8442/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008443 * Write application data, doing 1/n-1 splitting if necessary.
8444 *
8445 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008446 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008447 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008450static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008451 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008452{
8453 int ret;
8454
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008455 if( ssl->conf->cbc_record_splitting ==
8456 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008457 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008458 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8459 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8460 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008461 {
8462 return( ssl_write_real( ssl, buf, len ) );
8463 }
8464
8465 if( ssl->split_done == 0 )
8466 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008467 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008468 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008469 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008470 }
8471
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008472 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8473 return( ret );
8474 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008475
8476 return( ret + 1 );
8477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008478#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008479
8480/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008481 * Write application data (public-facing wrapper)
8482 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008483int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008484{
8485 int ret;
8486
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008488
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008489 if( ssl == NULL || ssl->conf == NULL )
8490 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8491
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008492#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008493 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8494 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008495 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008496 return( ret );
8497 }
8498#endif
8499
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008500 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008501 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008502 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008503 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008505 return( ret );
8506 }
8507 }
8508
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008509#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008510 ret = ssl_write_split( ssl, buf, len );
8511#else
8512 ret = ssl_write_real( ssl, buf, len );
8513#endif
8514
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008516
8517 return( ret );
8518}
8519
8520/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008521 * Notify the peer that the connection is being closed
8522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008523int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008524{
8525 int ret;
8526
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008527 if( ssl == NULL || ssl->conf == NULL )
8528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008531
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008532 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008533 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008535 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008537 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8538 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8539 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008541 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008542 return( ret );
8543 }
8544 }
8545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008547
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008548 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008549}
8550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008551void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008552{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008553 if( transform == NULL )
8554 return;
8555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008557 deflateEnd( &transform->ctx_deflate );
8558 inflateEnd( &transform->ctx_inflate );
8559#endif
8560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8562 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564 mbedtls_md_free( &transform->md_ctx_enc );
8565 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008566
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008567 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008568}
8569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008570#if defined(MBEDTLS_X509_CRT_PARSE_C)
8571static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008572{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008573 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008574
8575 while( cur != NULL )
8576 {
8577 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008579 cur = next;
8580 }
8581}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008583
Hanno Becker0271f962018-08-16 13:23:47 +01008584#if defined(MBEDTLS_SSL_PROTO_DTLS)
8585
8586static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8587{
8588 unsigned offset;
8589 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8590
8591 if( hs == NULL )
8592 return;
8593
8594 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008595 ssl_buffering_free_slot( ssl, offset );
8596}
8597
8598static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8599 uint8_t slot )
8600{
8601 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8602 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
8603 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008604 {
Hanno Beckere605b192018-08-21 15:59:07 +01008605 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
8606 mbedtls_free( hs_buf->data );
8607 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008608 }
8609}
8610
8611#endif /* MBEDTLS_SSL_PROTO_DTLS */
8612
Gilles Peskine9b562d52018-04-25 20:32:43 +02008613void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008614{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008615 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8616
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008617 if( handshake == NULL )
8618 return;
8619
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008620#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8621 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8622 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008623 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008624 handshake->async_in_progress = 0;
8625 }
8626#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8627
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008628#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8629 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8630 mbedtls_md5_free( &handshake->fin_md5 );
8631 mbedtls_sha1_free( &handshake->fin_sha1 );
8632#endif
8633#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8634#if defined(MBEDTLS_SHA256_C)
8635 mbedtls_sha256_free( &handshake->fin_sha256 );
8636#endif
8637#if defined(MBEDTLS_SHA512_C)
8638 mbedtls_sha512_free( &handshake->fin_sha512 );
8639#endif
8640#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008642#if defined(MBEDTLS_DHM_C)
8643 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008644#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645#if defined(MBEDTLS_ECDH_C)
8646 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008647#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008648#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008649 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008650#if defined(MBEDTLS_SSL_CLI_C)
8651 mbedtls_free( handshake->ecjpake_cache );
8652 handshake->ecjpake_cache = NULL;
8653 handshake->ecjpake_cache_len = 0;
8654#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008655#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008656
Janos Follath4ae5c292016-02-10 11:27:43 +00008657#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8658 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008659 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008661#endif
8662
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008663#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8664 if( handshake->psk != NULL )
8665 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008666 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008667 mbedtls_free( handshake->psk );
8668 }
8669#endif
8670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008671#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8672 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008673 /*
8674 * Free only the linked list wrapper, not the keys themselves
8675 * since the belong to the SNI callback
8676 */
8677 if( handshake->sni_key_cert != NULL )
8678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008679 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008680
8681 while( cur != NULL )
8682 {
8683 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008684 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008685 cur = next;
8686 }
8687 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008688#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008690#if defined(MBEDTLS_SSL_PROTO_DTLS)
8691 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008692 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008693 ssl_buffering_free( ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01008694 ssl_free_buffered_record( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008695#endif
8696
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008697 mbedtls_platform_zeroize( handshake,
8698 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008699}
8700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008701void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008702{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008703 if( session == NULL )
8704 return;
8705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008706#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008707 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008709 mbedtls_x509_crt_free( session->peer_cert );
8710 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008711 }
Paul Bakkered27a042013-04-18 22:46:23 +02008712#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008713
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008714#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008715 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008716#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008717
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008718 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008719}
8720
Paul Bakker5121ce52009-01-03 21:22:43 +00008721/*
8722 * Free an SSL context
8723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008724void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008725{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008726 if( ssl == NULL )
8727 return;
8728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008730
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008731 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008732 {
Angus Grattond8213d02016-05-25 20:56:48 +10008733 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008734 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008735 }
8736
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008737 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008738 {
Angus Grattond8213d02016-05-25 20:56:48 +10008739 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008740 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008741 }
8742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008743#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008744 if( ssl->compress_buf != NULL )
8745 {
Angus Grattond8213d02016-05-25 20:56:48 +10008746 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008747 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008748 }
8749#endif
8750
Paul Bakker48916f92012-09-16 19:57:18 +00008751 if( ssl->transform )
8752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008753 mbedtls_ssl_transform_free( ssl->transform );
8754 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008755 }
8756
8757 if( ssl->handshake )
8758 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008759 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008760 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8761 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763 mbedtls_free( ssl->handshake );
8764 mbedtls_free( ssl->transform_negotiate );
8765 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008766 }
8767
Paul Bakkerc0463502013-02-14 11:19:38 +01008768 if( ssl->session )
8769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770 mbedtls_ssl_session_free( ssl->session );
8771 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008772 }
8773
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008774#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008775 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008776 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008777 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008779 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008780#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008782#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8783 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8786 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008787 }
8788#endif
8789
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008790#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008791 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008792#endif
8793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008795
Paul Bakker86f04f42013-02-14 11:20:09 +01008796 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008797 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008798}
8799
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008800/*
8801 * Initialze mbedtls_ssl_config
8802 */
8803void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8804{
8805 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8806}
8807
Simon Butcherc97b6972015-12-27 23:48:17 +00008808#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008809static int ssl_preset_default_hashes[] = {
8810#if defined(MBEDTLS_SHA512_C)
8811 MBEDTLS_MD_SHA512,
8812 MBEDTLS_MD_SHA384,
8813#endif
8814#if defined(MBEDTLS_SHA256_C)
8815 MBEDTLS_MD_SHA256,
8816 MBEDTLS_MD_SHA224,
8817#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008818#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008819 MBEDTLS_MD_SHA1,
8820#endif
8821 MBEDTLS_MD_NONE
8822};
Simon Butcherc97b6972015-12-27 23:48:17 +00008823#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008824
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008825static int ssl_preset_suiteb_ciphersuites[] = {
8826 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8827 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8828 0
8829};
8830
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008831#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008832static int ssl_preset_suiteb_hashes[] = {
8833 MBEDTLS_MD_SHA256,
8834 MBEDTLS_MD_SHA384,
8835 MBEDTLS_MD_NONE
8836};
8837#endif
8838
8839#if defined(MBEDTLS_ECP_C)
8840static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8841 MBEDTLS_ECP_DP_SECP256R1,
8842 MBEDTLS_ECP_DP_SECP384R1,
8843 MBEDTLS_ECP_DP_NONE
8844};
8845#endif
8846
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008847/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008848 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008849 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008850int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008851 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008852{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008853#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008854 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008855#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008856
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008857 /* Use the functions here so that they are covered in tests,
8858 * but otherwise access member directly for efficiency */
8859 mbedtls_ssl_conf_endpoint( conf, endpoint );
8860 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008861
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008862 /*
8863 * Things that are common to all presets
8864 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008865#if defined(MBEDTLS_SSL_CLI_C)
8866 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8867 {
8868 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8869#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8870 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8871#endif
8872 }
8873#endif
8874
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008875#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008876 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008877#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008878
8879#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8880 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8881#endif
8882
8883#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8884 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8885#endif
8886
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008887#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8888 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8889#endif
8890
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008891#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008892 conf->f_cookie_write = ssl_cookie_write_dummy;
8893 conf->f_cookie_check = ssl_cookie_check_dummy;
8894#endif
8895
8896#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8897 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8898#endif
8899
Janos Follath088ce432017-04-10 12:42:31 +01008900#if defined(MBEDTLS_SSL_SRV_C)
8901 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8902#endif
8903
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008904#if defined(MBEDTLS_SSL_PROTO_DTLS)
8905 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8906 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8907#endif
8908
8909#if defined(MBEDTLS_SSL_RENEGOTIATION)
8910 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008911 memset( conf->renego_period, 0x00, 2 );
8912 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008913#endif
8914
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008915#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8916 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8917 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008918 const unsigned char dhm_p[] =
8919 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8920 const unsigned char dhm_g[] =
8921 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8922
Hanno Beckera90658f2017-10-04 15:29:08 +01008923 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8924 dhm_p, sizeof( dhm_p ),
8925 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008926 {
8927 return( ret );
8928 }
8929 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008930#endif
8931
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008932 /*
8933 * Preset-specific defaults
8934 */
8935 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008936 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008937 /*
8938 * NSA Suite B
8939 */
8940 case MBEDTLS_SSL_PRESET_SUITEB:
8941 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8942 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8943 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8944 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8945
8946 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8947 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8948 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8949 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8950 ssl_preset_suiteb_ciphersuites;
8951
8952#if defined(MBEDTLS_X509_CRT_PARSE_C)
8953 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008954#endif
8955
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008956#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008957 conf->sig_hashes = ssl_preset_suiteb_hashes;
8958#endif
8959
8960#if defined(MBEDTLS_ECP_C)
8961 conf->curve_list = ssl_preset_suiteb_curves;
8962#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008963 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008964
8965 /*
8966 * Default
8967 */
8968 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008969 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8970 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8971 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8972 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8973 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8974 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8975 MBEDTLS_SSL_MIN_MINOR_VERSION :
8976 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008977 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8978 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8979
8980#if defined(MBEDTLS_SSL_PROTO_DTLS)
8981 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8982 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8983#endif
8984
8985 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8986 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8987 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8988 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8989 mbedtls_ssl_list_ciphersuites();
8990
8991#if defined(MBEDTLS_X509_CRT_PARSE_C)
8992 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8993#endif
8994
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008995#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008996 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008997#endif
8998
8999#if defined(MBEDTLS_ECP_C)
9000 conf->curve_list = mbedtls_ecp_grp_id_list();
9001#endif
9002
9003#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9004 conf->dhm_min_bitlen = 1024;
9005#endif
9006 }
9007
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009008 return( 0 );
9009}
9010
9011/*
9012 * Free mbedtls_ssl_config
9013 */
9014void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9015{
9016#if defined(MBEDTLS_DHM_C)
9017 mbedtls_mpi_free( &conf->dhm_P );
9018 mbedtls_mpi_free( &conf->dhm_G );
9019#endif
9020
9021#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9022 if( conf->psk != NULL )
9023 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009024 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009025 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009026 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009027 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009028 }
9029
9030 if( conf->psk_identity != NULL )
9031 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009032 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009033 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009034 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009035 conf->psk_identity_len = 0;
9036 }
9037#endif
9038
9039#if defined(MBEDTLS_X509_CRT_PARSE_C)
9040 ssl_key_cert_free( conf->key_cert );
9041#endif
9042
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009043 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009044}
9045
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009046#if defined(MBEDTLS_PK_C) && \
9047 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009048/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009051unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009052{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009053#if defined(MBEDTLS_RSA_C)
9054 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9055 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009056#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009057#if defined(MBEDTLS_ECDSA_C)
9058 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9059 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009060#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009061 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009062}
9063
Hanno Becker7e5437a2017-04-28 17:15:26 +01009064unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9065{
9066 switch( type ) {
9067 case MBEDTLS_PK_RSA:
9068 return( MBEDTLS_SSL_SIG_RSA );
9069 case MBEDTLS_PK_ECDSA:
9070 case MBEDTLS_PK_ECKEY:
9071 return( MBEDTLS_SSL_SIG_ECDSA );
9072 default:
9073 return( MBEDTLS_SSL_SIG_ANON );
9074 }
9075}
9076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009078{
9079 switch( sig )
9080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009081#if defined(MBEDTLS_RSA_C)
9082 case MBEDTLS_SSL_SIG_RSA:
9083 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009084#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009085#if defined(MBEDTLS_ECDSA_C)
9086 case MBEDTLS_SSL_SIG_ECDSA:
9087 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009088#endif
9089 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009090 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009091 }
9092}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009093#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009094
Hanno Becker7e5437a2017-04-28 17:15:26 +01009095#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9096 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9097
9098/* Find an entry in a signature-hash set matching a given hash algorithm. */
9099mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9100 mbedtls_pk_type_t sig_alg )
9101{
9102 switch( sig_alg )
9103 {
9104 case MBEDTLS_PK_RSA:
9105 return( set->rsa );
9106 case MBEDTLS_PK_ECDSA:
9107 return( set->ecdsa );
9108 default:
9109 return( MBEDTLS_MD_NONE );
9110 }
9111}
9112
9113/* Add a signature-hash-pair to a signature-hash set */
9114void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9115 mbedtls_pk_type_t sig_alg,
9116 mbedtls_md_type_t md_alg )
9117{
9118 switch( sig_alg )
9119 {
9120 case MBEDTLS_PK_RSA:
9121 if( set->rsa == MBEDTLS_MD_NONE )
9122 set->rsa = md_alg;
9123 break;
9124
9125 case MBEDTLS_PK_ECDSA:
9126 if( set->ecdsa == MBEDTLS_MD_NONE )
9127 set->ecdsa = md_alg;
9128 break;
9129
9130 default:
9131 break;
9132 }
9133}
9134
9135/* Allow exactly one hash algorithm for each signature. */
9136void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9137 mbedtls_md_type_t md_alg )
9138{
9139 set->rsa = md_alg;
9140 set->ecdsa = md_alg;
9141}
9142
9143#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9144 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9145
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009146/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009147 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009149mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009150{
9151 switch( hash )
9152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009153#if defined(MBEDTLS_MD5_C)
9154 case MBEDTLS_SSL_HASH_MD5:
9155 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009156#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009157#if defined(MBEDTLS_SHA1_C)
9158 case MBEDTLS_SSL_HASH_SHA1:
9159 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009161#if defined(MBEDTLS_SHA256_C)
9162 case MBEDTLS_SSL_HASH_SHA224:
9163 return( MBEDTLS_MD_SHA224 );
9164 case MBEDTLS_SSL_HASH_SHA256:
9165 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009166#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009167#if defined(MBEDTLS_SHA512_C)
9168 case MBEDTLS_SSL_HASH_SHA384:
9169 return( MBEDTLS_MD_SHA384 );
9170 case MBEDTLS_SSL_HASH_SHA512:
9171 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009172#endif
9173 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009174 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009175 }
9176}
9177
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009178/*
9179 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9180 */
9181unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9182{
9183 switch( md )
9184 {
9185#if defined(MBEDTLS_MD5_C)
9186 case MBEDTLS_MD_MD5:
9187 return( MBEDTLS_SSL_HASH_MD5 );
9188#endif
9189#if defined(MBEDTLS_SHA1_C)
9190 case MBEDTLS_MD_SHA1:
9191 return( MBEDTLS_SSL_HASH_SHA1 );
9192#endif
9193#if defined(MBEDTLS_SHA256_C)
9194 case MBEDTLS_MD_SHA224:
9195 return( MBEDTLS_SSL_HASH_SHA224 );
9196 case MBEDTLS_MD_SHA256:
9197 return( MBEDTLS_SSL_HASH_SHA256 );
9198#endif
9199#if defined(MBEDTLS_SHA512_C)
9200 case MBEDTLS_MD_SHA384:
9201 return( MBEDTLS_SSL_HASH_SHA384 );
9202 case MBEDTLS_MD_SHA512:
9203 return( MBEDTLS_SSL_HASH_SHA512 );
9204#endif
9205 default:
9206 return( MBEDTLS_SSL_HASH_NONE );
9207 }
9208}
9209
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009210#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009211/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009212 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009213 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009214 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009215int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009216{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009217 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009218
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009219 if( ssl->conf->curve_list == NULL )
9220 return( -1 );
9221
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009222 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009223 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009224 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009225
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009226 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009227}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009228#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009229
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009230#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009231/*
9232 * Check if a hash proposed by the peer is in our list.
9233 * Return 0 if we're willing to use it, -1 otherwise.
9234 */
9235int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9236 mbedtls_md_type_t md )
9237{
9238 const int *cur;
9239
9240 if( ssl->conf->sig_hashes == NULL )
9241 return( -1 );
9242
9243 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9244 if( *cur == (int) md )
9245 return( 0 );
9246
9247 return( -1 );
9248}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009249#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009251#if defined(MBEDTLS_X509_CRT_PARSE_C)
9252int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9253 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009254 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009255 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009256{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009257 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009258#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009259 int usage = 0;
9260#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009262 const char *ext_oid;
9263 size_t ext_len;
9264#endif
9265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009266#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9267 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009268 ((void) cert);
9269 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009270 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009271#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009273#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9274 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009275 {
9276 /* Server part of the key exchange */
9277 switch( ciphersuite->key_exchange )
9278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279 case MBEDTLS_KEY_EXCHANGE_RSA:
9280 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009281 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009282 break;
9283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009284 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9285 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9286 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9287 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009288 break;
9289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9291 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009292 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009293 break;
9294
9295 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296 case MBEDTLS_KEY_EXCHANGE_NONE:
9297 case MBEDTLS_KEY_EXCHANGE_PSK:
9298 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9299 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009300 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009301 usage = 0;
9302 }
9303 }
9304 else
9305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9307 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009308 }
9309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009311 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009312 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009313 ret = -1;
9314 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009315#else
9316 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009319#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9320 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009322 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9323 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009324 }
9325 else
9326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009327 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9328 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009329 }
9330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009332 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009333 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009334 ret = -1;
9335 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009336#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009337
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009338 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009340#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009341
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009342/*
9343 * Convert version numbers to/from wire format
9344 * and, for DTLS, to/from TLS equivalent.
9345 *
9346 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009347 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009348 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9349 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009351void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009352 unsigned char ver[2] )
9353{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009354#if defined(MBEDTLS_SSL_PROTO_DTLS)
9355 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009358 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9359
9360 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9361 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9362 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009363 else
9364#else
9365 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009366#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009367 {
9368 ver[0] = (unsigned char) major;
9369 ver[1] = (unsigned char) minor;
9370 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009371}
9372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009374 const unsigned char ver[2] )
9375{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009376#if defined(MBEDTLS_SSL_PROTO_DTLS)
9377 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009378 {
9379 *major = 255 - ver[0] + 2;
9380 *minor = 255 - ver[1] + 1;
9381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009382 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009383 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9384 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009385 else
9386#else
9387 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009388#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009389 {
9390 *major = ver[0];
9391 *minor = ver[1];
9392 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009393}
9394
Simon Butcher99000142016-10-13 17:21:01 +01009395int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9396{
9397#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9398 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9399 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9400
9401 switch( md )
9402 {
9403#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9404#if defined(MBEDTLS_MD5_C)
9405 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009406 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009407#endif
9408#if defined(MBEDTLS_SHA1_C)
9409 case MBEDTLS_SSL_HASH_SHA1:
9410 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9411 break;
9412#endif
9413#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9414#if defined(MBEDTLS_SHA512_C)
9415 case MBEDTLS_SSL_HASH_SHA384:
9416 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9417 break;
9418#endif
9419#if defined(MBEDTLS_SHA256_C)
9420 case MBEDTLS_SSL_HASH_SHA256:
9421 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9422 break;
9423#endif
9424 default:
9425 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9426 }
9427
9428 return 0;
9429#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9430 (void) ssl;
9431 (void) md;
9432
9433 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9434#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9435}
9436
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009437#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9438 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9439int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9440 unsigned char *output,
9441 unsigned char *data, size_t data_len )
9442{
9443 int ret = 0;
9444 mbedtls_md5_context mbedtls_md5;
9445 mbedtls_sha1_context mbedtls_sha1;
9446
9447 mbedtls_md5_init( &mbedtls_md5 );
9448 mbedtls_sha1_init( &mbedtls_sha1 );
9449
9450 /*
9451 * digitally-signed struct {
9452 * opaque md5_hash[16];
9453 * opaque sha_hash[20];
9454 * };
9455 *
9456 * md5_hash
9457 * MD5(ClientHello.random + ServerHello.random
9458 * + ServerParams);
9459 * sha_hash
9460 * SHA(ClientHello.random + ServerHello.random
9461 * + ServerParams);
9462 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009463 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009464 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009465 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009466 goto exit;
9467 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009468 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009469 ssl->handshake->randbytes, 64 ) ) != 0 )
9470 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009471 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009472 goto exit;
9473 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009474 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009475 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009476 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009477 goto exit;
9478 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009479 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009480 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009481 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009482 goto exit;
9483 }
9484
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009485 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009486 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009487 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009488 goto exit;
9489 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009490 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009491 ssl->handshake->randbytes, 64 ) ) != 0 )
9492 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009493 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009494 goto exit;
9495 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009496 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009497 data_len ) ) != 0 )
9498 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009500 goto exit;
9501 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009502 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009503 output + 16 ) ) != 0 )
9504 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009506 goto exit;
9507 }
9508
9509exit:
9510 mbedtls_md5_free( &mbedtls_md5 );
9511 mbedtls_sha1_free( &mbedtls_sha1 );
9512
9513 if( ret != 0 )
9514 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9515 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9516
9517 return( ret );
9518
9519}
9520#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9521 MBEDTLS_SSL_PROTO_TLS1_1 */
9522
9523#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9524 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9525int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009526 unsigned char *hash, size_t *hashlen,
9527 unsigned char *data, size_t data_len,
9528 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009529{
9530 int ret = 0;
9531 mbedtls_md_context_t ctx;
9532 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009533 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009534
9535 mbedtls_md_init( &ctx );
9536
9537 /*
9538 * digitally-signed struct {
9539 * opaque client_random[32];
9540 * opaque server_random[32];
9541 * ServerDHParams params;
9542 * };
9543 */
9544 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9545 {
9546 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9547 goto exit;
9548 }
9549 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9550 {
9551 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9552 goto exit;
9553 }
9554 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9555 {
9556 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9557 goto exit;
9558 }
9559 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9560 {
9561 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9562 goto exit;
9563 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009564 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009565 {
9566 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9567 goto exit;
9568 }
9569
9570exit:
9571 mbedtls_md_free( &ctx );
9572
9573 if( ret != 0 )
9574 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9575 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9576
9577 return( ret );
9578}
9579#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9580 MBEDTLS_SSL_PROTO_TLS1_2 */
9581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582#endif /* MBEDTLS_SSL_TLS_C */