blob: 5fccedffed9555ac8198899a11c00b7c1e690e5d [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
Hanno Beckera67dee22018-08-22 10:05:20 +0100112static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100113static uint16_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
114{
Hanno Beckera67dee22018-08-22 10:05:20 +0100115 uint16_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100116
117 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
118 return( (int) mtu );
119
120 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
121}
122
Hanno Becker67bc7c32018-08-06 11:33:50 +0100123static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
124{
125 size_t const bytes_written = ssl->out_left;
126 uint16_t const mtu = ssl_get_maximum_datagram_size( ssl );
127
128 /* Double-check that the write-index hasn't gone
129 * past what we can transmit in a single datagram. */
130 if( bytes_written > (size_t) mtu )
131 {
132 /* Should never happen... */
133 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
134 }
135
136 return( (int) ( mtu - bytes_written ) );
137}
138
139static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
140{
141 int ret;
142 size_t remaining, expansion;
143 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
144
145#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
146 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
147
148 if( max_len > mfl )
149 max_len = mfl;
150#endif
151
152 ret = ssl_get_remaining_space_in_datagram( ssl );
153 if( ret < 0 )
154 return( ret );
155 remaining = (size_t) ret;
156
157 ret = mbedtls_ssl_get_record_expansion( ssl );
158 if( ret < 0 )
159 return( ret );
160 expansion = (size_t) ret;
161
162 if( remaining <= expansion )
163 return( 0 );
164
165 remaining -= expansion;
166 if( remaining >= max_len )
167 remaining = max_len;
168
169 return( (int) remaining );
170}
171
Hanno Becker0271f962018-08-16 13:23:47 +0100172static void ssl_buffering_free( mbedtls_ssl_context *ssl );
173
Hanno Beckere605b192018-08-21 15:59:07 +0100174static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
175 uint8_t slot );
176
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200177/*
178 * Double the retransmit timeout value, within the allowed range,
179 * returning -1 if the maximum value has already been reached.
180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200182{
183 uint32_t new_timeout;
184
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200185 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200186 return( -1 );
187
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200188 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
189 * in the following way: after the initial transmission and a first
190 * retransmission, back off to a temporary estimated MTU of 508 bytes.
191 * This value is guaranteed to be deliverable (if not guaranteed to be
192 * delivered) of any compliant IPv4 (and IPv6) network, and should work
193 * on most non-IP stacks too. */
194 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
195 ssl->handshake->mtu = 508;
196
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200197 new_timeout = 2 * ssl->handshake->retransmit_timeout;
198
199 /* Avoid arithmetic overflow and range overflow */
200 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200201 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200202 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200203 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200204 }
205
206 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200208 ssl->handshake->retransmit_timeout ) );
209
210 return( 0 );
211}
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200214{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200215 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200217 ssl->handshake->retransmit_timeout ) );
218}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200222/*
223 * Convert max_fragment_length codes to length.
224 * RFC 6066 says:
225 * enum{
226 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
227 * } MaxFragmentLength;
228 * and we add 0 -> extension unused
229 */
Angus Grattond8213d02016-05-25 20:56:48 +1000230static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200231{
Angus Grattond8213d02016-05-25 20:56:48 +1000232 switch( mfl )
233 {
234 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
235 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
236 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
237 return 512;
238 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
239 return 1024;
240 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
241 return 2048;
242 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
243 return 4096;
244 default:
245 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
246 }
247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200249
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200250#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200252{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_ssl_session_free( dst );
254 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200257 if( src->peer_cert != NULL )
258 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200259 int ret;
260
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200261 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200262 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200263 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200268 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200271 dst->peer_cert = NULL;
272 return( ret );
273 }
274 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200276
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200277#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200278 if( src->ticket != NULL )
279 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200280 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200281 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200282 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283
284 memcpy( dst->ticket, src->ticket, src->ticket_len );
285 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200286#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287
288 return( 0 );
289}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200290#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
293int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200294 const unsigned char *key_enc, const unsigned char *key_dec,
295 size_t keylen,
296 const unsigned char *iv_enc, const unsigned char *iv_dec,
297 size_t ivlen,
298 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200299 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
301int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
302int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
303int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
304int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
305#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000306
Paul Bakker5121ce52009-01-03 21:22:43 +0000307/*
308 * Key material generation
309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200311static int ssl3_prf( const unsigned char *secret, size_t slen,
312 const char *label,
313 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000314 unsigned char *dstbuf, size_t dlen )
315{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100316 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000317 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200318 mbedtls_md5_context md5;
319 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000320 unsigned char padding[16];
321 unsigned char sha1sum[20];
322 ((void)label);
323
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200324 mbedtls_md5_init( &md5 );
325 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200326
Paul Bakker5f70b252012-09-13 14:23:06 +0000327 /*
328 * SSLv3:
329 * block =
330 * MD5( secret + SHA1( 'A' + secret + random ) ) +
331 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
332 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
333 * ...
334 */
335 for( i = 0; i < dlen / 16; i++ )
336 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200337 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000338
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100339 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100340 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100341 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100342 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100343 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100344 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100345 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100346 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100347 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100348 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000349
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100350 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100351 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100352 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100353 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100354 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100355 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100356 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100357 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000358 }
359
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100360exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200361 mbedtls_md5_free( &md5 );
362 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000363
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500364 mbedtls_platform_zeroize( padding, sizeof( padding ) );
365 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000366
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100367 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200372static int tls1_prf( const unsigned char *secret, size_t slen,
373 const char *label,
374 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000375 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000376{
Paul Bakker23986e52011-04-24 08:57:21 +0000377 size_t nb, hs;
378 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200379 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 unsigned char tmp[128];
381 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 const mbedtls_md_info_t *md_info;
383 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100384 int ret;
385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
388 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
391 hs = ( slen + 1 ) / 2;
392 S1 = secret;
393 S2 = secret + slen - hs;
394
395 nb = strlen( label );
396 memcpy( tmp + 20, label, nb );
397 memcpy( tmp + 20 + nb, random, rlen );
398 nb += rlen;
399
400 /*
401 * First compute P_md5(secret,label+random)[0..dlen]
402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
404 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100407 return( ret );
408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
410 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
411 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
413 for( i = 0; i < dlen; i += 16 )
414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_md_hmac_reset ( &md_ctx );
416 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
417 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_md_hmac_reset ( &md_ctx );
420 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
421 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
423 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
424
425 for( j = 0; j < k; j++ )
426 dstbuf[i + j] = h_i[j];
427 }
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100430
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 /*
432 * XOR out with P_sha1(secret,label+random)[0..dlen]
433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
435 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100438 return( ret );
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
441 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
442 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443
444 for( i = 0; i < dlen; i += 20 )
445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_md_hmac_reset ( &md_ctx );
447 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
448 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_md_hmac_reset ( &md_ctx );
451 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
452 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
454 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
455
456 for( j = 0; j < k; j++ )
457 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
458 }
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100461
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500462 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
463 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465 return( 0 );
466}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
470static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100471 const unsigned char *secret, size_t slen,
472 const char *label,
473 const unsigned char *random, size_t rlen,
474 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000475{
476 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100477 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000478 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
480 const mbedtls_md_info_t *md_info;
481 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100482 int ret;
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
487 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100490
491 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000493
494 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100495 memcpy( tmp + md_len, label, nb );
496 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000497 nb += rlen;
498
499 /*
500 * Compute P_<hash>(secret, label + random)[0..dlen]
501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100503 return( ret );
504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
506 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
507 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100508
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100509 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_md_hmac_reset ( &md_ctx );
512 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
513 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_md_hmac_reset ( &md_ctx );
516 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
517 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000518
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100519 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000520
521 for( j = 0; j < k; j++ )
522 dstbuf[i + j] = h_i[j];
523 }
524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100526
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500527 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
528 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000529
530 return( 0 );
531}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100534static int tls_prf_sha256( const unsigned char *secret, size_t slen,
535 const char *label,
536 const unsigned char *random, size_t rlen,
537 unsigned char *dstbuf, size_t dlen )
538{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100540 label, random, rlen, dstbuf, dlen ) );
541}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200545static int tls_prf_sha384( const unsigned char *secret, size_t slen,
546 const char *label,
547 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000548 unsigned char *dstbuf, size_t dlen )
549{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100551 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#endif /* MBEDTLS_SHA512_C */
554#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
559 defined(MBEDTLS_SSL_PROTO_TLS1_1)
560static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200561#endif
Paul Bakker380da532012-04-18 16:10:25 +0000562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563#if defined(MBEDTLS_SSL_PROTO_SSL3)
564static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
565static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200566#endif
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
569static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
570static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200571#endif
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
574#if defined(MBEDTLS_SHA256_C)
575static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
576static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
577static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200578#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580#if defined(MBEDTLS_SHA512_C)
581static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
582static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
583static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100584#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000588{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200589 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 unsigned char keyblk[256];
592 unsigned char *key1;
593 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100594 unsigned char *mac_enc;
595 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000596 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200597 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 const mbedtls_cipher_info_t *cipher_info;
599 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_ssl_session *session = ssl->session_negotiate;
602 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
603 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100608 if( cipher_info == NULL )
609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100611 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100613 }
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100616 if( md_info == NULL )
617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100619 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100621 }
622
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000624 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626#if defined(MBEDTLS_SSL_PROTO_SSL3)
627 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000628 {
Paul Bakker48916f92012-09-16 19:57:18 +0000629 handshake->tls_prf = ssl3_prf;
630 handshake->calc_verify = ssl_calc_verify_ssl;
631 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000632 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200633 else
634#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
636 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000637 {
Paul Bakker48916f92012-09-16 19:57:18 +0000638 handshake->tls_prf = tls1_prf;
639 handshake->calc_verify = ssl_calc_verify_tls;
640 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000641 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200642 else
643#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
645#if defined(MBEDTLS_SHA512_C)
646 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
647 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000648 {
Paul Bakker48916f92012-09-16 19:57:18 +0000649 handshake->tls_prf = tls_prf_sha384;
650 handshake->calc_verify = ssl_calc_verify_tls_sha384;
651 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000652 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000653 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200654#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#if defined(MBEDTLS_SHA256_C)
656 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000657 {
Paul Bakker48916f92012-09-16 19:57:18 +0000658 handshake->tls_prf = tls_prf_sha256;
659 handshake->calc_verify = ssl_calc_verify_tls_sha256;
660 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000661 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200662 else
663#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
667 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200668 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000669
670 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 * SSLv3:
672 * master =
673 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
674 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
675 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200676 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200677 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 * master = PRF( premaster, "master secret", randbytes )[0..47]
679 */
Paul Bakker0a597072012-09-25 21:55:46 +0000680 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000683 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
686 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200687 {
688 unsigned char session_hash[48];
689 size_t hash_len;
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200692
693 ssl->handshake->calc_verify( ssl, session_hash );
694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
696 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200699 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200701 {
702 hash_len = 48;
703 }
704 else
705#endif
706 hash_len = 32;
707 }
708 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200710 hash_len = 36;
711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200713
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100714 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
715 "extended master secret",
716 session_hash, hash_len,
717 session->master, 48 );
718 if( ret != 0 )
719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100721 return( ret );
722 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200723
724 }
725 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200726#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100727 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
728 "master secret",
729 handshake->randbytes, 64,
730 session->master, 48 );
731 if( ret != 0 )
732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100734 return( ret );
735 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200736
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500737 mbedtls_platform_zeroize( handshake->premaster,
738 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739 }
740 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
743 /*
744 * Swap the client and server random values.
745 */
Paul Bakker48916f92012-09-16 19:57:18 +0000746 memcpy( tmp, handshake->randbytes, 64 );
747 memcpy( handshake->randbytes, tmp + 32, 32 );
748 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500749 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
751 /*
752 * SSLv3:
753 * key block =
754 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
755 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
756 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
757 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
758 * ...
759 *
760 * TLSv1:
761 * key block = PRF( master, "key expansion", randbytes )
762 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100763 ret = handshake->tls_prf( session->master, 48, "key expansion",
764 handshake->randbytes, 64, keyblk, 256 );
765 if( ret != 0 )
766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100768 return( ret );
769 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
772 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
773 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
774 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
775 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000776
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500777 mbedtls_platform_zeroize( handshake->randbytes,
778 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000779
780 /*
781 * Determine the appropriate key, IV and MAC length.
782 */
Paul Bakker68884e32013-01-07 18:20:04 +0100783
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200784 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200787 cipher_info->mode == MBEDTLS_MODE_CCM ||
788 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000789 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200790 size_t taglen, explicit_ivlen;
791
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200792 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000793 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200794
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200795 /* All modes haves 96-bit IVs;
796 * GCM and CCM has 4 implicit and 8 explicit bytes
797 * ChachaPoly has all 12 bytes implicit
798 */
Paul Bakker68884e32013-01-07 18:20:04 +0100799 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200800 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
801 transform->fixed_ivlen = 12;
802 else
803 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200804
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200805 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
806 taglen = transform->ciphersuite_info->flags &
807 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
808
809
810 /* Minimum length of encrypted record */
811 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
812 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100813 }
814 else
815 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200816 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
818 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200821 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100822 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000823
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200824 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000825 mac_key_len = mbedtls_md_get_size( md_info );
826 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200829 /*
830 * If HMAC is to be truncated, we shall keep the leftmost bytes,
831 * (rfc 6066 page 13 or rfc 2104 section 4),
832 * so we only need to adjust the length here.
833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000837
838#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
839 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000840 * HMAC implementation which also truncates the key
841 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000842 mac_key_len = transform->maclen;
843#endif
844 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200846
847 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100848 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000849
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200850 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200852 transform->minlen = transform->maclen;
853 else
Paul Bakker68884e32013-01-07 18:20:04 +0100854 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200855 /*
856 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100857 * 1. if EtM is in use: one block plus MAC
858 * otherwise: * first multiple of blocklen greater than maclen
859 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200860 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
862 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100863 {
864 transform->minlen = transform->maclen
865 + cipher_info->block_size;
866 }
867 else
868#endif
869 {
870 transform->minlen = transform->maclen
871 + cipher_info->block_size
872 - transform->maclen % cipher_info->block_size;
873 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
876 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
877 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200878 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100879 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200880#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
882 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
883 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200884 {
885 transform->minlen += transform->ivlen;
886 }
887 else
888#endif
889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
891 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200892 }
Paul Bakker68884e32013-01-07 18:20:04 +0100893 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000894 }
895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000897 transform->keylen, transform->minlen, transform->ivlen,
898 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000899
900 /*
901 * Finally setup the cipher contexts, IVs and MAC secrets.
902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200904 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000905 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000906 key1 = keyblk + mac_key_len * 2;
907 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Paul Bakker68884e32013-01-07 18:20:04 +0100909 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000910 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000912 /*
913 * This is not used in TLS v1.1.
914 */
Paul Bakker48916f92012-09-16 19:57:18 +0000915 iv_copy_len = ( transform->fixed_ivlen ) ?
916 transform->fixed_ivlen : transform->ivlen;
917 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
918 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000919 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000920 }
921 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922#endif /* MBEDTLS_SSL_CLI_C */
923#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200924 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000925 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000926 key1 = keyblk + mac_key_len * 2 + transform->keylen;
927 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
Hanno Becker81c7b182017-11-09 18:39:33 +0000929 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100930 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000932 /*
933 * This is not used in TLS v1.1.
934 */
Paul Bakker48916f92012-09-16 19:57:18 +0000935 iv_copy_len = ( transform->fixed_ivlen ) ?
936 transform->fixed_ivlen : transform->ivlen;
937 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
938 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000939 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000940 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100941 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
945 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100946 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948#if defined(MBEDTLS_SSL_PROTO_SSL3)
949 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100950 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000951 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100955 }
956
Hanno Becker81c7b182017-11-09 18:39:33 +0000957 memcpy( transform->mac_enc, mac_enc, mac_key_len );
958 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100959 }
960 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#endif /* MBEDTLS_SSL_PROTO_SSL3 */
962#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
963 defined(MBEDTLS_SSL_PROTO_TLS1_2)
964 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100965 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100966 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
967 For AEAD-based ciphersuites, there is nothing to do here. */
968 if( mac_key_len != 0 )
969 {
970 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
971 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
972 }
Paul Bakker68884e32013-01-07 18:20:04 +0100973 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200974 else
975#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
978 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200979 }
Paul Bakker68884e32013-01-07 18:20:04 +0100980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
982 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000983 {
984 int ret = 0;
985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100989 transform->iv_enc, transform->iv_dec,
990 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100991 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +0000992 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
995 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000996 }
997 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000999
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001000#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1001 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001002 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001003 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1004 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001005 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001006 iv_copy_len );
1007 }
1008#endif
1009
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001010 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001011 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001012 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001014 return( ret );
1015 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001016
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001017 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001018 cipher_info ) ) != 0 )
1019 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001020 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001021 return( ret );
1022 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001025 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001029 return( ret );
1030 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001033 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001037 return( ret );
1038 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040#if defined(MBEDTLS_CIPHER_MODE_CBC)
1041 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1044 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001047 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001048 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1051 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001054 return( ret );
1055 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001058
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001059 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001062 // Initialize compression
1063 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001065 {
Paul Bakker16770332013-10-11 09:59:44 +02001066 if( ssl->compress_buf == NULL )
1067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001069 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001070 if( ssl->compress_buf == NULL )
1071 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001073 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001074 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001075 }
1076 }
1077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001079
Paul Bakker48916f92012-09-16 19:57:18 +00001080 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1081 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001082
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001083 if( deflateInit( &transform->ctx_deflate,
1084 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001085 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1088 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001089 }
1090 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
1095 return( 0 );
1096}
1097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098#if defined(MBEDTLS_SSL_PROTO_SSL3)
1099void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001100{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001101 mbedtls_md5_context md5;
1102 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001103 unsigned char pad_1[48];
1104 unsigned char pad_2[48];
1105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001108 mbedtls_md5_init( &md5 );
1109 mbedtls_sha1_init( &sha1 );
1110
1111 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1112 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001113
Paul Bakker380da532012-04-18 16:10:25 +00001114 memset( pad_1, 0x36, 48 );
1115 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001116
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001117 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1118 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1119 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001121 mbedtls_md5_starts_ret( &md5 );
1122 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1123 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1124 mbedtls_md5_update_ret( &md5, hash, 16 );
1125 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001126
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001127 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1128 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1129 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001130
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001131 mbedtls_sha1_starts_ret( &sha1 );
1132 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1133 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1134 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1135 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001139
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001140 mbedtls_md5_free( &md5 );
1141 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001142
Paul Bakker380da532012-04-18 16:10:25 +00001143 return;
1144}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1148void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001149{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001150 mbedtls_md5_context md5;
1151 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001154
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001155 mbedtls_md5_init( &md5 );
1156 mbedtls_sha1_init( &sha1 );
1157
1158 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1159 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001160
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001161 mbedtls_md5_finish_ret( &md5, hash );
1162 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001166
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001167 mbedtls_md5_free( &md5 );
1168 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001169
Paul Bakker380da532012-04-18 16:10:25 +00001170 return;
1171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1175#if defined(MBEDTLS_SHA256_C)
1176void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001177{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001178 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001179
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001180 mbedtls_sha256_init( &sha256 );
1181
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001183
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001184 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001185 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001189
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001190 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001191
Paul Bakker380da532012-04-18 16:10:25 +00001192 return;
1193}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196#if defined(MBEDTLS_SHA512_C)
1197void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001198{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001199 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001200
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001201 mbedtls_sha512_init( &sha512 );
1202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001204
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001205 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001206 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001210
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001211 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001212
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 return;
1214}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#endif /* MBEDTLS_SHA512_C */
1216#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1219int 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 +02001220{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001221 unsigned char *p = ssl->handshake->premaster;
1222 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001223 const unsigned char *psk = ssl->conf->psk;
1224 size_t psk_len = ssl->conf->psk_len;
1225
1226 /* If the psk callback was called, use its result */
1227 if( ssl->handshake->psk != NULL )
1228 {
1229 psk = ssl->handshake->psk;
1230 psk_len = ssl->handshake->psk_len;
1231 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001232
1233 /*
1234 * PMS = struct {
1235 * opaque other_secret<0..2^16-1>;
1236 * opaque psk<0..2^16-1>;
1237 * };
1238 * with "other_secret" depending on the particular key exchange
1239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1241 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001242 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001243 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001245
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001246 *(p++) = (unsigned char)( psk_len >> 8 );
1247 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001248
1249 if( end < p || (size_t)( end - p ) < psk_len )
1250 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1251
1252 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001253 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001254 }
1255 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1257#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1258 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001259 {
1260 /*
1261 * other_secret already set by the ClientKeyExchange message,
1262 * and is 48 bytes long
1263 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001264 if( end - p < 2 )
1265 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1266
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001267 *p++ = 0;
1268 *p++ = 48;
1269 p += 48;
1270 }
1271 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1273#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1274 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001275 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001276 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001277 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001278
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001279 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001281 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001282 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001285 return( ret );
1286 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001287 *(p++) = (unsigned char)( len >> 8 );
1288 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001289 p += len;
1290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001292 }
1293 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1295#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1296 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001297 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001298 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001299 size_t zlen;
1300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001302 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001303 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001306 return( ret );
1307 }
1308
1309 *(p++) = (unsigned char)( zlen >> 8 );
1310 *(p++) = (unsigned char)( zlen );
1311 p += zlen;
1312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001314 }
1315 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1319 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001320 }
1321
1322 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001323 if( end - p < 2 )
1324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001325
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001326 *(p++) = (unsigned char)( psk_len >> 8 );
1327 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001328
1329 if( end < p || (size_t)( end - p ) < psk_len )
1330 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1331
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001332 memcpy( p, psk, psk_len );
1333 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001334
1335 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1336
1337 return( 0 );
1338}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001342/*
1343 * SSLv3.0 MAC functions
1344 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001345#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001346static void ssl_mac( mbedtls_md_context_t *md_ctx,
1347 const unsigned char *secret,
1348 const unsigned char *buf, size_t len,
1349 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001350 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001351{
1352 unsigned char header[11];
1353 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001354 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1356 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001357
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001358 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001360 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001361 else
Paul Bakker68884e32013-01-07 18:20:04 +01001362 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001363
1364 memcpy( header, ctr, 8 );
1365 header[ 8] = (unsigned char) type;
1366 header[ 9] = (unsigned char)( len >> 8 );
1367 header[10] = (unsigned char)( len );
1368
Paul Bakker68884e32013-01-07 18:20:04 +01001369 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 mbedtls_md_starts( md_ctx );
1371 mbedtls_md_update( md_ctx, secret, md_size );
1372 mbedtls_md_update( md_ctx, padding, padlen );
1373 mbedtls_md_update( md_ctx, header, 11 );
1374 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001375 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
Paul Bakker68884e32013-01-07 18:20:04 +01001377 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 mbedtls_md_starts( md_ctx );
1379 mbedtls_md_update( md_ctx, secret, md_size );
1380 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001381 mbedtls_md_update( md_ctx, out, md_size );
1382 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1387 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001388 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001389#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001390#endif
1391
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001392/* The function below is only used in the Lucky 13 counter-measure in
1393 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1394#if defined(SSL_SOME_MODES_USE_MAC) && \
1395 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1396 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1397 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1398/* This function makes sure every byte in the memory region is accessed
1399 * (in ascending addresses order) */
1400static void ssl_read_memory( unsigned char *p, size_t len )
1401{
1402 unsigned char acc = 0;
1403 volatile unsigned char force;
1404
1405 for( ; len != 0; p++, len-- )
1406 acc ^= *p;
1407
1408 force = acc;
1409 (void) force;
1410}
1411#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1412
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001413/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001417{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001419 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001422
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001423 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1426 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001427 }
1428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001432 ssl->out_msg, ssl->out_msglen );
1433
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001435 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001437#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 if( mode == MBEDTLS_MODE_STREAM ||
1439 ( mode == MBEDTLS_MODE_CBC
1440#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1441 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001442#endif
1443 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#if defined(MBEDTLS_SSL_PROTO_SSL3)
1446 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001447 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001448 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001449
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001450 ssl_mac( &ssl->transform_out->md_ctx_enc,
1451 ssl->transform_out->mac_enc,
1452 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001453 ssl->out_ctr, ssl->out_msgtype,
1454 mac );
1455
1456 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001457 }
1458 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001459#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1461 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1462 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001463 {
Hanno Becker992b6872017-11-09 18:57:39 +00001464 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1467 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1468 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1469 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001470 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001471 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001473
1474 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001475 }
1476 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001477#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1480 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001481 }
1482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001484 ssl->out_msg + ssl->out_msglen,
1485 ssl->transform_out->maclen );
1486
1487 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001488 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001489 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001490#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001491
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001492 /*
1493 * Encrypt
1494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1496 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001497 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001498 int ret;
1499 size_t olen = 0;
1500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 "including %d bytes of padding",
1503 ssl->out_msglen, 0 ) );
1504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001506 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001507 ssl->transform_out->ivlen,
1508 ssl->out_msg, ssl->out_msglen,
1509 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001512 return( ret );
1513 }
1514
1515 if( ssl->out_msglen != olen )
1516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1518 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001519 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001520 }
Paul Bakker68884e32013-01-07 18:20:04 +01001521 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001523#if defined(MBEDTLS_GCM_C) || \
1524 defined(MBEDTLS_CCM_C) || \
1525 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001527 mode == MBEDTLS_MODE_CCM ||
1528 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001529 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001530 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001531 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001532 unsigned char *enc_msg;
1533 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001534 unsigned char iv[12];
1535 mbedtls_ssl_transform *transform = ssl->transform_out;
1536 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001538 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001539
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001540 /*
1541 * Prepare additional authenticated data
1542 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001543 memcpy( add_data, ssl->out_ctr, 8 );
1544 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001546 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001547 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1548 add_data[12] = ssl->out_msglen & 0xFF;
1549
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001550 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001551
Paul Bakker68884e32013-01-07 18:20:04 +01001552 /*
1553 * Generate IV
1554 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001555 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1556 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001557 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001558 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1559 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1560 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1561
1562 }
1563 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1564 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001565 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001566 unsigned char i;
1567
1568 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1569
1570 for( i = 0; i < 8; i++ )
1571 iv[i+4] ^= ssl->out_ctr[i];
1572 }
1573 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001574 {
1575 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1577 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001578 }
1579
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001580 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1581 iv, transform->ivlen );
1582 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1583 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001584
Paul Bakker68884e32013-01-07 18:20:04 +01001585 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001586 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001587 */
1588 enc_msg = ssl->out_msg;
1589 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001590 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001593 "including 0 bytes of padding",
1594 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001595
Paul Bakker68884e32013-01-07 18:20:04 +01001596 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001597 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001598 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001599 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1600 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001601 add_data, 13,
1602 enc_msg, enc_msglen,
1603 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001604 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001607 return( ret );
1608 }
1609
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001610 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1613 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001614 }
1615
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001616 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001617 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001620 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001621 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1623#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001624 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001626 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001627 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001628 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001629 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001630
Paul Bakker48916f92012-09-16 19:57:18 +00001631 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1632 ssl->transform_out->ivlen;
1633 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001634 padlen = 0;
1635
1636 for( i = 0; i <= padlen; i++ )
1637 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1638
1639 ssl->out_msglen += padlen + 1;
1640
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001641 enc_msglen = ssl->out_msglen;
1642 enc_msg = ssl->out_msg;
1643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001645 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001646 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1647 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001650 {
1651 /*
1652 * Generate IV
1653 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001654 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001655 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001656 if( ret != 0 )
1657 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001658
Paul Bakker92be97b2013-01-02 17:30:03 +01001659 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001660 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001661
1662 /*
1663 * Fix pointer positions and message length with added IV
1664 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001665 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001666 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001667 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001668 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001672 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001673 ssl->out_msglen, ssl->transform_out->ivlen,
1674 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001677 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001678 ssl->transform_out->ivlen,
1679 enc_msg, enc_msglen,
1680 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001683 return( ret );
1684 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001685
Paul Bakkercca5b812013-08-31 17:40:26 +02001686 if( enc_msglen != olen )
1687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001690 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1693 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001694 {
1695 /*
1696 * Save IV in SSL3 and TLS1
1697 */
1698 memcpy( ssl->transform_out->iv_enc,
1699 ssl->transform_out->cipher_ctx_enc.iv,
1700 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001701 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001702#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001705 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001706 {
1707 /*
1708 * MAC(MAC_write_key, seq_num +
1709 * TLSCipherText.type +
1710 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001711 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001712 * IV + // except for TLS 1.0
1713 * ENC(content + padding + padding_length));
1714 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001715 unsigned char pseudo_hdr[13];
1716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001718
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001719 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1720 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001721 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1722 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1727 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001728 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001730 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001732
1733 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001734 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001735 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001737 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001738 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001740 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1743 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001744 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001745
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001746 /* Make extra sure authentication was performed, exactly once */
1747 if( auth_done != 1 )
1748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1750 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001751 }
1752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
1755 return( 0 );
1756}
1757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001759{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001761 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001762#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001763 size_t padlen = 0, correct = 1;
1764#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001767
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001768 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1771 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001772 }
1773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001775
Paul Bakker48916f92012-09-16 19:57:18 +00001776 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001779 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001781 }
1782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1784 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001785 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001786 int ret;
1787 size_t olen = 0;
1788
Paul Bakker68884e32013-01-07 18:20:04 +01001789 padlen = 0;
1790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001792 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001793 ssl->transform_in->ivlen,
1794 ssl->in_msg, ssl->in_msglen,
1795 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001798 return( ret );
1799 }
1800
1801 if( ssl->in_msglen != olen )
1802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1804 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001805 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001806 }
Paul Bakker68884e32013-01-07 18:20:04 +01001807 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001809#if defined(MBEDTLS_GCM_C) || \
1810 defined(MBEDTLS_CCM_C) || \
1811 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001813 mode == MBEDTLS_MODE_CCM ||
1814 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001815 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001816 int ret;
1817 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001818 unsigned char *dec_msg;
1819 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001820 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001821 unsigned char iv[12];
1822 mbedtls_ssl_transform *transform = ssl->transform_in;
1823 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001825 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001826
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001827 /*
1828 * Compute and update sizes
1829 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001830 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001833 "+ taglen (%d)", ssl->in_msglen,
1834 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001836 }
1837 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1838
Paul Bakker68884e32013-01-07 18:20:04 +01001839 dec_msg = ssl->in_msg;
1840 dec_msg_result = ssl->in_msg;
1841 ssl->in_msglen = dec_msglen;
1842
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001843 /*
1844 * Prepare additional authenticated data
1845 */
Paul Bakker68884e32013-01-07 18:20:04 +01001846 memcpy( add_data, ssl->in_ctr, 8 );
1847 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001849 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001850 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1851 add_data[12] = ssl->in_msglen & 0xFF;
1852
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001853 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001854
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001855 /*
1856 * Prepare IV
1857 */
1858 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1859 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001860 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001861 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1862 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001863
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001864 }
1865 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1866 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001867 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001868 unsigned char i;
1869
1870 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1871
1872 for( i = 0; i < 8; i++ )
1873 iv[i+4] ^= ssl->in_ctr[i];
1874 }
1875 else
1876 {
1877 /* Reminder if we ever add an AEAD mode with a different size */
1878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1879 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1880 }
1881
1882 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001884
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001885 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001886 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001889 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001890 add_data, 13,
1891 dec_msg, dec_msglen,
1892 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001893 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1898 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001899
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001900 return( ret );
1901 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001902 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001903
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001904 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1907 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001908 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001909 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1912#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001913 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001915 {
Paul Bakker45829992013-01-03 14:52:21 +01001916 /*
1917 * Decrypt and check the padding
1918 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001919 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001920 unsigned char *dec_msg;
1921 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001922 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001923 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001924 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001925
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 /*
Paul Bakker45829992013-01-03 14:52:21 +01001927 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1930 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001931 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001932#endif
Paul Bakker45829992013-01-03 14:52:21 +01001933
1934 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1935 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001938 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1939 ssl->transform_in->ivlen,
1940 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001942 }
1943
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001944 dec_msglen = ssl->in_msglen;
1945 dec_msg = ssl->in_msg;
1946 dec_msg_result = ssl->in_msg;
1947
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001948 /*
1949 * Authenticate before decrypt if enabled
1950 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1952 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001953 {
Hanno Becker992b6872017-11-09 18:57:39 +00001954 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001955 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001958
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001959 dec_msglen -= ssl->transform_in->maclen;
1960 ssl->in_msglen -= ssl->transform_in->maclen;
1961
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001962 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1963 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1964 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1965 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1970 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001971 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001972 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001976 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001977 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001978 ssl->transform_in->maclen );
1979
Hanno Becker992b6872017-11-09 18:57:39 +00001980 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1981 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001986 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001987 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001988 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001990
1991 /*
1992 * Check length sanity
1993 */
1994 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001997 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001999 }
2000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002002 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002003 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002006 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002007 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002008 dec_msglen -= ssl->transform_in->ivlen;
2009 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002010
Paul Bakker48916f92012-09-16 19:57:18 +00002011 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002012 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002013 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002017 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002018 ssl->transform_in->ivlen,
2019 dec_msg, dec_msglen,
2020 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002023 return( ret );
2024 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002025
Paul Bakkercca5b812013-08-31 17:40:26 +02002026 if( dec_msglen != olen )
2027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2029 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002030 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2033 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002034 {
2035 /*
2036 * Save IV in SSL3 and TLS1
2037 */
2038 memcpy( ssl->transform_in->iv_dec,
2039 ssl->transform_in->cipher_ctx_dec.iv,
2040 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
2044 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002045
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002046 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002047 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049#if defined(MBEDTLS_SSL_DEBUG_ALL)
2050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002051 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002052#endif
Paul Bakker45829992013-01-03 14:52:21 +01002053 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002054 correct = 0;
2055 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057#if defined(MBEDTLS_SSL_PROTO_SSL3)
2058 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 {
Paul Bakker48916f92012-09-16 19:57:18 +00002060 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062#if defined(MBEDTLS_SSL_DEBUG_ALL)
2063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002064 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002065 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002066#endif
Paul Bakker45829992013-01-03 14:52:21 +01002067 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 }
2069 }
2070 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2072#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2073 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2074 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 {
2076 /*
Paul Bakker45829992013-01-03 14:52:21 +01002077 * TLSv1+: always check the padding up to the first failure
2078 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002080 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002081 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002082 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002083
Paul Bakker956c9e02013-12-19 14:42:28 +01002084 /*
2085 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002086 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002087 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002088 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002089 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002090 *
2091 * In both cases we reset padding_idx to a safe value (0) to
2092 * prevent out-of-buffer reads.
2093 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002094 correct &= ( padlen <= ssl->in_msglen );
2095 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002096 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002097
2098 padding_idx *= correct;
2099
Angus Grattonb512bc12018-06-19 15:57:50 +10002100 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002101 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002102 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002103 pad_count += real_count *
2104 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2105 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002106
2107 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002110 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002112#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002113 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002115 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2117 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +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 );
Paul Bakker577e0062013-08-28 11:57:20 +02002121 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002122
2123 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002125 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002127 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2130 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002131 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002132
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002133#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002136#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002137
2138 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002139 * Authenticate if not done yet.
2140 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002142#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002143 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002144 {
Hanno Becker992b6872017-11-09 18:57:39 +00002145 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002146
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002147 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002148
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002149 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2150 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#if defined(MBEDTLS_SSL_PROTO_SSL3)
2153 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002154 {
2155 ssl_mac( &ssl->transform_in->md_ctx_dec,
2156 ssl->transform_in->mac_dec,
2157 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002158 ssl->in_ctr, ssl->in_msgtype,
2159 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002160 }
2161 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2163#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2164 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2165 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002166 {
2167 /*
2168 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002169 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002170 *
2171 * Known timing attacks:
2172 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2173 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002174 * To compensate for different timings for the MAC calculation
2175 * depending on how much padding was removed (which is determined
2176 * by padlen), process extra_run more blocks through the hash
2177 * function.
2178 *
2179 * The formula in the paper is
2180 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2181 * where L1 is the size of the header plus the decrypted message
2182 * plus CBC padding and L2 is the size of the header plus the
2183 * decrypted message. This is for an underlying hash function
2184 * with 64-byte blocks.
2185 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2186 * correctly. We round down instead of up, so -56 is the correct
2187 * value for our calculations instead of -55.
2188 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002189 * Repeat the formula rather than defining a block_size variable.
2190 * This avoids requiring division by a variable at runtime
2191 * (which would be marginally less efficient and would require
2192 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002193 */
2194 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002195
2196 /*
2197 * The next two sizes are the minimum and maximum values of
2198 * in_msglen over all padlen values.
2199 *
2200 * They're independent of padlen, since we previously did
2201 * in_msglen -= padlen.
2202 *
2203 * Note that max_len + maclen is never more than the buffer
2204 * length, as we previously did in_msglen -= maclen too.
2205 */
2206 const size_t max_len = ssl->in_msglen + padlen;
2207 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2208
Gilles Peskine20b44082018-05-29 14:06:49 +02002209 switch( ssl->transform_in->ciphersuite_info->mac )
2210 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002211#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2212 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002213 case MBEDTLS_MD_MD5:
2214 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002215 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002216 /* 8 bytes of message size, 64-byte compression blocks */
2217 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2218 ( 13 + ssl->in_msglen + 8 ) / 64;
2219 break;
2220#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002221#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002222 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002223 /* 16 bytes of message size, 128-byte compression blocks */
2224 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2225 ( 13 + ssl->in_msglen + 16 ) / 128;
2226 break;
2227#endif
2228 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002230 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2231 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002232
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002233 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2236 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2237 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2238 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002239 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002240 /* Make sure we access everything even when padlen > 0. This
2241 * makes the synchronisation requirements for just-in-time
2242 * Prime+Probe attacks much tighter and hopefully impractical. */
2243 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002244 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002245
2246 /* Call mbedtls_md_process at least once due to cache attacks
2247 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002248 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002252
2253 /* Make sure we access all the memory that could contain the MAC,
2254 * before we check it in the next code block. This makes the
2255 * synchronisation requirements for just-in-time Prime+Probe
2256 * attacks much tighter and hopefully impractical. */
2257 ssl_read_memory( ssl->in_msg + min_len,
2258 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002259 }
2260 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2262 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2265 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002266 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002267
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002268#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002269 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2270 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2271 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002272#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002273
Hanno Becker992b6872017-11-09 18:57:39 +00002274 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2275 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277#if defined(MBEDTLS_SSL_DEBUG_ALL)
2278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002279#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002280 correct = 0;
2281 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002282 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002283
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002284 /*
2285 * Finally check the correct flag
2286 */
2287 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002289 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002290#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002291
2292 /* Make extra sure authentication was performed, exactly once */
2293 if( auth_done != 1 )
2294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2296 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002297 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002298
2299 if( ssl->in_msglen == 0 )
2300 {
Angus Gratton34817922018-06-19 15:58:22 +10002301#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2302 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2303 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2304 {
2305 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2307 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2308 }
2309#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2310
Paul Bakker5121ce52009-01-03 21:22:43 +00002311 ssl->nb_zero++;
2312
2313 /*
2314 * Three or more empty messages may be a DoS attack
2315 * (excessive CPU consumption).
2316 */
2317 if( ssl->nb_zero > 3 )
2318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002320 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002322 }
2323 }
2324 else
2325 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002328 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002329 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002330 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002331 }
2332 else
2333#endif
2334 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002335 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002336 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2337 if( ++ssl->in_ctr[i - 1] != 0 )
2338 break;
2339
2340 /* The loop goes to its end iff the counter is wrapping */
2341 if( i == ssl_ep_len( ssl ) )
2342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2344 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002345 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002346 }
2347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002349
2350 return( 0 );
2351}
2352
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002353#undef MAC_NONE
2354#undef MAC_PLAINTEXT
2355#undef MAC_CIPHERTEXT
2356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002358/*
2359 * Compression/decompression functions
2360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002362{
2363 int ret;
2364 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002365 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002366 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002367 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002370
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002371 if( len_pre == 0 )
2372 return( 0 );
2373
Paul Bakker2770fbd2012-07-03 13:30:23 +00002374 memcpy( msg_pre, ssl->out_msg, len_pre );
2375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002377 ssl->out_msglen ) );
2378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002380 ssl->out_msg, ssl->out_msglen );
2381
Paul Bakker48916f92012-09-16 19:57:18 +00002382 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2383 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2384 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002385 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002386
Paul Bakker48916f92012-09-16 19:57:18 +00002387 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002388 if( ret != Z_OK )
2389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2391 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002392 }
2393
Angus Grattond8213d02016-05-25 20:56:48 +10002394 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002395 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002398 ssl->out_msglen ) );
2399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002401 ssl->out_msg, ssl->out_msglen );
2402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002404
2405 return( 0 );
2406}
2407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002409{
2410 int ret;
2411 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002412 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002413 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002414 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002417
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002418 if( len_pre == 0 )
2419 return( 0 );
2420
Paul Bakker2770fbd2012-07-03 13:30:23 +00002421 memcpy( msg_pre, ssl->in_msg, len_pre );
2422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002424 ssl->in_msglen ) );
2425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002427 ssl->in_msg, ssl->in_msglen );
2428
Paul Bakker48916f92012-09-16 19:57:18 +00002429 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2430 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2431 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002432 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002433 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002434
Paul Bakker48916f92012-09-16 19:57:18 +00002435 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002436 if( ret != Z_OK )
2437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2439 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002440 }
2441
Angus Grattond8213d02016-05-25 20:56:48 +10002442 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002443 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002446 ssl->in_msglen ) );
2447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002449 ssl->in_msg, ssl->in_msglen );
2450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002452
2453 return( 0 );
2454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2458static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460#if defined(MBEDTLS_SSL_PROTO_DTLS)
2461static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002462{
2463 /* If renegotiation is not enforced, retransmit until we would reach max
2464 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002465 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002466 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002467 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002468 unsigned char doublings = 1;
2469
2470 while( ratio != 0 )
2471 {
2472 ++doublings;
2473 ratio >>= 1;
2474 }
2475
2476 if( ++ssl->renego_records_seen > doublings )
2477 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002479 return( 0 );
2480 }
2481 }
2482
2483 return( ssl_write_hello_request( ssl ) );
2484}
2485#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002487
Paul Bakker5121ce52009-01-03 21:22:43 +00002488/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002489 * Fill the input message buffer by appending data to it.
2490 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002491 *
2492 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2493 * available (from this read and/or a previous one). Otherwise, an error code
2494 * is returned (possibly EOF or WANT_READ).
2495 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002496 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2497 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2498 * since we always read a whole datagram at once.
2499 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002500 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002501 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002504{
Paul Bakker23986e52011-04-24 08:57:21 +00002505 int ret;
2506 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002510 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002513 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002515 }
2516
Angus Grattond8213d02016-05-25 20:56:48 +10002517 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002521 }
2522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002524 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002526 uint32_t timeout;
2527
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002528 /* Just to be sure */
2529 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2530 {
2531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2532 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2533 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2534 }
2535
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002536 /*
2537 * The point is, we need to always read a full datagram at once, so we
2538 * sometimes read more then requested, and handle the additional data.
2539 * It could be the rest of the current record (while fetching the
2540 * header) and/or some other records in the same datagram.
2541 */
2542
2543 /*
2544 * Move to the next record in the already read datagram if applicable
2545 */
2546 if( ssl->next_record_offset != 0 )
2547 {
2548 if( ssl->in_left < ssl->next_record_offset )
2549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2551 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002552 }
2553
2554 ssl->in_left -= ssl->next_record_offset;
2555
2556 if( ssl->in_left != 0 )
2557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002559 ssl->next_record_offset ) );
2560 memmove( ssl->in_hdr,
2561 ssl->in_hdr + ssl->next_record_offset,
2562 ssl->in_left );
2563 }
2564
2565 ssl->next_record_offset = 0;
2566 }
2567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002570
2571 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002572 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002573 */
2574 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002577 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002578 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002579
2580 /*
2581 * A record can't be split accross datagrams. If we need to read but
2582 * are not at the beginning of a new record, the caller did something
2583 * wrong.
2584 */
2585 if( ssl->in_left != 0 )
2586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002589 }
2590
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002591 /*
2592 * Don't even try to read if time's out already.
2593 * This avoids by-passing the timer when repeatedly receiving messages
2594 * that will end up being dropped.
2595 */
2596 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002597 {
2598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002599 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002600 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002601 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002602 {
Angus Grattond8213d02016-05-25 20:56:48 +10002603 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002606 timeout = ssl->handshake->retransmit_timeout;
2607 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002608 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002611
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002612 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002613 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2614 timeout );
2615 else
2616 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002619
2620 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002622 }
2623
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002624 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002627 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002630 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002631 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002634 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002635 }
2636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002640 return( ret );
2641 }
2642
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002643 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002644 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002646 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002648 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002649 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002652 return( ret );
2653 }
2654
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002655 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002656 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002658 }
2659
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 if( ret < 0 )
2661 return( ret );
2662
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002663 ssl->in_left = ret;
2664 }
2665 else
2666#endif
2667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002669 ssl->in_left, nb_want ) );
2670
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002671 while( ssl->in_left < nb_want )
2672 {
2673 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002674
2675 if( ssl_check_timer( ssl ) != 0 )
2676 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2677 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002678 {
2679 if( ssl->f_recv_timeout != NULL )
2680 {
2681 ret = ssl->f_recv_timeout( ssl->p_bio,
2682 ssl->in_hdr + ssl->in_left, len,
2683 ssl->conf->read_timeout );
2684 }
2685 else
2686 {
2687 ret = ssl->f_recv( ssl->p_bio,
2688 ssl->in_hdr + ssl->in_left, len );
2689 }
2690 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002693 ssl->in_left, nb_want ) );
2694 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002695
2696 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002698
2699 if( ret < 0 )
2700 return( ret );
2701
mohammad160352aecb92018-03-28 23:41:40 -07002702 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002703 {
Darryl Green11999bb2018-03-13 15:22:58 +00002704 MBEDTLS_SSL_DEBUG_MSG( 1,
2705 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002706 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002707 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2708 }
2709
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002710 ssl->in_left += ret;
2711 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 }
2713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
2716 return( 0 );
2717}
2718
2719/*
2720 * Flush any data not yet written
2721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002723{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002724 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002725 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002729 if( ssl->f_send == NULL )
2730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002732 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002734 }
2735
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002736 /* Avoid incrementing counter if data is flushed */
2737 if( ssl->out_left == 0 )
2738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002740 return( 0 );
2741 }
2742
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 while( ssl->out_left > 0 )
2744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2746 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002747
Hanno Becker2b1e3542018-08-06 11:19:13 +01002748 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002749 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002752
2753 if( ret <= 0 )
2754 return( ret );
2755
mohammad160352aecb92018-03-28 23:41:40 -07002756 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002757 {
Darryl Green11999bb2018-03-13 15:22:58 +00002758 MBEDTLS_SSL_DEBUG_MSG( 1,
2759 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002760 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002761 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2762 }
2763
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 ssl->out_left -= ret;
2765 }
2766
Hanno Becker2b1e3542018-08-06 11:19:13 +01002767#if defined(MBEDTLS_SSL_PROTO_DTLS)
2768 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2769 {
2770 ssl->out_hdr = ssl->out_buf;
2771 }
2772 else
2773#endif
2774 {
2775 ssl->out_hdr = ssl->out_buf + 8;
2776 }
2777 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002780
2781 return( 0 );
2782}
2783
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002784/*
2785 * Functions to handle the DTLS retransmission state machine
2786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002788/*
2789 * Append current handshake message to current outgoing flight
2790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002792{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2795 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2796 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002797
2798 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002799 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002800 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002803 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002804 }
2805
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002806 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002807 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002810 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002811 }
2812
2813 /* Copy current handshake message with headers */
2814 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2815 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002816 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002817 msg->next = NULL;
2818
2819 /* Append to the current flight */
2820 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002821 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002822 else
2823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002825 while( cur->next != NULL )
2826 cur = cur->next;
2827 cur->next = msg;
2828 }
2829
Hanno Becker3b235902018-08-06 09:54:53 +01002830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002831 return( 0 );
2832}
2833
2834/*
2835 * Free the current flight of handshake messages
2836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002838{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 mbedtls_ssl_flight_item *cur = flight;
2840 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002841
2842 while( cur != NULL )
2843 {
2844 next = cur->next;
2845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 mbedtls_free( cur->p );
2847 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002848
2849 cur = next;
2850 }
2851}
2852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2854static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002855#endif
2856
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002857/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002858 * Swap transform_out and out_ctr with the alternative ones
2859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002861{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002863 unsigned char tmp_out_ctr[8];
2864
2865 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002868 return;
2869 }
2870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002872
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002873 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002874 tmp_transform = ssl->transform_out;
2875 ssl->transform_out = ssl->handshake->alt_transform_out;
2876 ssl->handshake->alt_transform_out = tmp_transform;
2877
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002878 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002879 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2880 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002881 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002882
2883 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002884 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2887 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2892 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002893 }
2894 }
2895#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002896}
2897
2898/*
2899 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002900 */
2901int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2902{
2903 int ret = 0;
2904
2905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2906
2907 ret = mbedtls_ssl_flight_transmit( ssl );
2908
2909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2910
2911 return( ret );
2912}
2913
2914/*
2915 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002916 *
2917 * Need to remember the current message in case flush_output returns
2918 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002919 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002920 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002921int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002922{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002923 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002927 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002929
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002930 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002931 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002932 ssl_swap_epochs( ssl );
2933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002935 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002936
2937 while( ssl->handshake->cur_msg != NULL )
2938 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002939 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002940 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002941
Hanno Beckere1dcb032018-08-17 16:47:58 +01002942 int const is_finished =
2943 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2944 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2945
Hanno Becker04da1892018-08-14 13:22:10 +01002946 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2947 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2948
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002949 /* Swap epochs before sending Finished: we can't do it after
2950 * sending ChangeCipherSpec, in case write returns WANT_READ.
2951 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002952 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002953 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002955 ssl_swap_epochs( ssl );
2956 }
2957
Hanno Becker67bc7c32018-08-06 11:33:50 +01002958 ret = ssl_get_remaining_payload_in_datagram( ssl );
2959 if( ret < 0 )
2960 return( ret );
2961 max_frag_len = (size_t) ret;
2962
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002963 /* CCS is copied as is, while HS messages may need fragmentation */
2964 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2965 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002966 if( max_frag_len == 0 )
2967 {
2968 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2969 return( ret );
2970
2971 continue;
2972 }
2973
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002974 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002975 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002976 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002977
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002978 /* Update position inside current message */
2979 ssl->handshake->cur_msg_p += cur->len;
2980 }
2981 else
2982 {
2983 const unsigned char * const p = ssl->handshake->cur_msg_p;
2984 const size_t hs_len = cur->len - 12;
2985 const size_t frag_off = p - ( cur->p + 12 );
2986 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002987 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002988
Hanno Beckere1dcb032018-08-17 16:47:58 +01002989 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002990 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002991 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002992 ssl_swap_epochs( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002993
2994 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2995 return( ret );
2996
2997 continue;
2998 }
2999 max_hs_frag_len = max_frag_len - 12;
3000
3001 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3002 max_hs_frag_len : rem_len;
3003
3004 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003005 {
3006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003007 (unsigned) cur_hs_frag_len,
3008 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003009 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003010
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003011 /* Messages are stored with handshake headers as if not fragmented,
3012 * copy beginning of headers then fill fragmentation fields.
3013 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3014 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003015
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003016 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3017 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3018 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3019
Hanno Becker67bc7c32018-08-06 11:33:50 +01003020 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3021 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3022 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003023
3024 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3025
Hanno Becker67bc7c32018-08-06 11:33:50 +01003026 /* Copy the handshame message content and set records fields */
3027 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3028 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003029 ssl->out_msgtype = cur->type;
3030
3031 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003032 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003033 }
3034
3035 /* If done with the current message move to the next one if any */
3036 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3037 {
3038 if( cur->next != NULL )
3039 {
3040 ssl->handshake->cur_msg = cur->next;
3041 ssl->handshake->cur_msg_p = cur->next->p + 12;
3042 }
3043 else
3044 {
3045 ssl->handshake->cur_msg = NULL;
3046 ssl->handshake->cur_msg_p = NULL;
3047 }
3048 }
3049
3050 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003051 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003054 return( ret );
3055 }
3056 }
3057
Hanno Becker67bc7c32018-08-06 11:33:50 +01003058 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3059 return( ret );
3060
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003061 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3063 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003064 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003067 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3068 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003069
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003071
3072 return( 0 );
3073}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003074
3075/*
3076 * To be called when the last message of an incoming flight is received.
3077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003079{
3080 /* We won't need to resend that one any more */
3081 ssl_flight_free( ssl->handshake->flight );
3082 ssl->handshake->flight = NULL;
3083 ssl->handshake->cur_msg = NULL;
3084
3085 /* The next incoming flight will start with this msg_seq */
3086 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3087
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003088 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003089 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003090
Hanno Becker0271f962018-08-16 13:23:47 +01003091 /* Clear future message buffering structure. */
3092 ssl_buffering_free( ssl );
3093
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003094 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003095 ssl_set_timer( ssl, 0 );
3096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3098 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003101 }
3102 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003104}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003105
3106/*
3107 * To be called when the last message of an outgoing flight is send.
3108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003110{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003111 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003112 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3115 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003118 }
3119 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003121}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003123
Paul Bakker5121ce52009-01-03 21:22:43 +00003124/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003125 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003126 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003127
3128/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003129 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003130 *
3131 * - fill in handshake headers
3132 * - update handshake checksum
3133 * - DTLS: save message for resending
3134 * - then pass to the record layer
3135 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003136 * DTLS: except for HelloRequest, messages are only queued, and will only be
3137 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003138 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003139 * Inputs:
3140 * - ssl->out_msglen: 4 + actual handshake message len
3141 * (4 is the size of handshake headers for TLS)
3142 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3143 * - ssl->out_msg + 4: the handshake message body
3144 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003145 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003146 * - ssl->out_msglen: the length of the record contents
3147 * (including handshake headers but excluding record headers)
3148 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003149 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003150int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003151{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003152 int ret;
3153 const size_t hs_len = ssl->out_msglen - 4;
3154 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003155
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3157
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003158 /*
3159 * Sanity checks
3160 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003161 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3162 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3163 {
3164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3165 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3166 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003167
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003168 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3169 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3170 ssl->handshake == NULL )
3171 {
3172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3173 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3174 }
3175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003177 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003178 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003180 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3182 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003183 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003184#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003185
Hanno Beckerb50a2532018-08-06 11:52:54 +01003186 /* Double-check that we did not exceed the bounds
3187 * of the outgoing record buffer.
3188 * This should never fail as the various message
3189 * writing functions must obey the bounds of the
3190 * outgoing record buffer, but better be safe.
3191 *
3192 * Note: We deliberately do not check for the MTU or MFL here.
3193 */
3194 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3195 {
3196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3197 "size %u, maximum %u",
3198 (unsigned) ssl->out_msglen,
3199 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3200 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3201 }
3202
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003203 /*
3204 * Fill handshake headers
3205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003207 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003208 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3209 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3210 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003211
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003212 /*
3213 * DTLS has additional fields in the Handshake layer,
3214 * between the length field and the actual payload:
3215 * uint16 message_seq;
3216 * uint24 fragment_offset;
3217 * uint24 fragment_length;
3218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003220 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003221 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003222 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003223 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003224 {
3225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3226 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003227 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003228 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003229 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3230 }
3231
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003232 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003233 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003234
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003235 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003236 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003237 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003238 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3239 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3240 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003241 }
3242 else
3243 {
3244 ssl->out_msg[4] = 0;
3245 ssl->out_msg[5] = 0;
3246 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003247
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003248 /* Handshake hashes are computed without fragmentation,
3249 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003250 memset( ssl->out_msg + 6, 0x00, 3 );
3251 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003252 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003254
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003255 /* Update running hashes of hanshake messages seen */
3256 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3257 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003258 }
3259
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003260 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003262 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003263 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003264 {
3265 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003267 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003268 return( ret );
3269 }
3270 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003271 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003272#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003273 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003274 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003275 {
3276 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3277 return( ret );
3278 }
3279 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003280
3281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3282
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003283 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003284}
3285
3286/*
3287 * Record layer functions
3288 */
3289
3290/*
3291 * Write current record.
3292 *
3293 * Uses:
3294 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3295 * - ssl->out_msglen: length of the record content (excl headers)
3296 * - ssl->out_msg: record content
3297 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003298int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003299{
3300 int ret, done = 0;
3301 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003302 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003303
3304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003307 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003309 {
3310 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003313 return( ret );
3314 }
3315
3316 len = ssl->out_msglen;
3317 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003318#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3321 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 ret = mbedtls_ssl_hw_record_write( ssl );
3326 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3329 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003330 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003331
3332 if( ret == 0 )
3333 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003334 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003336 if( !done )
3337 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003338 unsigned i;
3339 size_t protected_record_size;
3340
Paul Bakker05ef8352012-05-08 09:17:57 +00003341 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003343 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003344
Hanno Becker19859472018-08-06 09:40:20 +01003345 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003346 ssl->out_len[0] = (unsigned char)( len >> 8 );
3347 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003348
Paul Bakker48916f92012-09-16 19:57:18 +00003349 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003350 {
3351 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003354 return( ret );
3355 }
3356
3357 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003358 ssl->out_len[0] = (unsigned char)( len >> 8 );
3359 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003360 }
3361
Hanno Becker2b1e3542018-08-06 11:19:13 +01003362 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3363
3364#if defined(MBEDTLS_SSL_PROTO_DTLS)
3365 /* In case of DTLS, double-check that we don't exceed
3366 * the remaining space in the datagram. */
3367 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3368 {
3369 ret = ssl_get_maximum_datagram_size( ssl );
3370 if( ret < 0 )
3371 return( ret );
3372
3373 if( protected_record_size > (size_t) ret )
3374 {
3375 /* Should never happen */
3376 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3377 }
3378 }
3379#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Becker2b1e3542018-08-06 11:19:13 +01003382 "version = [%d:%d], msglen = %d",
3383 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2], len ) );
3384
Paul Bakker05ef8352012-05-08 09:17:57 +00003385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Becker2b1e3542018-08-06 11:19:13 +01003387 ssl->out_hdr, protected_record_size );
3388
3389 ssl->out_left += protected_record_size;
3390 ssl->out_hdr += protected_record_size;
3391 ssl_update_out_pointers( ssl, ssl->transform_out );
3392
Hanno Becker04484622018-08-06 09:49:38 +01003393 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3394 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3395 break;
3396
3397 /* The loop goes to its end iff the counter is wrapping */
3398 if( i == ssl_ep_len( ssl ) )
3399 {
3400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3401 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3402 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003403 }
3404
Hanno Becker67bc7c32018-08-06 11:33:50 +01003405#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003406 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3407 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003408 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003409 size_t remaining;
3410 ret = ssl_get_remaining_payload_in_datagram( ssl );
3411 if( ret < 0 )
3412 {
3413 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3414 ret );
3415 return( ret );
3416 }
3417
3418 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003419 if( remaining == 0 )
3420 flush = SSL_FORCE_FLUSH;
3421 else
3422 {
Hanno Becker513815a2018-08-20 11:56:09 +01003423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003424 }
3425 }
3426#endif /* MBEDTLS_SSL_PROTO_DTLS */
3427
3428 if( ( flush == SSL_FORCE_FLUSH ) &&
3429 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003432 return( ret );
3433 }
3434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003436
3437 return( 0 );
3438}
3439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003441
3442static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3443{
3444 if( ssl->in_msglen < ssl->in_hslen ||
3445 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3446 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3447 {
3448 return( 1 );
3449 }
3450 return( 0 );
3451}
Hanno Becker44650b72018-08-16 12:51:11 +01003452
3453static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context *ssl )
3454{
3455 return( ( ssl->in_msg[9] << 16 ) |
3456 ( ssl->in_msg[10] << 8 ) |
3457 ssl->in_msg[11] );
3458}
3459
3460static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context *ssl )
3461{
3462 return( ( ssl->in_msg[6] << 16 ) |
3463 ( ssl->in_msg[7] << 8 ) |
3464 ssl->in_msg[8] );
3465}
3466
3467static int ssl_check_hs_header( mbedtls_ssl_context *ssl )
3468{
3469 uint32_t msg_len, frag_off, frag_len;
3470
3471 msg_len = ssl_get_hs_total_len( ssl );
3472 frag_off = ssl_get_hs_frag_off( ssl );
3473 frag_len = ssl_get_hs_frag_len( ssl );
3474
3475 if( frag_off > msg_len )
3476 return( -1 );
3477
3478 if( frag_len > msg_len - frag_off )
3479 return( -1 );
3480
3481 if( frag_len + 12 > ssl->in_msglen )
3482 return( -1 );
3483
3484 return( 0 );
3485}
3486
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003487/*
3488 * Mark bits in bitmask (used for DTLS HS reassembly)
3489 */
3490static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3491{
3492 unsigned int start_bits, end_bits;
3493
3494 start_bits = 8 - ( offset % 8 );
3495 if( start_bits != 8 )
3496 {
3497 size_t first_byte_idx = offset / 8;
3498
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003499 /* Special case */
3500 if( len <= start_bits )
3501 {
3502 for( ; len != 0; len-- )
3503 mask[first_byte_idx] |= 1 << ( start_bits - len );
3504
3505 /* Avoid potential issues with offset or len becoming invalid */
3506 return;
3507 }
3508
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003509 offset += start_bits; /* Now offset % 8 == 0 */
3510 len -= start_bits;
3511
3512 for( ; start_bits != 0; start_bits-- )
3513 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3514 }
3515
3516 end_bits = len % 8;
3517 if( end_bits != 0 )
3518 {
3519 size_t last_byte_idx = ( offset + len ) / 8;
3520
3521 len -= end_bits; /* Now len % 8 == 0 */
3522
3523 for( ; end_bits != 0; end_bits-- )
3524 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3525 }
3526
3527 memset( mask + offset / 8, 0xFF, len / 8 );
3528}
3529
3530/*
3531 * Check that bitmask is full
3532 */
3533static int ssl_bitmask_check( unsigned char *mask, size_t len )
3534{
3535 size_t i;
3536
3537 for( i = 0; i < len / 8; i++ )
3538 if( mask[i] != 0xFF )
3539 return( -1 );
3540
3541 for( i = 0; i < len % 8; i++ )
3542 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3543 return( -1 );
3544
3545 return( 0 );
3546}
3547
Hanno Becker56e205e2018-08-16 09:06:12 +01003548/* msg_len does not include the handshake header */
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003549static size_t ssl_get_reassembly_buffer_size( unsigned msg_len,
3550 unsigned add_bitmap )
Hanno Becker56e205e2018-08-16 09:06:12 +01003551{
3552 size_t alloc_len;
Hanno Becker56e205e2018-08-16 09:06:12 +01003553
3554 alloc_len = 12; /* Handshake header */
3555 alloc_len += msg_len; /* Content buffer */
Hanno Beckerd07df862018-08-16 09:14:58 +01003556
3557 if( add_bitmap )
3558 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Hanno Becker56e205e2018-08-16 09:06:12 +01003559
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003560 return( alloc_len );
Hanno Becker56e205e2018-08-16 09:06:12 +01003561}
3562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003564
Hanno Becker12555c62018-08-16 12:47:53 +01003565static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context *ssl )
3566{
3567 return( ( ssl->in_msg[1] << 16 ) |
3568 ( ssl->in_msg[2] << 8 ) |
3569 ssl->in_msg[3] );
3570}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003571
Simon Butcher99000142016-10-13 17:21:01 +01003572int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003573{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003577 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003579 }
3580
Hanno Becker12555c62018-08-16 12:47:53 +01003581 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003584 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003585 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003588 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003589 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003590 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003591 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003592
Hanno Becker44650b72018-08-16 12:51:11 +01003593 if( ssl_check_hs_header( ssl ) != 0 )
3594 {
3595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3596 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3597 }
3598
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003599 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003600 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3601 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3602 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3603 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003604 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003605 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3606 {
3607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3608 recv_msg_seq,
3609 ssl->handshake->in_msg_seq ) );
3610 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3611 }
3612
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003613 /* Retransmit only on last message from previous flight, to avoid
3614 * too many retransmissions.
3615 * Besides, No sane server ever retransmits HelloVerifyRequest */
3616 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003620 "message_seq = %d, start_of_flight = %d",
3621 recv_msg_seq,
3622 ssl->handshake->in_flight_start_seq ) );
3623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003627 return( ret );
3628 }
3629 }
3630 else
3631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003633 "message_seq = %d, expected = %d",
3634 recv_msg_seq,
3635 ssl->handshake->in_msg_seq ) );
3636 }
3637
Hanno Becker90333da2017-10-10 11:27:13 +01003638 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003639 }
3640 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003641
Hanno Becker6d97ef52018-08-16 13:09:04 +01003642 /* Message reassembly is handled alongside buffering of future
3643 * messages; the commonality is that both handshake fragments and
3644 * future messages cannot be forwarded immediately to the handshake
3645 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003646 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003649 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003650 }
3651 }
3652 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003654 /* With TLS we don't handle fragmentation (for now) */
3655 if( ssl->in_msglen < ssl->in_hslen )
3656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3658 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003659 }
3660
Simon Butcher99000142016-10-13 17:21:01 +01003661 return( 0 );
3662}
3663
3664void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3665{
Hanno Becker0271f962018-08-16 13:23:47 +01003666 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003667
Hanno Becker0271f962018-08-16 13:23:47 +01003668 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003669 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003670 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003671 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003672
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003673 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003675 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003676 ssl->handshake != NULL )
3677 {
Hanno Becker0271f962018-08-16 13:23:47 +01003678 unsigned offset;
3679 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003680
Hanno Becker0271f962018-08-16 13:23:47 +01003681 /* Increment handshake sequence number */
3682 hs->in_msg_seq++;
3683
3684 /*
3685 * Clear up handshake buffering and reassembly structure.
3686 */
3687
3688 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003689 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003690
3691 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003692 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3693 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003694 offset++, hs_buf++ )
3695 {
3696 *hs_buf = *(hs_buf + 1);
3697 }
3698
3699 /* Create a fresh last entry */
3700 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003701 }
3702#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003703}
3704
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003705/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003706 * DTLS anti-replay: RFC 6347 4.1.2.6
3707 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003708 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3709 * Bit n is set iff record number in_window_top - n has been seen.
3710 *
3711 * Usually, in_window_top is the last record number seen and the lsb of
3712 * in_window is set. The only exception is the initial state (record number 0
3713 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3716static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003717{
3718 ssl->in_window_top = 0;
3719 ssl->in_window = 0;
3720}
3721
3722static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3723{
3724 return( ( (uint64_t) buf[0] << 40 ) |
3725 ( (uint64_t) buf[1] << 32 ) |
3726 ( (uint64_t) buf[2] << 24 ) |
3727 ( (uint64_t) buf[3] << 16 ) |
3728 ( (uint64_t) buf[4] << 8 ) |
3729 ( (uint64_t) buf[5] ) );
3730}
3731
3732/*
3733 * Return 0 if sequence number is acceptable, -1 otherwise
3734 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003736{
3737 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3738 uint64_t bit;
3739
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003740 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003741 return( 0 );
3742
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003743 if( rec_seqnum > ssl->in_window_top )
3744 return( 0 );
3745
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003746 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003747
3748 if( bit >= 64 )
3749 return( -1 );
3750
3751 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3752 return( -1 );
3753
3754 return( 0 );
3755}
3756
3757/*
3758 * Update replay window on new validated record
3759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003761{
3762 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3763
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003764 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003765 return;
3766
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003767 if( rec_seqnum > ssl->in_window_top )
3768 {
3769 /* Update window_top and the contents of the window */
3770 uint64_t shift = rec_seqnum - ssl->in_window_top;
3771
3772 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003773 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003774 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003775 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003776 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003777 ssl->in_window |= 1;
3778 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003779
3780 ssl->in_window_top = rec_seqnum;
3781 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003782 else
3783 {
3784 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003785 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003786
3787 if( bit < 64 ) /* Always true, but be extra sure */
3788 ssl->in_window |= (uint64_t) 1 << bit;
3789 }
3790}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003792
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003793#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003794/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003795static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3796
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003797/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003798 * Without any SSL context, check if a datagram looks like a ClientHello with
3799 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003800 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003801 *
3802 * - if cookie is valid, return 0
3803 * - if ClientHello looks superficially valid but cookie is not,
3804 * fill obuf and set olen, then
3805 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3806 * - otherwise return a specific error code
3807 */
3808static int ssl_check_dtls_clihlo_cookie(
3809 mbedtls_ssl_cookie_write_t *f_cookie_write,
3810 mbedtls_ssl_cookie_check_t *f_cookie_check,
3811 void *p_cookie,
3812 const unsigned char *cli_id, size_t cli_id_len,
3813 const unsigned char *in, size_t in_len,
3814 unsigned char *obuf, size_t buf_len, size_t *olen )
3815{
3816 size_t sid_len, cookie_len;
3817 unsigned char *p;
3818
3819 if( f_cookie_write == NULL || f_cookie_check == NULL )
3820 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3821
3822 /*
3823 * Structure of ClientHello with record and handshake headers,
3824 * and expected values. We don't need to check a lot, more checks will be
3825 * done when actually parsing the ClientHello - skipping those checks
3826 * avoids code duplication and does not make cookie forging any easier.
3827 *
3828 * 0-0 ContentType type; copied, must be handshake
3829 * 1-2 ProtocolVersion version; copied
3830 * 3-4 uint16 epoch; copied, must be 0
3831 * 5-10 uint48 sequence_number; copied
3832 * 11-12 uint16 length; (ignored)
3833 *
3834 * 13-13 HandshakeType msg_type; (ignored)
3835 * 14-16 uint24 length; (ignored)
3836 * 17-18 uint16 message_seq; copied
3837 * 19-21 uint24 fragment_offset; copied, must be 0
3838 * 22-24 uint24 fragment_length; (ignored)
3839 *
3840 * 25-26 ProtocolVersion client_version; (ignored)
3841 * 27-58 Random random; (ignored)
3842 * 59-xx SessionID session_id; 1 byte len + sid_len content
3843 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3844 * ...
3845 *
3846 * Minimum length is 61 bytes.
3847 */
3848 if( in_len < 61 ||
3849 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3850 in[3] != 0 || in[4] != 0 ||
3851 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3852 {
3853 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3854 }
3855
3856 sid_len = in[59];
3857 if( sid_len > in_len - 61 )
3858 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3859
3860 cookie_len = in[60 + sid_len];
3861 if( cookie_len > in_len - 60 )
3862 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3863
3864 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3865 cli_id, cli_id_len ) == 0 )
3866 {
3867 /* Valid cookie */
3868 return( 0 );
3869 }
3870
3871 /*
3872 * If we get here, we've got an invalid cookie, let's prepare HVR.
3873 *
3874 * 0-0 ContentType type; copied
3875 * 1-2 ProtocolVersion version; copied
3876 * 3-4 uint16 epoch; copied
3877 * 5-10 uint48 sequence_number; copied
3878 * 11-12 uint16 length; olen - 13
3879 *
3880 * 13-13 HandshakeType msg_type; hello_verify_request
3881 * 14-16 uint24 length; olen - 25
3882 * 17-18 uint16 message_seq; copied
3883 * 19-21 uint24 fragment_offset; copied
3884 * 22-24 uint24 fragment_length; olen - 25
3885 *
3886 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3887 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3888 *
3889 * Minimum length is 28.
3890 */
3891 if( buf_len < 28 )
3892 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3893
3894 /* Copy most fields and adapt others */
3895 memcpy( obuf, in, 25 );
3896 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3897 obuf[25] = 0xfe;
3898 obuf[26] = 0xff;
3899
3900 /* Generate and write actual cookie */
3901 p = obuf + 28;
3902 if( f_cookie_write( p_cookie,
3903 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3904 {
3905 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3906 }
3907
3908 *olen = p - obuf;
3909
3910 /* Go back and fill length fields */
3911 obuf[27] = (unsigned char)( *olen - 28 );
3912
3913 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3914 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3915 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3916
3917 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3918 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3919
3920 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3921}
3922
3923/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003924 * Handle possible client reconnect with the same UDP quadruplet
3925 * (RFC 6347 Section 4.2.8).
3926 *
3927 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3928 * that looks like a ClientHello.
3929 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003930 * - if the input looks like a ClientHello without cookies,
3931 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003932 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003933 * - if the input looks like a ClientHello with a valid cookie,
3934 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003935 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003936 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003937 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003938 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003939 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3940 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003941 */
3942static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3943{
3944 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003945 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003946
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003947 ret = ssl_check_dtls_clihlo_cookie(
3948 ssl->conf->f_cookie_write,
3949 ssl->conf->f_cookie_check,
3950 ssl->conf->p_cookie,
3951 ssl->cli_id, ssl->cli_id_len,
3952 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003953 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003954
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003955 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3956
3957 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003958 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003959 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003960 * If the error is permanent we'll catch it later,
3961 * if it's not, then hopefully it'll work next time. */
3962 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3963
3964 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003965 }
3966
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003967 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003968 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003969 /* Got a valid cookie, partially reset context */
3970 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3971 {
3972 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3973 return( ret );
3974 }
3975
3976 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003977 }
3978
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003979 return( ret );
3980}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003981#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003982
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003983/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003984 * ContentType type;
3985 * ProtocolVersion version;
3986 * uint16 epoch; // DTLS only
3987 * uint48 sequence_number; // DTLS only
3988 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003989 *
3990 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003991 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003992 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3993 *
3994 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003995 * 1. proceed with the record if this function returns 0
3996 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3997 * 3. return CLIENT_RECONNECT if this function return that value
3998 * 4. drop the whole datagram if this function returns anything else.
3999 * Point 2 is needed when the peer is resending, and we have already received
4000 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004001 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004002static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004003{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004004 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006 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 +02004007
Paul Bakker5121ce52009-01-03 21:22:43 +00004008 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004009 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004010 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004012 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004013 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004014 ssl->in_msgtype,
4015 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004016
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004017 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4019 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4020 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4021 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004024
4025#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004026 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4027 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004028 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4029#endif /* MBEDTLS_SSL_PROTO_DTLS */
4030 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4031 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004034 }
4035
4036 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004037 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4040 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004041 }
4042
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004043 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4046 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004047 }
4048
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004049 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004050 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004051 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4054 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004055 }
4056
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004057 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004058 * DTLS-related tests.
4059 * Check epoch before checking length constraint because
4060 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4061 * message gets duplicated before the corresponding Finished message,
4062 * the second ChangeCipherSpec should be discarded because it belongs
4063 * to an old epoch, but not because its length is shorter than
4064 * the minimum record length for packets using the new record transform.
4065 * Note that these two kinds of failures are handled differently,
4066 * as an unexpected record is silently skipped but an invalid
4067 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004068 */
4069#if defined(MBEDTLS_SSL_PROTO_DTLS)
4070 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4071 {
4072 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4073
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004074 /* Check epoch (and sequence number) with DTLS */
4075 if( rec_epoch != ssl->in_epoch )
4076 {
4077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4078 "expected %d, received %d",
4079 ssl->in_epoch, rec_epoch ) );
4080
4081#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4082 /*
4083 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4084 * access the first byte of record content (handshake type), as we
4085 * have an active transform (possibly iv_len != 0), so use the
4086 * fact that the record header len is 13 instead.
4087 */
4088 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4089 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4090 rec_epoch == 0 &&
4091 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4092 ssl->in_left > 13 &&
4093 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4094 {
4095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4096 "from the same port" ) );
4097 return( ssl_handle_possible_reconnect( ssl ) );
4098 }
4099 else
4100#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004101 {
4102 /* Consider buffering the record. */
4103 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4104 {
4105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4106 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4107 }
4108
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004109 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004110 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004111 }
4112
4113#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4114 /* Replay detection only works for the current epoch */
4115 if( rec_epoch == ssl->in_epoch &&
4116 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4117 {
4118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4119 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4120 }
4121#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004122
Hanno Becker52c6dc62017-05-26 16:07:36 +01004123 /* Drop unexpected ApplicationData records,
4124 * except at the beginning of renegotiations */
4125 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4126 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4127#if defined(MBEDTLS_SSL_RENEGOTIATION)
4128 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4129 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4130#endif
4131 )
4132 {
4133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4134 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4135 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004136 }
4137#endif /* MBEDTLS_SSL_PROTO_DTLS */
4138
Hanno Becker52c6dc62017-05-26 16:07:36 +01004139
4140 /* Check length against bounds of the current transform and version */
4141 if( ssl->transform_in == NULL )
4142 {
4143 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004144 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004145 {
4146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4147 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4148 }
4149 }
4150 else
4151 {
4152 if( ssl->in_msglen < ssl->transform_in->minlen )
4153 {
4154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4155 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4156 }
4157
4158#if defined(MBEDTLS_SSL_PROTO_SSL3)
4159 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004160 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004161 {
4162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4163 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4164 }
4165#endif
4166#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4167 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4168 /*
4169 * TLS encrypted messages can have up to 256 bytes of padding
4170 */
4171 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4172 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004173 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004174 {
4175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4176 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4177 }
4178#endif
4179 }
4180
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004181 return( 0 );
4182}
Paul Bakker5121ce52009-01-03 21:22:43 +00004183
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004184/*
4185 * If applicable, decrypt (and decompress) record content
4186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004188{
4189 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4192 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004194#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4195 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199 ret = mbedtls_ssl_hw_record_read( ssl );
4200 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004202 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4203 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004204 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004205
4206 if( ret == 0 )
4207 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004208 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004210 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004211 {
4212 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004215 return( ret );
4216 }
4217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004219 ssl->in_msg, ssl->in_msglen );
4220
Angus Grattond8213d02016-05-25 20:56:48 +10004221 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4224 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004225 }
4226 }
4227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004229 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004230 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004231 {
4232 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004235 return( ret );
4236 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004237 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004238#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004240#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004241 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004244 }
4245#endif
4246
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004247 return( 0 );
4248}
4249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004250static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004251
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004252/*
4253 * Read a record.
4254 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004255 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4256 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4257 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004258 */
Hanno Becker1097b342018-08-15 14:09:41 +01004259
4260/* Helper functions for mbedtls_ssl_read_record(). */
4261static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004262static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4263static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004264
Hanno Becker40f50842018-08-15 14:48:01 +01004265#if defined(MBEDTLS_SSL_PROTO_DTLS)
4266static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01004267static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01004268static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01004269static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01004270static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl );
4271#endif /* MBEDTLS_SSL_PROTO_DTLS */
4272
Hanno Becker327c93b2018-08-15 13:56:18 +01004273int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004274 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004275{
4276 int ret;
4277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004279
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004280 if( ssl->keep_current_message == 0 )
4281 {
4282 do {
Simon Butcher99000142016-10-13 17:21:01 +01004283
Hanno Becker26994592018-08-15 14:14:59 +01004284 ret = ssl_consume_current_message( ssl );
4285 if( ret != 0 )
4286 return( ret );
4287
Hanno Beckere74d5562018-08-15 14:26:08 +01004288 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004289 {
Hanno Becker40f50842018-08-15 14:48:01 +01004290#if defined(MBEDTLS_SSL_PROTO_DTLS)
4291 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004292
Hanno Becker40f50842018-08-15 14:48:01 +01004293 /* We only check for buffered messages if the
4294 * current datagram is fully consumed. */
4295 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4296 ssl_another_record_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004297 {
Hanno Becker40f50842018-08-15 14:48:01 +01004298 if( ssl_load_buffered_message( ssl ) == 0 )
4299 have_buffered = 1;
4300 }
4301
4302 if( have_buffered == 0 )
4303#endif /* MBEDTLS_SSL_PROTO_DTLS */
4304 {
4305 ret = ssl_get_next_record( ssl );
4306 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4307 continue;
4308
4309 if( ret != 0 )
4310 {
4311 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4312 return( ret );
4313 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004314 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004315 }
4316
4317 ret = mbedtls_ssl_handle_message_type( ssl );
4318
Hanno Becker40f50842018-08-15 14:48:01 +01004319#if defined(MBEDTLS_SSL_PROTO_DTLS)
4320 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4321 {
4322 /* Buffer future message */
4323 ret = ssl_buffer_message( ssl );
4324 if( ret != 0 )
4325 return( ret );
4326
4327 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4328 }
4329#endif /* MBEDTLS_SSL_PROTO_DTLS */
4330
Hanno Becker90333da2017-10-10 11:27:13 +01004331 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4332 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004333
4334 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004335 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004336 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004337 return( ret );
4338 }
4339
Hanno Becker327c93b2018-08-15 13:56:18 +01004340 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004341 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004342 {
4343 mbedtls_ssl_update_handshake_status( ssl );
4344 }
Simon Butcher99000142016-10-13 17:21:01 +01004345 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004346 else
Simon Butcher99000142016-10-13 17:21:01 +01004347 {
Hanno Becker02f59072018-08-15 14:00:24 +01004348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004349 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004350 }
4351
4352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4353
4354 return( 0 );
4355}
4356
Hanno Becker40f50842018-08-15 14:48:01 +01004357#if defined(MBEDTLS_SSL_PROTO_DTLS)
4358static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl )
4359{
4360 if( ssl->in_left > ssl->next_record_offset )
4361 return( 1 );
4362
4363 return( 0 );
4364}
4365
4366static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4367{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004368 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004369 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004370 int ret = 0;
4371
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004372 if( hs == NULL )
4373 return( -1 );
4374
Hanno Beckere00ae372018-08-20 09:39:42 +01004375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4376
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004377 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4378 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4379 {
4380 /* Check if we have seen a ChangeCipherSpec before.
4381 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004382 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004383 {
4384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4385 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004386 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004387 }
4388
4389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Inject buffered CCS message" ) );
4390 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4391 ssl->in_msglen = 1;
4392 ssl->in_msg[0] = 1;
4393
4394 /* As long as they are equal, the exact value doesn't matter. */
4395 ssl->in_left = 0;
4396 ssl->next_record_offset = 0;
4397
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004398 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004399 goto exit;
4400 }
Hanno Becker37f95322018-08-16 13:55:32 +01004401
4402 /* Debug only */
4403 {
4404 unsigned offset;
4405 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4406 {
4407 hs_buf = &hs->buffering.hs[offset];
4408 if( hs_buf->is_valid == 1 )
4409 {
4410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4411 hs->in_msg_seq + offset,
4412 hs_buf->is_complete ? "fully" : "partitially" ) );
4413 }
4414 }
4415 }
4416
4417 /* Check if we have buffered and/or fully reassembled the
4418 * next handshake message. */
4419 hs_buf = &hs->buffering.hs[0];
4420 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4421 {
4422 /* Synthesize a record containing the buffered HS message. */
4423 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4424 ( hs_buf->data[2] << 8 ) |
4425 hs_buf->data[3];
4426
4427 /* Double-check that we haven't accidentally buffered
4428 * a message that doesn't fit into the input buffer. */
4429 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4430 {
4431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4432 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4433 }
4434
4435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4436 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4437 hs_buf->data, msg_len + 12 );
4438
4439 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4440 ssl->in_hslen = msg_len + 12;
4441 ssl->in_msglen = msg_len + 12;
4442 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4443
4444 ret = 0;
4445 goto exit;
4446 }
4447 else
4448 {
4449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4450 hs->in_msg_seq ) );
4451 }
4452
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004453 ret = -1;
4454
4455exit:
4456
4457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4458 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004459}
4460
Hanno Becker01315ea2018-08-21 17:22:17 +01004461static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004462static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4463 size_t desired )
4464{
4465 int offset;
4466 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4467
Hanno Becker01315ea2018-08-21 17:22:17 +01004468 /* Get rid of future records epoch first, if such exist. */
4469 ssl_free_buffered_record( ssl );
4470
4471 /* Check if we have enough space available now. */
4472 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4473 hs->buffering.total_bytes_buffered ) )
4474 {
4475 return( 0 );
4476 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004477
4478 /* We don't have enough space to buffer the next expected
4479 * handshake message. Remove buffers used for future msgs
4480 * to gain space, starting with the most distant one. */
4481 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4482 offset >= 0; offset-- )
4483 {
4484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4485 offset ) );
4486
4487 ssl_buffering_free_slot( ssl, offset );
4488
4489 /* Check if we have enough space available now. */
4490 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4491 hs->buffering.total_bytes_buffered ) )
4492 {
4493 return( 0 );
4494 }
4495 }
4496
4497 return( -1 );
4498}
4499
Hanno Becker40f50842018-08-15 14:48:01 +01004500static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4501{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004502 int ret = 0;
4503 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4504
4505 if( hs == NULL )
4506 return( 0 );
4507
4508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4509
4510 switch( ssl->in_msgtype )
4511 {
4512 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004514
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004515 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004516 break;
4517
4518 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004519 {
4520 unsigned recv_msg_seq_offset;
4521 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4522 mbedtls_ssl_hs_buffer *hs_buf;
4523 size_t msg_len = ssl->in_hslen - 12;
4524
4525 /* We should never receive an old handshake
4526 * message - double-check nonetheless. */
4527 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4528 {
4529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4530 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4531 }
4532
4533 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4534 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4535 {
4536 /* Silently ignore -- message too far in the future */
4537 MBEDTLS_SSL_DEBUG_MSG( 2,
4538 ( "Ignore future HS message with sequence number %u, "
4539 "buffering window %u - %u",
4540 recv_msg_seq, ssl->handshake->in_msg_seq,
4541 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4542
4543 goto exit;
4544 }
4545
4546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4547 recv_msg_seq, recv_msg_seq_offset ) );
4548
4549 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4550
4551 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004552 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004553 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004554 size_t reassembly_buf_sz;
4555
Hanno Becker37f95322018-08-16 13:55:32 +01004556 hs_buf->is_fragmented =
4557 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4558
4559 /* We copy the message back into the input buffer
4560 * after reassembly, so check that it's not too large.
4561 * This is an implementation-specific limitation
4562 * and not one from the standard, hence it is not
4563 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004564 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004565 {
4566 /* Ignore message */
4567 goto exit;
4568 }
4569
Hanno Beckere0b150f2018-08-21 15:51:03 +01004570 /* Check if we have enough space to buffer the message. */
4571 if( hs->buffering.total_bytes_buffered >
4572 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4573 {
4574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4575 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4576 }
4577
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004578 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4579 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004580
4581 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4582 hs->buffering.total_bytes_buffered ) )
4583 {
4584 if( recv_msg_seq_offset > 0 )
4585 {
4586 /* If we can't buffer a future message because
4587 * of space limitations -- ignore. */
4588 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",
4589 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4590 (unsigned) hs->buffering.total_bytes_buffered ) );
4591 goto exit;
4592 }
Hanno Beckere1801392018-08-21 16:51:05 +01004593 else
4594 {
4595 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",
4596 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4597 (unsigned) hs->buffering.total_bytes_buffered ) );
4598 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004599
Hanno Beckera02b0b42018-08-21 17:20:27 +01004600 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004601 {
4602 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 +01004603 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4604 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004605 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4606 goto exit;
4607 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004608 }
4609
4610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4611 msg_len ) );
4612
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004613 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4614 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004615 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004616 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004617 goto exit;
4618 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004619 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004620
4621 /* Prepare final header: copy msg_type, length and message_seq,
4622 * then add standardised fragment_offset and fragment_length */
4623 memcpy( hs_buf->data, ssl->in_msg, 6 );
4624 memset( hs_buf->data + 6, 0, 3 );
4625 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4626
4627 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004628
4629 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004630 }
4631 else
4632 {
4633 /* Make sure msg_type and length are consistent */
4634 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4635 {
4636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4637 /* Ignore */
4638 goto exit;
4639 }
4640 }
4641
Hanno Becker4422bbb2018-08-20 09:40:19 +01004642 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004643 {
4644 size_t frag_len, frag_off;
4645 unsigned char * const msg = hs_buf->data + 12;
4646
4647 /*
4648 * Check and copy current fragment
4649 */
4650
4651 /* Validation of header fields already done in
4652 * mbedtls_ssl_prepare_handshake_record(). */
4653 frag_off = ssl_get_hs_frag_off( ssl );
4654 frag_len = ssl_get_hs_frag_len( ssl );
4655
4656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4657 frag_off, frag_len ) );
4658 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4659
4660 if( hs_buf->is_fragmented )
4661 {
4662 unsigned char * const bitmask = msg + msg_len;
4663 ssl_bitmask_set( bitmask, frag_off, frag_len );
4664 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4665 msg_len ) == 0 );
4666 }
4667 else
4668 {
4669 hs_buf->is_complete = 1;
4670 }
4671
4672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4673 hs_buf->is_complete ? "" : "not yet " ) );
4674 }
4675
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004676 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004677 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004678
4679 default:
4680 break;
4681 }
4682
4683exit:
4684
4685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4686 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004687}
4688#endif /* MBEDTLS_SSL_PROTO_DTLS */
4689
Hanno Becker1097b342018-08-15 14:09:41 +01004690static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004691{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004692 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004693 * Consume last content-layer message and potentially
4694 * update in_msglen which keeps track of the contents'
4695 * consumption state.
4696 *
4697 * (1) Handshake messages:
4698 * Remove last handshake message, move content
4699 * and adapt in_msglen.
4700 *
4701 * (2) Alert messages:
4702 * Consume whole record content, in_msglen = 0.
4703 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004704 * (3) Change cipher spec:
4705 * Consume whole record content, in_msglen = 0.
4706 *
4707 * (4) Application data:
4708 * Don't do anything - the record layer provides
4709 * the application data as a stream transport
4710 * and consumes through mbedtls_ssl_read only.
4711 *
4712 */
4713
4714 /* Case (1): Handshake messages */
4715 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004716 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004717 /* Hard assertion to be sure that no application data
4718 * is in flight, as corrupting ssl->in_msglen during
4719 * ssl->in_offt != NULL is fatal. */
4720 if( ssl->in_offt != NULL )
4721 {
4722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4723 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4724 }
4725
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004726 /*
4727 * Get next Handshake message in the current record
4728 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004729
Hanno Becker4a810fb2017-05-24 16:27:30 +01004730 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004731 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004732 * current handshake content: If DTLS handshake
4733 * fragmentation is used, that's the fragment
4734 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004735 * size here is faulty and should be changed at
4736 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004737 * (2) While it doesn't seem to cause problems, one
4738 * has to be very careful not to assume that in_hslen
4739 * is always <= in_msglen in a sensible communication.
4740 * Again, it's wrong for DTLS handshake fragmentation.
4741 * The following check is therefore mandatory, and
4742 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004743 * Additionally, ssl->in_hslen might be arbitrarily out of
4744 * bounds after handling a DTLS message with an unexpected
4745 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004746 */
4747 if( ssl->in_hslen < ssl->in_msglen )
4748 {
4749 ssl->in_msglen -= ssl->in_hslen;
4750 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4751 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004752
Hanno Becker4a810fb2017-05-24 16:27:30 +01004753 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4754 ssl->in_msg, ssl->in_msglen );
4755 }
4756 else
4757 {
4758 ssl->in_msglen = 0;
4759 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004760
Hanno Becker4a810fb2017-05-24 16:27:30 +01004761 ssl->in_hslen = 0;
4762 }
4763 /* Case (4): Application data */
4764 else if( ssl->in_offt != NULL )
4765 {
4766 return( 0 );
4767 }
4768 /* Everything else (CCS & Alerts) */
4769 else
4770 {
4771 ssl->in_msglen = 0;
4772 }
4773
Hanno Becker1097b342018-08-15 14:09:41 +01004774 return( 0 );
4775}
4776
Hanno Beckere74d5562018-08-15 14:26:08 +01004777static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4778{
4779 if( ssl->in_msglen > 0 )
4780 return( 1 );
4781
4782 return( 0 );
4783}
4784
Hanno Becker5f066e72018-08-16 14:56:31 +01004785#if defined(MBEDTLS_SSL_PROTO_DTLS)
4786
4787static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4788{
4789 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4790 if( hs == NULL )
4791 return;
4792
Hanno Becker01315ea2018-08-21 17:22:17 +01004793 if( hs->buffering.future_record.data != NULL )
4794 {
4795 hs->buffering.total_bytes_buffered -=
4796 hs->buffering.future_record.len;
4797
4798 mbedtls_free( hs->buffering.future_record.data );
4799 hs->buffering.future_record.data = NULL;
4800 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004801}
4802
4803static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4804{
4805 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4806 unsigned char * rec;
4807 size_t rec_len;
4808 unsigned rec_epoch;
4809
4810 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4811 return( 0 );
4812
4813 if( hs == NULL )
4814 return( 0 );
4815
Hanno Becker5f066e72018-08-16 14:56:31 +01004816 rec = hs->buffering.future_record.data;
4817 rec_len = hs->buffering.future_record.len;
4818 rec_epoch = hs->buffering.future_record.epoch;
4819
4820 if( rec == NULL )
4821 return( 0 );
4822
Hanno Becker4cb782d2018-08-20 11:19:05 +01004823 /* Only consider loading future records if the
4824 * input buffer is empty. */
4825 if( ssl_another_record_in_datagram( ssl ) == 1 )
4826 return( 0 );
4827
Hanno Becker5f066e72018-08-16 14:56:31 +01004828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4829
4830 if( rec_epoch != ssl->in_epoch )
4831 {
4832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4833 goto exit;
4834 }
4835
4836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4837
4838 /* Double-check that the record is not too large */
4839 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4840 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4841 {
4842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4843 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4844 }
4845
4846 memcpy( ssl->in_hdr, rec, rec_len );
4847 ssl->in_left = rec_len;
4848 ssl->next_record_offset = 0;
4849
4850 ssl_free_buffered_record( ssl );
4851
4852exit:
4853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4854 return( 0 );
4855}
4856
4857static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4858{
4859 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4860 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01004861 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01004862
4863 /* Don't buffer future records outside handshakes. */
4864 if( hs == NULL )
4865 return( 0 );
4866
4867 /* Only buffer handshake records (we are only interested
4868 * in Finished messages). */
4869 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4870 return( 0 );
4871
4872 /* Don't buffer more than one future epoch record. */
4873 if( hs->buffering.future_record.data != NULL )
4874 return( 0 );
4875
Hanno Becker01315ea2018-08-21 17:22:17 +01004876 /* Don't buffer record if there's not enough buffering space remaining. */
4877 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4878 hs->buffering.total_bytes_buffered ) )
4879 {
4880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
4881 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4882 (unsigned) hs->buffering.total_bytes_buffered ) );
4883 return( 0 );
4884 }
4885
Hanno Becker5f066e72018-08-16 14:56:31 +01004886 /* Buffer record */
4887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4888 ssl->in_epoch + 1 ) );
4889 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4890 rec_hdr_len + ssl->in_msglen );
4891
4892 /* ssl_parse_record_header() only considers records
4893 * of the next epoch as candidates for buffering. */
4894 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01004895 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004896
4897 hs->buffering.future_record.data =
4898 mbedtls_calloc( 1, hs->buffering.future_record.len );
4899 if( hs->buffering.future_record.data == NULL )
4900 {
4901 /* If we run out of RAM trying to buffer a
4902 * record from the next epoch, just ignore. */
4903 return( 0 );
4904 }
4905
Hanno Becker01315ea2018-08-21 17:22:17 +01004906 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01004907
Hanno Becker01315ea2018-08-21 17:22:17 +01004908 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004909 return( 0 );
4910}
4911
4912#endif /* MBEDTLS_SSL_PROTO_DTLS */
4913
Hanno Beckere74d5562018-08-15 14:26:08 +01004914static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004915{
4916 int ret;
4917
Hanno Becker5f066e72018-08-16 14:56:31 +01004918#if defined(MBEDTLS_SSL_PROTO_DTLS)
4919 /* We might have buffered a future record; if so,
4920 * and if the epoch matches now, load it.
4921 * On success, this call will set ssl->in_left to
4922 * the length of the buffered record, so that
4923 * the calls to ssl_fetch_input() below will
4924 * essentially be no-ops. */
4925 ret = ssl_load_buffered_record( ssl );
4926 if( ret != 0 )
4927 return( ret );
4928#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004930 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004933 return( ret );
4934 }
4935
4936 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004938#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004939 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4940 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004941 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004942 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4943 {
4944 ret = ssl_buffer_future_record( ssl );
4945 if( ret != 0 )
4946 return( ret );
4947
4948 /* Fall through to handling of unexpected records */
4949 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4950 }
4951
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004952 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4953 {
4954 /* Skip unexpected record (but not whole datagram) */
4955 ssl->next_record_offset = ssl->in_msglen
4956 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004957
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4959 "(header)" ) );
4960 }
4961 else
4962 {
4963 /* Skip invalid record and the rest of the datagram */
4964 ssl->next_record_offset = 0;
4965 ssl->in_left = 0;
4966
4967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4968 "(header)" ) );
4969 }
4970
4971 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004972 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004973 }
4974#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004975 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004976 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004977
4978 /*
4979 * Read and optionally decrypt the message contents
4980 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004981 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4982 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004984 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004985 return( ret );
4986 }
4987
4988 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004989#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004990 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004992 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004993 if( ssl->next_record_offset < ssl->in_left )
4994 {
4995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4996 }
4997 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004998 else
4999#endif
5000 ssl->in_left = 0;
5001
5002 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005004#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005005 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005006 {
5007 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005008 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5009 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005010 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005011 /* Except when waiting for Finished as a bad mac here
5012 * probably means something went wrong in the handshake
5013 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5014 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5015 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5016 {
5017#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5018 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5019 {
5020 mbedtls_ssl_send_alert_message( ssl,
5021 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5022 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5023 }
5024#endif
5025 return( ret );
5026 }
5027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005028#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005029 if( ssl->conf->badmac_limit != 0 &&
5030 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5033 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005034 }
5035#endif
5036
Hanno Becker4a810fb2017-05-24 16:27:30 +01005037 /* As above, invalid records cause
5038 * dismissal of the whole datagram. */
5039
5040 ssl->next_record_offset = 0;
5041 ssl->in_left = 0;
5042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005044 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005045 }
5046
5047 return( ret );
5048 }
5049 else
5050#endif
5051 {
5052 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005053#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5054 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056 mbedtls_ssl_send_alert_message( ssl,
5057 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5058 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005059 }
5060#endif
5061 return( ret );
5062 }
5063 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005064
Simon Butcher99000142016-10-13 17:21:01 +01005065 return( 0 );
5066}
5067
5068int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5069{
5070 int ret;
5071
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005072 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005073 * Handle particular types of records
5074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005075 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005076 {
Simon Butcher99000142016-10-13 17:21:01 +01005077 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5078 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005079 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005080 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005081 }
5082
Hanno Beckere678eaa2018-08-21 14:57:46 +01005083 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005084 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005085 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005086 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5088 ssl->in_msglen ) );
5089 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005090 }
5091
Hanno Beckere678eaa2018-08-21 14:57:46 +01005092 if( ssl->in_msg[0] != 1 )
5093 {
5094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5095 ssl->in_msg[0] ) );
5096 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5097 }
5098
5099#if defined(MBEDTLS_SSL_PROTO_DTLS)
5100 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5101 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5102 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5103 {
5104 if( ssl->handshake == NULL )
5105 {
5106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5107 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5108 }
5109
5110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5111 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5112 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005113#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005114 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005116 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005117 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005118 if( ssl->in_msglen != 2 )
5119 {
5120 /* Note: Standard allows for more than one 2 byte alert
5121 to be packed in a single message, but Mbed TLS doesn't
5122 currently support this. */
5123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5124 ssl->in_msglen ) );
5125 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5126 }
5127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005129 ssl->in_msg[0], ssl->in_msg[1] ) );
5130
5131 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005132 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005134 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005137 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005138 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005139 }
5140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005141 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5142 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5145 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005146 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005147
5148#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5149 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5150 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5151 {
Hanno Becker90333da2017-10-10 11:27:13 +01005152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005153 /* Will be handled when trying to parse ServerHello */
5154 return( 0 );
5155 }
5156#endif
5157
5158#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5159 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5160 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5161 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5162 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5163 {
5164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5165 /* Will be handled in mbedtls_ssl_parse_certificate() */
5166 return( 0 );
5167 }
5168#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5169
5170 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005171 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005172 }
5173
Hanno Beckerc76c6192017-06-06 10:03:17 +01005174#if defined(MBEDTLS_SSL_PROTO_DTLS)
5175 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5176 ssl->handshake != NULL &&
5177 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5178 {
5179 ssl_handshake_wrapup_free_hs_transform( ssl );
5180 }
5181#endif
5182
Paul Bakker5121ce52009-01-03 21:22:43 +00005183 return( 0 );
5184}
5185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005186int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005187{
5188 int ret;
5189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005190 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5191 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5192 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005193 {
5194 return( ret );
5195 }
5196
5197 return( 0 );
5198}
5199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005201 unsigned char level,
5202 unsigned char message )
5203{
5204 int ret;
5205
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005206 if( ssl == NULL || ssl->conf == NULL )
5207 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005213 ssl->out_msglen = 2;
5214 ssl->out_msg[0] = level;
5215 ssl->out_msg[1] = message;
5216
Hanno Becker67bc7c32018-08-06 11:33:50 +01005217 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005219 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005220 return( ret );
5221 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005223
5224 return( 0 );
5225}
5226
Paul Bakker5121ce52009-01-03 21:22:43 +00005227/*
5228 * Handshake functions
5229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005230#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5231 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5232 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5233 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5234 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5235 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5236 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005237/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005238int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005240 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005244 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5245 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005246 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5247 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005250 ssl->state++;
5251 return( 0 );
5252 }
5253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5255 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005256}
5257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005258int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005259{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005260 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005262 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5265 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005266 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5267 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005270 ssl->state++;
5271 return( 0 );
5272 }
5273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5275 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005276}
Gilles Peskinef9828522017-05-03 12:28:43 +02005277
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005278#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005279/* Some certificate support -> implement write and parse */
5280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005281int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005282{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005284 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285 const mbedtls_x509_crt *crt;
5286 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005290 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5291 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005292 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5293 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005296 ssl->state++;
5297 return( 0 );
5298 }
5299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005300#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005301 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005302 {
5303 if( ssl->client_auth == 0 )
5304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005306 ssl->state++;
5307 return( 0 );
5308 }
5309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005310#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005311 /*
5312 * If using SSLv3 and got no cert, send an Alert message
5313 * (otherwise an empty Certificate message will be sent).
5314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005315 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5316 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005317 {
5318 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5320 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5321 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005324 goto write_msg;
5325 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005327 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328#endif /* MBEDTLS_SSL_CLI_C */
5329#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005330 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5335 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005336 }
5337 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005338#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005340 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005341
5342 /*
5343 * 0 . 0 handshake type
5344 * 1 . 3 handshake length
5345 * 4 . 6 length of all certs
5346 * 7 . 9 length of cert. 1
5347 * 10 . n-1 peer certificate
5348 * n . n+2 length of cert. 2
5349 * n+3 . ... upper level cert, etc.
5350 */
5351 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005352 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005353
Paul Bakker29087132010-03-21 21:03:34 +00005354 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005355 {
5356 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005357 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005360 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005362 }
5363
5364 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5365 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5366 ssl->out_msg[i + 2] = (unsigned char)( n );
5367
5368 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5369 i += n; crt = crt->next;
5370 }
5371
5372 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5373 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5374 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5375
5376 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5378 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005379
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005380#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005381write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005382#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005383
5384 ssl->state++;
5385
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005386 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005387 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005388 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005389 return( ret );
5390 }
5391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005393
Paul Bakkered27a042013-04-18 22:46:23 +02005394 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005395}
5396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00005400 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005402 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005403 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5408 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005409 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5410 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005413 ssl->state++;
5414 return( 0 );
5415 }
5416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005417#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005418 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005419 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5420 {
5421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5422 ssl->state++;
5423 return( 0 );
5424 }
5425
5426#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5427 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
5428 authmode = ssl->handshake->sni_authmode;
5429#endif
5430
5431 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5432 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005433 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005434 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005436 ssl->state++;
5437 return( 0 );
5438 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005439#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005440
Hanno Becker327c93b2018-08-15 13:56:18 +01005441 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005442 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005443 /* mbedtls_ssl_read_record may have sent an alert already. We
5444 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005446 return( ret );
5447 }
5448
5449 ssl->state++;
5450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451#if defined(MBEDTLS_SSL_SRV_C)
5452#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005453 /*
5454 * Check if the client sent an empty certificate
5455 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005456 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005457 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005458 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005459 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5461 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5462 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005465
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005466 /* The client was asked for a certificate but didn't send
5467 one. The client should know what's going on, so we
5468 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005469 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005470 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005471 return( 0 );
5472 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005474 }
5475 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5479 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005480 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005481 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005483 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5484 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5485 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5486 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005489
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005490 /* The client was asked for a certificate but didn't send
5491 one. The client should know what's going on, so we
5492 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005493 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005494 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005495 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005496 else
5497 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005498 }
5499 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005500#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5501 MBEDTLS_SSL_PROTO_TLS1_2 */
5502#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005507 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5508 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005510 }
5511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5513 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005516 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5517 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005518 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005519 }
5520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005522
Paul Bakker5121ce52009-01-03 21:22:43 +00005523 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005525 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005526 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005527
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005528 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005532 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5533 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005534 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005535 }
5536
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005537 /* In case we tried to reuse a session but it failed */
5538 if( ssl->session_negotiate->peer_cert != NULL )
5539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005540 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5541 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005542 }
5543
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005544 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005545 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005546 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005549 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5550 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005551 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005552 }
5553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005554 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005555
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005556 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005557
5558 while( i < ssl->in_hslen )
5559 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005560 if ( i + 3 > ssl->in_hslen ) {
5561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5562 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5563 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5564 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5565 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005566 if( ssl->in_msg[i] != 0 )
5567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005569 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5570 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005572 }
5573
5574 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5575 | (unsigned int) ssl->in_msg[i + 2];
5576 i += 3;
5577
5578 if( n < 128 || i + n > ssl->in_hslen )
5579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005581 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5582 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005584 }
5585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005586 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005587 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005588 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005589 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005590 case 0: /*ok*/
5591 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5592 /* Ignore certificate with an unknown algorithm: maybe a
5593 prior certificate was already trusted. */
5594 break;
5595
5596 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5597 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5598 goto crt_parse_der_failed;
5599
5600 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5601 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5602 goto crt_parse_der_failed;
5603
5604 default:
5605 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5606 crt_parse_der_failed:
5607 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005609 return( ret );
5610 }
5611
5612 i += n;
5613 }
5614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005616
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005617 /*
5618 * On client, make sure the server cert doesn't change during renego to
5619 * avoid "triple handshake" attack: https://secure-resumption.com/
5620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005622 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005623 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005624 {
5625 if( ssl->session->peer_cert == NULL )
5626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005628 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5629 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005631 }
5632
5633 if( ssl->session->peer_cert->raw.len !=
5634 ssl->session_negotiate->peer_cert->raw.len ||
5635 memcmp( ssl->session->peer_cert->raw.p,
5636 ssl->session_negotiate->peer_cert->raw.p,
5637 ssl->session->peer_cert->raw.len ) != 0 )
5638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005640 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5641 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005642 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005643 }
5644 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005646
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005647 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005648 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005649 mbedtls_x509_crt *ca_chain;
5650 mbedtls_x509_crl *ca_crl;
5651
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005652#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005653 if( ssl->handshake->sni_ca_chain != NULL )
5654 {
5655 ca_chain = ssl->handshake->sni_ca_chain;
5656 ca_crl = ssl->handshake->sni_ca_crl;
5657 }
5658 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005659#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005660 {
5661 ca_chain = ssl->conf->ca_chain;
5662 ca_crl = ssl->conf->ca_crl;
5663 }
5664
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005665 /*
5666 * Main check: verify certificate
5667 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005668 ret = mbedtls_x509_crt_verify_with_profile(
5669 ssl->session_negotiate->peer_cert,
5670 ca_chain, ca_crl,
5671 ssl->conf->cert_profile,
5672 ssl->hostname,
5673 &ssl->session_negotiate->verify_result,
5674 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005675
5676 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005678 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005679 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005680
5681 /*
5682 * Secondary checks: always done, but change 'ret' only if it was 0
5683 */
5684
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005685#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005688
5689 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005690 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005691 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005692 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005693 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005696 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005697 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005698 }
5699 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005700#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005703 ciphersuite_info,
5704 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005705 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005708 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005710 }
5711
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005712 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5713 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5714 * with details encoded in the verification flags. All other kinds
5715 * of error codes, including those from the user provided f_vrfy
5716 * functions, are treated as fatal and lead to a failure of
5717 * ssl_parse_certificate even if verification was optional. */
5718 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5719 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5720 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5721 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005722 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005723 }
5724
5725 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5726 {
5727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5728 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5729 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005730
5731 if( ret != 0 )
5732 {
5733 /* The certificate may have been rejected for several reasons.
5734 Pick one and send the corresponding alert. Which alert to send
5735 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005736 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5737 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5738 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5739 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5740 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5741 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5742 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5743 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5744 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5745 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5746 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5747 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5748 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5749 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5750 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5751 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5752 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5753 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5754 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5755 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005756 else
5757 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005758 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5759 alert );
5760 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005761
Hanno Beckere6706e62017-05-15 16:05:15 +01005762#if defined(MBEDTLS_DEBUG_C)
5763 if( ssl->session_negotiate->verify_result != 0 )
5764 {
5765 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5766 ssl->session_negotiate->verify_result ) );
5767 }
5768 else
5769 {
5770 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5771 }
5772#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005773 }
5774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005776
5777 return( ret );
5778}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5780 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5781 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5782 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5783 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5784 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5785 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005787int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005788{
5789 int ret;
5790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005794 ssl->out_msglen = 1;
5795 ssl->out_msg[0] = 1;
5796
Paul Bakker5121ce52009-01-03 21:22:43 +00005797 ssl->state++;
5798
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005799 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005800 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005801 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005802 return( ret );
5803 }
5804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005806
5807 return( 0 );
5808}
5809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005811{
5812 int ret;
5813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005815
Hanno Becker327c93b2018-08-15 13:56:18 +01005816 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005819 return( ret );
5820 }
5821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005822 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005825 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5826 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005828 }
5829
Hanno Beckere678eaa2018-08-21 14:57:46 +01005830 /* CCS records are only accepted if they have length 1 and content '1',
5831 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005832
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005833 /*
5834 * Switch to our negotiated transform and session parameters for inbound
5835 * data.
5836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005838 ssl->transform_in = ssl->transform_negotiate;
5839 ssl->session_in = ssl->session_negotiate;
5840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005841#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005842 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005845 ssl_dtls_replay_reset( ssl );
5846#endif
5847
5848 /* Increment epoch */
5849 if( ++ssl->in_epoch == 0 )
5850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005852 /* This is highly unlikely to happen for legitimate reasons, so
5853 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005854 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005855 }
5856 }
5857 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005858#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005859 memset( ssl->in_ctr, 0, 8 );
5860
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005861 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5864 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005866 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005869 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5870 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005871 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005872 }
5873 }
5874#endif
5875
Paul Bakker5121ce52009-01-03 21:22:43 +00005876 ssl->state++;
5877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005879
5880 return( 0 );
5881}
5882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5884 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005885{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005886 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5889 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5890 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005891 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005892 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005893#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5895#if defined(MBEDTLS_SHA512_C)
5896 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005897 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5898 else
5899#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900#if defined(MBEDTLS_SHA256_C)
5901 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005902 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005903 else
5904#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005905#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005908 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005909 }
Paul Bakker380da532012-04-18 16:10:25 +00005910}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005913{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005914#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5915 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005916 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5917 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005918#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005919#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5920#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005921 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005922#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005923#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005924 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005925#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005926#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005927}
5928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005929static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005930 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005931{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005932#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5933 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005934 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5935 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005936#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005937#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5938#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005939 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005940#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005941#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005942 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005943#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005945}
5946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5948 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5949static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005950 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005951{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005952 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5953 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005954}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005955#endif
Paul Bakker380da532012-04-18 16:10:25 +00005956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005957#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5958#if defined(MBEDTLS_SHA256_C)
5959static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005960 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005961{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005962 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005963}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005964#endif
Paul Bakker380da532012-04-18 16:10:25 +00005965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966#if defined(MBEDTLS_SHA512_C)
5967static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005968 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005969{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005970 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005971}
Paul Bakker769075d2012-11-24 11:26:46 +01005972#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005973#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005976static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005978{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005979 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005980 mbedtls_md5_context md5;
5981 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005982
Paul Bakker5121ce52009-01-03 21:22:43 +00005983 unsigned char padbuf[48];
5984 unsigned char md5sum[16];
5985 unsigned char sha1sum[20];
5986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005988 if( !session )
5989 session = ssl->session;
5990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005992
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005993 mbedtls_md5_init( &md5 );
5994 mbedtls_sha1_init( &sha1 );
5995
5996 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5997 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005998
5999 /*
6000 * SSLv3:
6001 * hash =
6002 * MD5( master + pad2 +
6003 * MD5( handshake + sender + master + pad1 ) )
6004 * + SHA1( master + pad2 +
6005 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006006 */
6007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006009 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6010 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006011#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006014 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6015 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006016#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006019 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006020
Paul Bakker1ef83d62012-04-11 12:09:53 +00006021 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006022
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006023 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6024 mbedtls_md5_update_ret( &md5, session->master, 48 );
6025 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6026 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006027
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006028 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6029 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6030 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6031 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006032
Paul Bakker1ef83d62012-04-11 12:09:53 +00006033 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006034
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006035 mbedtls_md5_starts_ret( &md5 );
6036 mbedtls_md5_update_ret( &md5, session->master, 48 );
6037 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6038 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6039 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006040
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006041 mbedtls_sha1_starts_ret( &sha1 );
6042 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6043 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6044 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6045 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006048
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006049 mbedtls_md5_free( &md5 );
6050 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006051
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006052 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6053 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6054 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006057}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006061static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006063{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006064 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006065 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006066 mbedtls_md5_context md5;
6067 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006068 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006071 if( !session )
6072 session = ssl->session;
6073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006075
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006076 mbedtls_md5_init( &md5 );
6077 mbedtls_sha1_init( &sha1 );
6078
6079 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6080 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006081
Paul Bakker1ef83d62012-04-11 12:09:53 +00006082 /*
6083 * TLSv1:
6084 * hash = PRF( master, finished_label,
6085 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6086 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006088#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006089 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6090 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006091#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006094 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6095 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006096#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006098 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006099 ? "client finished"
6100 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006101
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006102 mbedtls_md5_finish_ret( &md5, padbuf );
6103 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006104
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006105 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006106 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006109
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006110 mbedtls_md5_free( &md5 );
6111 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006112
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006113 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006116}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006117#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006119#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6120#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006121static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006122 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006123{
6124 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006125 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006126 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006127 unsigned char padbuf[32];
6128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006129 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006130 if( !session )
6131 session = ssl->session;
6132
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006133 mbedtls_sha256_init( &sha256 );
6134
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006136
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006137 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006138
6139 /*
6140 * TLSv1.2:
6141 * hash = PRF( master, finished_label,
6142 * Hash( handshake ) )[0.11]
6143 */
6144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145#if !defined(MBEDTLS_SHA256_ALT)
6146 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006147 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006148#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006151 ? "client finished"
6152 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006153
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006154 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006155
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006156 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006157 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006160
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006161 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006162
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006163 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006169#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006170static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006171 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006172{
6173 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006174 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006175 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006176 unsigned char padbuf[48];
6177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006179 if( !session )
6180 session = ssl->session;
6181
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006182 mbedtls_sha512_init( &sha512 );
6183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006185
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006186 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006187
6188 /*
6189 * TLSv1.2:
6190 * hash = PRF( master, finished_label,
6191 * Hash( handshake ) )[0.11]
6192 */
6193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006195 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6196 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006197#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006200 ? "client finished"
6201 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006202
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006203 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006204
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006205 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006206 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006208 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006209
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006210 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006211
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006212 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006215}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006216#endif /* MBEDTLS_SHA512_C */
6217#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006219static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006220{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006222
6223 /*
6224 * Free our handshake params
6225 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006226 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006228 ssl->handshake = NULL;
6229
6230 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006231 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006232 */
6233 if( ssl->transform )
6234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 mbedtls_ssl_transform_free( ssl->transform );
6236 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006237 }
6238 ssl->transform = ssl->transform_negotiate;
6239 ssl->transform_negotiate = NULL;
6240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006241 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006242}
6243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006245{
6246 int resume = ssl->handshake->resume;
6247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006250#if defined(MBEDTLS_SSL_RENEGOTIATION)
6251 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006253 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006254 ssl->renego_records_seen = 0;
6255 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006256#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006257
6258 /*
6259 * Free the previous session and switch in the current one
6260 */
Paul Bakker0a597072012-09-25 21:55:46 +00006261 if( ssl->session )
6262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006263#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006264 /* RFC 7366 3.1: keep the EtM state */
6265 ssl->session_negotiate->encrypt_then_mac =
6266 ssl->session->encrypt_then_mac;
6267#endif
6268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269 mbedtls_ssl_session_free( ssl->session );
6270 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006271 }
6272 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006273 ssl->session_negotiate = NULL;
6274
Paul Bakker0a597072012-09-25 21:55:46 +00006275 /*
6276 * Add cache entry
6277 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006278 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006279 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006280 resume == 0 )
6281 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006282 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006284 }
Paul Bakker0a597072012-09-25 21:55:46 +00006285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006286#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006287 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006288 ssl->handshake->flight != NULL )
6289 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006290 /* Cancel handshake timer */
6291 ssl_set_timer( ssl, 0 );
6292
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006293 /* Keep last flight around in case we need to resend it:
6294 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006295 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006296 }
6297 else
6298#endif
6299 ssl_handshake_wrapup_free_hs_transform( ssl );
6300
Paul Bakker48916f92012-09-16 19:57:18 +00006301 ssl->state++;
6302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006304}
6305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006307{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006308 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006311
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006312 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006313
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006314 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006315
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006316 /*
6317 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6318 * may define some other value. Currently (early 2016), no defined
6319 * ciphersuite does this (and this is unlikely to change as activity has
6320 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006322 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006324#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006325 ssl->verify_data_len = hash_len;
6326 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006327#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006328
Paul Bakker5121ce52009-01-03 21:22:43 +00006329 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6331 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006332
6333 /*
6334 * In case of session resuming, invert the client and server
6335 * ChangeCipherSpec messages order.
6336 */
Paul Bakker0a597072012-09-25 21:55:46 +00006337 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006340 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006342#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006343#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006344 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006345 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006346#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006347 }
6348 else
6349 ssl->state++;
6350
Paul Bakker48916f92012-09-16 19:57:18 +00006351 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006352 * Switch to our negotiated transform and session parameters for outbound
6353 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006357#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006358 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006359 {
6360 unsigned char i;
6361
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006362 /* Remember current epoch settings for resending */
6363 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006364 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006365
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006366 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006367 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006368
6369 /* Increment epoch */
6370 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006371 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006372 break;
6373
6374 /* The loop goes to its end iff the counter is wrapping */
6375 if( i == 0 )
6376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6378 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006379 }
6380 }
6381 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006382#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006383 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006384
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006385 ssl->transform_out = ssl->transform_negotiate;
6386 ssl->session_out = ssl->session_negotiate;
6387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006388#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6389 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006393 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6394 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006395 }
6396 }
6397#endif
6398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006399#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006400 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006401 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006402#endif
6403
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006404 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006405 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006407 return( ret );
6408 }
6409
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006410#if defined(MBEDTLS_SSL_PROTO_DTLS)
6411 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6412 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6413 {
6414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6415 return( ret );
6416 }
6417#endif
6418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006420
6421 return( 0 );
6422}
6423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006424#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006425#define SSL_MAX_HASH_LEN 36
6426#else
6427#define SSL_MAX_HASH_LEN 12
6428#endif
6429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006430int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006431{
Paul Bakker23986e52011-04-24 08:57:21 +00006432 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006433 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006434 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006437
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006438 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006439
Hanno Becker327c93b2018-08-15 13:56:18 +01006440 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006443 return( ret );
6444 }
6445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006446 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006449 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6450 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006451 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006452 }
6453
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006454 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455#if defined(MBEDTLS_SSL_PROTO_SSL3)
6456 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006457 hash_len = 36;
6458 else
6459#endif
6460 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006462 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6463 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006466 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6467 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006468 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006469 }
6470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006472 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006475 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6476 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006478 }
6479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006481 ssl->verify_data_len = hash_len;
6482 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006483#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006484
Paul Bakker0a597072012-09-25 21:55:46 +00006485 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006488 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006490#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006492 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006494#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006495 }
6496 else
6497 ssl->state++;
6498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006500 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006501 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006502#endif
6503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006505
6506 return( 0 );
6507}
6508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006510{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6514 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6515 mbedtls_md5_init( &handshake->fin_md5 );
6516 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006517 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6518 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006519#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006520#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6521#if defined(MBEDTLS_SHA256_C)
6522 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006523 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006524#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525#if defined(MBEDTLS_SHA512_C)
6526 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006527 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006528#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006529#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006530
6531 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006532
6533#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6534 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6535 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6536#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538#if defined(MBEDTLS_DHM_C)
6539 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006540#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541#if defined(MBEDTLS_ECDH_C)
6542 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006543#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006544#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006545 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006546#if defined(MBEDTLS_SSL_CLI_C)
6547 handshake->ecjpake_cache = NULL;
6548 handshake->ecjpake_cache_len = 0;
6549#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006550#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006551
6552#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6553 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6554#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006555}
6556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006558{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6562 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564 mbedtls_md_init( &transform->md_ctx_enc );
6565 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006566}
6567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006569{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006571}
6572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006574{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006575 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006576 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006578 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006580 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006581 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006582
6583 /*
6584 * Either the pointers are now NULL or cleared properly and can be freed.
6585 * Now allocate missing structures.
6586 */
6587 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006588 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006589 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006590 }
Paul Bakker48916f92012-09-16 19:57:18 +00006591
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006592 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006593 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006594 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006595 }
Paul Bakker48916f92012-09-16 19:57:18 +00006596
Paul Bakker82788fb2014-10-20 13:59:19 +02006597 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006598 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006599 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006600 }
Paul Bakker48916f92012-09-16 19:57:18 +00006601
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006602 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006603 if( ssl->handshake == NULL ||
6604 ssl->transform_negotiate == NULL ||
6605 ssl->session_negotiate == NULL )
6606 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006609 mbedtls_free( ssl->handshake );
6610 mbedtls_free( ssl->transform_negotiate );
6611 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006612
6613 ssl->handshake = NULL;
6614 ssl->transform_negotiate = NULL;
6615 ssl->session_negotiate = NULL;
6616
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006617 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006618 }
6619
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006620 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006622 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006623 ssl_handshake_params_init( ssl->handshake );
6624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006626 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6627 {
6628 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006629
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006630 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6631 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6632 else
6633 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006634
6635 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006636 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006637#endif
6638
Paul Bakker48916f92012-09-16 19:57:18 +00006639 return( 0 );
6640}
6641
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006642#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006643/* Dummy cookie callbacks for defaults */
6644static int ssl_cookie_write_dummy( void *ctx,
6645 unsigned char **p, unsigned char *end,
6646 const unsigned char *cli_id, size_t cli_id_len )
6647{
6648 ((void) ctx);
6649 ((void) p);
6650 ((void) end);
6651 ((void) cli_id);
6652 ((void) cli_id_len);
6653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006655}
6656
6657static int ssl_cookie_check_dummy( void *ctx,
6658 const unsigned char *cookie, size_t cookie_len,
6659 const unsigned char *cli_id, size_t cli_id_len )
6660{
6661 ((void) ctx);
6662 ((void) cookie);
6663 ((void) cookie_len);
6664 ((void) cli_id);
6665 ((void) cli_id_len);
6666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006668}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006669#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006670
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006671/* Once ssl->out_hdr as the address of the beginning of the
6672 * next outgoing record is set, deduce the other pointers.
6673 *
6674 * Note: For TLS, we save the implicit record sequence number
6675 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6676 * and the caller has to make sure there's space for this.
6677 */
6678
6679static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6680 mbedtls_ssl_transform *transform )
6681{
6682#if defined(MBEDTLS_SSL_PROTO_DTLS)
6683 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6684 {
6685 ssl->out_ctr = ssl->out_hdr + 3;
6686 ssl->out_len = ssl->out_hdr + 11;
6687 ssl->out_iv = ssl->out_hdr + 13;
6688 }
6689 else
6690#endif
6691 {
6692 ssl->out_ctr = ssl->out_hdr - 8;
6693 ssl->out_len = ssl->out_hdr + 3;
6694 ssl->out_iv = ssl->out_hdr + 5;
6695 }
6696
6697 /* Adjust out_msg to make space for explicit IV, if used. */
6698 if( transform != NULL &&
6699 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6700 {
6701 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6702 }
6703 else
6704 ssl->out_msg = ssl->out_iv;
6705}
6706
6707/* Once ssl->in_hdr as the address of the beginning of the
6708 * next incoming record is set, deduce the other pointers.
6709 *
6710 * Note: For TLS, we save the implicit record sequence number
6711 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6712 * and the caller has to make sure there's space for this.
6713 */
6714
6715static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6716 mbedtls_ssl_transform *transform )
6717{
6718#if defined(MBEDTLS_SSL_PROTO_DTLS)
6719 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6720 {
6721 ssl->in_ctr = ssl->in_hdr + 3;
6722 ssl->in_len = ssl->in_hdr + 11;
6723 ssl->in_iv = ssl->in_hdr + 13;
6724 }
6725 else
6726#endif
6727 {
6728 ssl->in_ctr = ssl->in_hdr - 8;
6729 ssl->in_len = ssl->in_hdr + 3;
6730 ssl->in_iv = ssl->in_hdr + 5;
6731 }
6732
6733 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6734 if( transform != NULL &&
6735 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6736 {
6737 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6738 }
6739 else
6740 ssl->in_msg = ssl->in_iv;
6741}
6742
Paul Bakker5121ce52009-01-03 21:22:43 +00006743/*
6744 * Initialize an SSL context
6745 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006746void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6747{
6748 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6749}
6750
6751/*
6752 * Setup an SSL context
6753 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006754
6755static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6756{
6757 /* Set the incoming and outgoing record pointers. */
6758#if defined(MBEDTLS_SSL_PROTO_DTLS)
6759 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6760 {
6761 ssl->out_hdr = ssl->out_buf;
6762 ssl->in_hdr = ssl->in_buf;
6763 }
6764 else
6765#endif /* MBEDTLS_SSL_PROTO_DTLS */
6766 {
6767 ssl->out_hdr = ssl->out_buf + 8;
6768 ssl->in_hdr = ssl->in_buf + 8;
6769 }
6770
6771 /* Derive other internal pointers. */
6772 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6773 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6774}
6775
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006776int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006777 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006778{
Paul Bakker48916f92012-09-16 19:57:18 +00006779 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006780
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006781 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006782
6783 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006784 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006785 */
Angus Grattond8213d02016-05-25 20:56:48 +10006786 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6787 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006788 {
Angus Grattond8213d02016-05-25 20:56:48 +10006789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6790 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6791 }
6792
6793 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6794 if( ssl->out_buf == NULL )
6795 {
6796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006798 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006799 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006800 }
6801
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006802 ssl_reset_in_out_pointers( ssl );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006803
Paul Bakker48916f92012-09-16 19:57:18 +00006804 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6805 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006806
6807 return( 0 );
6808}
6809
6810/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006811 * Reset an initialized and used SSL context for re-use while retaining
6812 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006813 *
6814 * If partial is non-zero, keep data in the input buffer and client ID.
6815 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006816 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006817static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006818{
Paul Bakker48916f92012-09-16 19:57:18 +00006819 int ret;
6820
Hanno Becker7e772132018-08-10 12:38:21 +01006821#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6822 !defined(MBEDTLS_SSL_SRV_C)
6823 ((void) partial);
6824#endif
6825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006827
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006828 /* Cancel any possibly running timer */
6829 ssl_set_timer( ssl, 0 );
6830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831#if defined(MBEDTLS_SSL_RENEGOTIATION)
6832 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006833 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006834
6835 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6837 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006838#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006840
Paul Bakker7eb013f2011-10-06 12:37:39 +00006841 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006842 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006843
6844 ssl->in_msgtype = 0;
6845 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006847 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006848 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006849#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006851 ssl_dtls_replay_reset( ssl );
6852#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006853
6854 ssl->in_hslen = 0;
6855 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006856
6857 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006858
6859 ssl->out_msgtype = 0;
6860 ssl->out_msglen = 0;
6861 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006862#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6863 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006864 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006865#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006866
Hanno Becker19859472018-08-06 09:40:20 +01006867 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6868
Paul Bakker48916f92012-09-16 19:57:18 +00006869 ssl->transform_in = NULL;
6870 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006871
Hanno Becker78640902018-08-13 16:35:15 +01006872 ssl->session_in = NULL;
6873 ssl->session_out = NULL;
6874
Angus Grattond8213d02016-05-25 20:56:48 +10006875 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006876
6877#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006878 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006879#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6880 {
6881 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006882 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006883 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6886 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6889 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6892 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006893 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006894 }
6895#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006896
Paul Bakker48916f92012-09-16 19:57:18 +00006897 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899 mbedtls_ssl_transform_free( ssl->transform );
6900 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006901 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006902 }
Paul Bakker48916f92012-09-16 19:57:18 +00006903
Paul Bakkerc0463502013-02-14 11:19:38 +01006904 if( ssl->session )
6905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906 mbedtls_ssl_session_free( ssl->session );
6907 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006908 ssl->session = NULL;
6909 }
6910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006912 ssl->alpn_chosen = NULL;
6913#endif
6914
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006915#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006916#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006917 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006918#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006919 {
6920 mbedtls_free( ssl->cli_id );
6921 ssl->cli_id = NULL;
6922 ssl->cli_id_len = 0;
6923 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006924#endif
6925
Paul Bakker48916f92012-09-16 19:57:18 +00006926 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6927 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006928
6929 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006930}
6931
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006932/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006933 * Reset an initialized and used SSL context for re-use while retaining
6934 * all application-set variables, function pointers and data.
6935 */
6936int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6937{
6938 return( ssl_session_reset_int( ssl, 0 ) );
6939}
6940
6941/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006942 * SSL set accessors
6943 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006944void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006945{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006946 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006947}
6948
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006949void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006950{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006951 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006952}
6953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006954#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006955void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006956{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006957 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006958}
6959#endif
6960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006962void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006963{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006964 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006965}
6966#endif
6967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01006969
6970void mbedtls_ssl_conf_datagram_packing( mbedtls_ssl_context *ssl,
6971 unsigned allow_packing )
6972{
6973 ssl->disable_datagram_packing = !allow_packing;
6974}
6975
6976void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
6977 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006978{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006979 conf->hs_timeout_min = min;
6980 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006981}
6982#endif
6983
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006984void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006985{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006986 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006987}
6988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006990void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006991 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006992 void *p_vrfy )
6993{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006994 conf->f_vrfy = f_vrfy;
6995 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006996}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006998
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006999void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007000 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007001 void *p_rng )
7002{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007003 conf->f_rng = f_rng;
7004 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007005}
7006
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007007void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007008 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007009 void *p_dbg )
7010{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007011 conf->f_dbg = f_dbg;
7012 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007013}
7014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007015void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007016 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007017 mbedtls_ssl_send_t *f_send,
7018 mbedtls_ssl_recv_t *f_recv,
7019 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007020{
7021 ssl->p_bio = p_bio;
7022 ssl->f_send = f_send;
7023 ssl->f_recv = f_recv;
7024 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007025}
7026
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007027#if defined(MBEDTLS_SSL_PROTO_DTLS)
7028void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7029{
7030 ssl->mtu = mtu;
7031}
7032#endif
7033
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007034void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007035{
7036 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007037}
7038
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007039void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7040 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007041 mbedtls_ssl_set_timer_t *f_set_timer,
7042 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007043{
7044 ssl->p_timer = p_timer;
7045 ssl->f_set_timer = f_set_timer;
7046 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007047
7048 /* Make sure we start with no timer running */
7049 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007050}
7051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007053void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007054 void *p_cache,
7055 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7056 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007057{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007058 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007059 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007060 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007061}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064#if defined(MBEDTLS_SSL_CLI_C)
7065int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007066{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007067 int ret;
7068
7069 if( ssl == NULL ||
7070 session == NULL ||
7071 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007072 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007074 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007075 }
7076
7077 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7078 return( ret );
7079
Paul Bakker0a597072012-09-25 21:55:46 +00007080 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007081
7082 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007083}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007085
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007086void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007087 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007088{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007089 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7090 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7091 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7092 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007093}
7094
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007095void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007096 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007097 int major, int minor )
7098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007100 return;
7101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007103 return;
7104
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007105 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007106}
7107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007109void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007110 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007111{
7112 conf->cert_profile = profile;
7113}
7114
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007115/* Append a new keycert entry to a (possibly empty) list */
7116static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7117 mbedtls_x509_crt *cert,
7118 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007119{
niisato8ee24222018-06-25 19:05:48 +09007120 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007121
niisato8ee24222018-06-25 19:05:48 +09007122 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7123 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007124 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007125
niisato8ee24222018-06-25 19:05:48 +09007126 new_cert->cert = cert;
7127 new_cert->key = key;
7128 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007129
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007130 /* Update head is the list was null, else add to the end */
7131 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007132 {
niisato8ee24222018-06-25 19:05:48 +09007133 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007134 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007135 else
7136 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007137 mbedtls_ssl_key_cert *cur = *head;
7138 while( cur->next != NULL )
7139 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007140 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007141 }
7142
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007143 return( 0 );
7144}
7145
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007146int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007147 mbedtls_x509_crt *own_cert,
7148 mbedtls_pk_context *pk_key )
7149{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007150 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007151}
7152
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007153void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007154 mbedtls_x509_crt *ca_chain,
7155 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007156{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007157 conf->ca_chain = ca_chain;
7158 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007161
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007162#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7163int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7164 mbedtls_x509_crt *own_cert,
7165 mbedtls_pk_context *pk_key )
7166{
7167 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7168 own_cert, pk_key ) );
7169}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007170
7171void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7172 mbedtls_x509_crt *ca_chain,
7173 mbedtls_x509_crl *ca_crl )
7174{
7175 ssl->handshake->sni_ca_chain = ca_chain;
7176 ssl->handshake->sni_ca_crl = ca_crl;
7177}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007178
7179void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7180 int authmode )
7181{
7182 ssl->handshake->sni_authmode = authmode;
7183}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007184#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7185
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007186#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007187/*
7188 * Set EC J-PAKE password for current handshake
7189 */
7190int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7191 const unsigned char *pw,
7192 size_t pw_len )
7193{
7194 mbedtls_ecjpake_role role;
7195
Janos Follath8eb64132016-06-03 15:40:57 +01007196 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007197 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7198
7199 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7200 role = MBEDTLS_ECJPAKE_SERVER;
7201 else
7202 role = MBEDTLS_ECJPAKE_CLIENT;
7203
7204 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7205 role,
7206 MBEDTLS_MD_SHA256,
7207 MBEDTLS_ECP_DP_SECP256R1,
7208 pw, pw_len ) );
7209}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007210#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007213int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007214 const unsigned char *psk, size_t psk_len,
7215 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007216{
Paul Bakker6db455e2013-09-18 17:29:31 +02007217 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7221 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007222
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007223 /* Identity len will be encoded on two bytes */
7224 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007225 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007226 {
7227 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7228 }
7229
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007230 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007231 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007232 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007233
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007234 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007235 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007236 conf->psk_len = 0;
7237 }
7238 if( conf->psk_identity != NULL )
7239 {
7240 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007241 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007242 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007243 }
7244
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007245 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7246 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007247 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007248 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007249 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007250 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007251 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007252 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007253 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007254
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007255 conf->psk_len = psk_len;
7256 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007257
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007258 memcpy( conf->psk, psk, conf->psk_len );
7259 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007260
7261 return( 0 );
7262}
7263
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007264int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7265 const unsigned char *psk, size_t psk_len )
7266{
7267 if( psk == NULL || ssl->handshake == NULL )
7268 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7269
7270 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7271 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7272
7273 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007274 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007275 mbedtls_platform_zeroize( ssl->handshake->psk,
7276 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007277 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007278 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007279 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007280
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007281 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007282 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007283
7284 ssl->handshake->psk_len = psk_len;
7285 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7286
7287 return( 0 );
7288}
7289
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007290void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007292 size_t),
7293 void *p_psk )
7294{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007295 conf->f_psk = f_psk;
7296 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007299
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007300#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007301
7302#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007303int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007304{
7305 int ret;
7306
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007307 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7308 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7309 {
7310 mbedtls_mpi_free( &conf->dhm_P );
7311 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007312 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007313 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007314
7315 return( 0 );
7316}
Hanno Becker470a8c42017-10-04 15:28:46 +01007317#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007318
Hanno Beckera90658f2017-10-04 15:29:08 +01007319int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7320 const unsigned char *dhm_P, size_t P_len,
7321 const unsigned char *dhm_G, size_t G_len )
7322{
7323 int ret;
7324
7325 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7326 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7327 {
7328 mbedtls_mpi_free( &conf->dhm_P );
7329 mbedtls_mpi_free( &conf->dhm_G );
7330 return( ret );
7331 }
7332
7333 return( 0 );
7334}
Paul Bakker5121ce52009-01-03 21:22:43 +00007335
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007336int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007337{
7338 int ret;
7339
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007340 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7341 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7342 {
7343 mbedtls_mpi_free( &conf->dhm_P );
7344 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007345 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007346 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007347
7348 return( 0 );
7349}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007350#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007351
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007352#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7353/*
7354 * Set the minimum length for Diffie-Hellman parameters
7355 */
7356void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7357 unsigned int bitlen )
7358{
7359 conf->dhm_min_bitlen = bitlen;
7360}
7361#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7362
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007363#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007364/*
7365 * Set allowed/preferred hashes for handshake signatures
7366 */
7367void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7368 const int *hashes )
7369{
7370 conf->sig_hashes = hashes;
7371}
Hanno Becker947194e2017-04-07 13:25:49 +01007372#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007373
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007374#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007375/*
7376 * Set the allowed elliptic curves
7377 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007378void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007379 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007380{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007381 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007382}
Hanno Becker947194e2017-04-07 13:25:49 +01007383#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007384
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007385#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007387{
Hanno Becker947194e2017-04-07 13:25:49 +01007388 /* Initialize to suppress unnecessary compiler warning */
7389 size_t hostname_len = 0;
7390
7391 /* Check if new hostname is valid before
7392 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007393 if( hostname != NULL )
7394 {
7395 hostname_len = strlen( hostname );
7396
7397 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7399 }
7400
7401 /* Now it's clear that we will overwrite the old hostname,
7402 * so we can free it safely */
7403
7404 if( ssl->hostname != NULL )
7405 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007406 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007407 mbedtls_free( ssl->hostname );
7408 }
7409
7410 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007411
Paul Bakker5121ce52009-01-03 21:22:43 +00007412 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007413 {
7414 ssl->hostname = NULL;
7415 }
7416 else
7417 {
7418 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007419 if( ssl->hostname == NULL )
7420 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007421
Hanno Becker947194e2017-04-07 13:25:49 +01007422 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007423
Hanno Becker947194e2017-04-07 13:25:49 +01007424 ssl->hostname[hostname_len] = '\0';
7425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007426
7427 return( 0 );
7428}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007429#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007430
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007431#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007432void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007434 const unsigned char *, size_t),
7435 void *p_sni )
7436{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007437 conf->f_sni = f_sni;
7438 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007439}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007443int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007444{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007445 size_t cur_len, tot_len;
7446 const char **p;
7447
7448 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007449 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7450 * MUST NOT be truncated."
7451 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007452 */
7453 tot_len = 0;
7454 for( p = protos; *p != NULL; p++ )
7455 {
7456 cur_len = strlen( *p );
7457 tot_len += cur_len;
7458
7459 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007460 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007461 }
7462
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007463 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007464
7465 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007466}
7467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007469{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007470 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007471}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007473
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007474void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007475{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007476 conf->max_major_ver = major;
7477 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007478}
7479
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007480void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007481{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007482 conf->min_major_ver = major;
7483 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007484}
7485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007486#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007487void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007488{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007489 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007490}
7491#endif
7492
Janos Follath088ce432017-04-10 12:42:31 +01007493#if defined(MBEDTLS_SSL_SRV_C)
7494void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7495 char cert_req_ca_list )
7496{
7497 conf->cert_req_ca_list = cert_req_ca_list;
7498}
7499#endif
7500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007502void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007503{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007504 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007505}
7506#endif
7507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007509void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007510{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007511 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007512}
7513#endif
7514
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007515#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007516void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007517{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007518 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007519}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007520#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007523int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007524{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007526 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007529 }
7530
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007531 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007532
7533 return( 0 );
7534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007535#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007537#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007538void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007539{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007540 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007541}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007542#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007545void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007546{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007547 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007548}
7549#endif
7550
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007551void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007552{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007553 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007554}
7555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007557void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007558{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007559 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007560}
7561
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007562void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007563{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007564 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007565}
7566
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007567void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007568 const unsigned char period[8] )
7569{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007570 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007575#if defined(MBEDTLS_SSL_CLI_C)
7576void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007577{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007578 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007579}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007580#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007581
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007582#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007583void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7584 mbedtls_ssl_ticket_write_t *f_ticket_write,
7585 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7586 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007587{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007588 conf->f_ticket_write = f_ticket_write;
7589 conf->f_ticket_parse = f_ticket_parse;
7590 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007591}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007592#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007594
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007595#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7596void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7597 mbedtls_ssl_export_keys_t *f_export_keys,
7598 void *p_export_keys )
7599{
7600 conf->f_export_keys = f_export_keys;
7601 conf->p_export_keys = p_export_keys;
7602}
7603#endif
7604
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007605#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007606void mbedtls_ssl_conf_async_private_cb(
7607 mbedtls_ssl_config *conf,
7608 mbedtls_ssl_async_sign_t *f_async_sign,
7609 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7610 mbedtls_ssl_async_resume_t *f_async_resume,
7611 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007612 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007613{
7614 conf->f_async_sign_start = f_async_sign;
7615 conf->f_async_decrypt_start = f_async_decrypt;
7616 conf->f_async_resume = f_async_resume;
7617 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007618 conf->p_async_config_data = async_config_data;
7619}
7620
Gilles Peskine8f97af72018-04-26 11:46:10 +02007621void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7622{
7623 return( conf->p_async_config_data );
7624}
7625
Gilles Peskine1febfef2018-04-30 11:54:39 +02007626void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007627{
7628 if( ssl->handshake == NULL )
7629 return( NULL );
7630 else
7631 return( ssl->handshake->user_async_ctx );
7632}
7633
Gilles Peskine1febfef2018-04-30 11:54:39 +02007634void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007635 void *ctx )
7636{
7637 if( ssl->handshake != NULL )
7638 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007639}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007640#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007641
Paul Bakker5121ce52009-01-03 21:22:43 +00007642/*
7643 * SSL get accessors
7644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007646{
7647 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7648}
7649
Hanno Becker8b170a02017-10-10 11:51:19 +01007650int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7651{
7652 /*
7653 * Case A: We're currently holding back
7654 * a message for further processing.
7655 */
7656
7657 if( ssl->keep_current_message == 1 )
7658 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007659 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007660 return( 1 );
7661 }
7662
7663 /*
7664 * Case B: Further records are pending in the current datagram.
7665 */
7666
7667#if defined(MBEDTLS_SSL_PROTO_DTLS)
7668 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7669 ssl->in_left > ssl->next_record_offset )
7670 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007671 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007672 return( 1 );
7673 }
7674#endif /* MBEDTLS_SSL_PROTO_DTLS */
7675
7676 /*
7677 * Case C: A handshake message is being processed.
7678 */
7679
Hanno Becker8b170a02017-10-10 11:51:19 +01007680 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7681 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007682 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007683 return( 1 );
7684 }
7685
7686 /*
7687 * Case D: An application data message is being processed
7688 */
7689 if( ssl->in_offt != NULL )
7690 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007692 return( 1 );
7693 }
7694
7695 /*
7696 * In all other cases, the rest of the message can be dropped.
7697 * As in ssl_read_record_layer, this needs to be adapted if
7698 * we implement support for multiple alerts in single records.
7699 */
7700
7701 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7702 return( 0 );
7703}
7704
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007705uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007706{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007707 if( ssl->session != NULL )
7708 return( ssl->session->verify_result );
7709
7710 if( ssl->session_negotiate != NULL )
7711 return( ssl->session_negotiate->verify_result );
7712
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007713 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007714}
7715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007717{
Paul Bakker926c8e42013-03-06 10:23:34 +01007718 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007719 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007722}
7723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007725{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007727 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007728 {
7729 switch( ssl->minor_ver )
7730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007732 return( "DTLSv1.0" );
7733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007735 return( "DTLSv1.2" );
7736
7737 default:
7738 return( "unknown (DTLS)" );
7739 }
7740 }
7741#endif
7742
Paul Bakker43ca69c2011-01-15 17:35:19 +00007743 switch( ssl->minor_ver )
7744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007746 return( "SSLv3.0" );
7747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007749 return( "TLSv1.0" );
7750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007752 return( "TLSv1.1" );
7753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007755 return( "TLSv1.2" );
7756
Paul Bakker43ca69c2011-01-15 17:35:19 +00007757 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007758 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007759 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007760}
7761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007763{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007764 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007766 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007767
Hanno Becker78640902018-08-13 16:35:15 +01007768 if( transform == NULL )
7769 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771#if defined(MBEDTLS_ZLIB_SUPPORT)
7772 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7773 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007774#endif
7775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007776 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778 case MBEDTLS_MODE_GCM:
7779 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007780 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007782 transform_expansion = transform->minlen;
7783 break;
7784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007786
7787 block_size = mbedtls_cipher_get_block_size(
7788 &transform->cipher_ctx_enc );
7789
7790#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7791 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7792 {
7793 /* Expansion due to addition of
7794 * - MAC
7795 * - CBC padding (theoretically up to 256 bytes, but
7796 * we never use more than block_size)
7797 * - explicit IV
7798 */
7799 transform_expansion = transform->maclen + 2 * block_size;
7800 }
7801 else
7802#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7803 {
7804 /* No explicit IV prior to TLS 1.1. */
7805 transform_expansion = transform->maclen + block_size;
7806 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007807 break;
7808
7809 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007812 }
7813
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007814 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007815}
7816
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007817#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7818size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7819{
7820 size_t max_len;
7821
7822 /*
7823 * Assume mfl_code is correct since it was checked when set
7824 */
Angus Grattond8213d02016-05-25 20:56:48 +10007825 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007826
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007827 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007828 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007829 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007830 {
Angus Grattond8213d02016-05-25 20:56:48 +10007831 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007832 }
7833
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007834 /* During a handshake, use the value being negotiated */
7835 if( ssl->session_negotiate != NULL &&
7836 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7837 {
7838 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7839 }
7840
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007841 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007842}
7843#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7844
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007845#if defined(MBEDTLS_SSL_PROTO_DTLS)
7846static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
7847{
7848 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
7849 return( ssl->mtu );
7850
7851 if( ssl->mtu == 0 )
7852 return( ssl->handshake->mtu );
7853
7854 return( ssl->mtu < ssl->handshake->mtu ?
7855 ssl->mtu : ssl->handshake->mtu );
7856}
7857#endif /* MBEDTLS_SSL_PROTO_DTLS */
7858
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007859int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7860{
7861 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7862
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02007863#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7864 !defined(MBEDTLS_SSL_PROTO_DTLS)
7865 (void) ssl;
7866#endif
7867
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007868#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7869 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7870
7871 if( max_len > mfl )
7872 max_len = mfl;
7873#endif
7874
7875#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007876 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007877 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007878 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007879 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7880 const size_t overhead = (size_t) ret;
7881
7882 if( ret < 0 )
7883 return( ret );
7884
7885 if( mtu <= overhead )
7886 {
7887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7888 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7889 }
7890
7891 if( max_len > mtu - overhead )
7892 max_len = mtu - overhead;
7893 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007894#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007895
Hanno Becker0defedb2018-08-10 12:35:02 +01007896#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7897 !defined(MBEDTLS_SSL_PROTO_DTLS)
7898 ((void) ssl);
7899#endif
7900
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007901 return( (int) max_len );
7902}
7903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904#if defined(MBEDTLS_X509_CRT_PARSE_C)
7905const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007906{
7907 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007908 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007909
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007910 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007911}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914#if defined(MBEDTLS_SSL_CLI_C)
7915int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007916{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007917 if( ssl == NULL ||
7918 dst == NULL ||
7919 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007920 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007923 }
7924
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007925 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007926}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007928
Paul Bakker5121ce52009-01-03 21:22:43 +00007929/*
Paul Bakker1961b702013-01-25 14:49:24 +01007930 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007931 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007933{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007935
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007936 if( ssl == NULL || ssl->conf == NULL )
7937 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007940 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007942#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007944 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007946#endif
7947
Paul Bakker1961b702013-01-25 14:49:24 +01007948 return( ret );
7949}
7950
7951/*
7952 * Perform the SSL handshake
7953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007955{
7956 int ret = 0;
7957
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007958 if( ssl == NULL || ssl->conf == NULL )
7959 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007966
7967 if( ret != 0 )
7968 break;
7969 }
7970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007972
7973 return( ret );
7974}
7975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976#if defined(MBEDTLS_SSL_RENEGOTIATION)
7977#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007978/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007979 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007980 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007982{
7983 int ret;
7984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007986
7987 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007988 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7989 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007990
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007991 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007992 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007993 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007994 return( ret );
7995 }
7996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007998
7999 return( 0 );
8000}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008002
8003/*
8004 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005 * - any side: calling mbedtls_ssl_renegotiate(),
8006 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8007 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008008 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008009 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008013{
8014 int ret;
8015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008017
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008018 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8019 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008020
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008021 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8022 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008024 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008026 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008027 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008028 ssl->handshake->out_msg_seq = 1;
8029 else
8030 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008031 }
8032#endif
8033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008034 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8035 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008040 return( ret );
8041 }
8042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008044
8045 return( 0 );
8046}
8047
8048/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008049 * Renegotiate current connection on client,
8050 * or request renegotiation on server
8051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008052int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008053{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008054 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008055
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008056 if( ssl == NULL || ssl->conf == NULL )
8057 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008059#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008060 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008061 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008063 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008066 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008067
8068 /* Did we already try/start sending HelloRequest? */
8069 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008070 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008071
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008072 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008073 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008077 /*
8078 * On client, either start the renegotiation process or,
8079 * if already in progress, continue the handshake
8080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008083 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008085
8086 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008088 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008089 return( ret );
8090 }
8091 }
8092 else
8093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008097 return( ret );
8098 }
8099 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008101
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008102 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008103}
8104
8105/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008106 * Check record counters and renegotiate if they're above the limit.
8107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008109{
Andres AG2196c7f2016-12-15 17:01:16 +00008110 size_t ep_len = ssl_ep_len( ssl );
8111 int in_ctr_cmp;
8112 int out_ctr_cmp;
8113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8115 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008116 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008117 {
8118 return( 0 );
8119 }
8120
Andres AG2196c7f2016-12-15 17:01:16 +00008121 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8122 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008123 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008124 ssl->conf->renego_period + ep_len, 8 - ep_len );
8125
8126 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008127 {
8128 return( 0 );
8129 }
8130
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008135
8136/*
8137 * Receive application data decrypted from the SSL layer
8138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008140{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008141 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008142 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008143
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008144 if( ssl == NULL || ssl->conf == NULL )
8145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008150 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008153 return( ret );
8154
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008155 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008157 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008158 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008159 return( ret );
8160 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008161 }
8162#endif
8163
Hanno Becker4a810fb2017-05-24 16:27:30 +01008164 /*
8165 * Check if renegotiation is necessary and/or handshake is
8166 * in process. If yes, perform/continue, and fall through
8167 * if an unexpected packet is received while the client
8168 * is waiting for the ServerHello.
8169 *
8170 * (There is no equivalent to the last condition on
8171 * the server-side as it is not treated as within
8172 * a handshake while waiting for the ClientHello
8173 * after a renegotiation request.)
8174 */
8175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008177 ret = ssl_check_ctr_renegotiate( ssl );
8178 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8179 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008182 return( ret );
8183 }
8184#endif
8185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008188 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008189 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8190 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008193 return( ret );
8194 }
8195 }
8196
Hanno Beckere41158b2017-10-23 13:30:32 +01008197 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008198 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008199 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008200 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008201 if( ssl->f_get_timer != NULL &&
8202 ssl->f_get_timer( ssl->p_timer ) == -1 )
8203 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008204 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008205 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008206
Hanno Becker327c93b2018-08-15 13:56:18 +01008207 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008208 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008209 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8210 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008211
Hanno Becker4a810fb2017-05-24 16:27:30 +01008212 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8213 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008214 }
8215
8216 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008218 {
8219 /*
8220 * OpenSSL sends empty messages to randomize the IV
8221 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008222 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008224 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008225 return( 0 );
8226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008228 return( ret );
8229 }
8230 }
8231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008235
Hanno Becker4a810fb2017-05-24 16:27:30 +01008236 /*
8237 * - For client-side, expect SERVER_HELLO_REQUEST.
8238 * - For server-side, expect CLIENT_HELLO.
8239 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8240 */
8241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008242#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008243 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008244 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008245 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008248
8249 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008251 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008252 {
8253 continue;
8254 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008255#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008256 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008257 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008258#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008259
Hanno Becker4a810fb2017-05-24 16:27:30 +01008260#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008261 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008262 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008265
8266 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008267#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008268 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008269 {
8270 continue;
8271 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008272#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008274 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008275#endif /* MBEDTLS_SSL_SRV_C */
8276
Hanno Becker21df7f92017-10-17 11:03:26 +01008277#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008278 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008279 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8280 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8281 ssl->conf->allow_legacy_renegotiation ==
8282 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8283 {
8284 /*
8285 * Accept renegotiation request
8286 */
Paul Bakker48916f92012-09-16 19:57:18 +00008287
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008288 /* DTLS clients need to know renego is server-initiated */
8289#if defined(MBEDTLS_SSL_PROTO_DTLS)
8290 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8291 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8292 {
8293 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8294 }
8295#endif
8296 ret = ssl_start_renegotiation( ssl );
8297 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8298 ret != 0 )
8299 {
8300 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8301 return( ret );
8302 }
8303 }
8304 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008305#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008306 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008307 /*
8308 * Refuse renegotiation
8309 */
8310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313#if defined(MBEDTLS_SSL_PROTO_SSL3)
8314 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008315 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008316 /* SSLv3 does not have a "no_renegotiation" warning, so
8317 we send a fatal alert and abort the connection. */
8318 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8319 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8320 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008321 }
8322 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8324#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8325 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8326 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8329 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8330 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008331 {
8332 return( ret );
8333 }
Paul Bakker48916f92012-09-16 19:57:18 +00008334 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008335 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8337 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8340 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008341 }
Paul Bakker48916f92012-09-16 19:57:18 +00008342 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008343
Hanno Becker90333da2017-10-10 11:27:13 +01008344 /* At this point, we don't know whether the renegotiation has been
8345 * completed or not. The cases to consider are the following:
8346 * 1) The renegotiation is complete. In this case, no new record
8347 * has been read yet.
8348 * 2) The renegotiation is incomplete because the client received
8349 * an application data record while awaiting the ServerHello.
8350 * 3) The renegotiation is incomplete because the client received
8351 * a non-handshake, non-application data message while awaiting
8352 * the ServerHello.
8353 * In each of these case, looping will be the proper action:
8354 * - For 1), the next iteration will read a new record and check
8355 * if it's application data.
8356 * - For 2), the loop condition isn't satisfied as application data
8357 * is present, hence continue is the same as break
8358 * - For 3), the loop condition is satisfied and read_record
8359 * will re-deliver the message that was held back by the client
8360 * when expecting the ServerHello.
8361 */
8362 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008363 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008364#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008366 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008367 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008368 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008369 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008372 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008374 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008375 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008376 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008377#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8380 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008383 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008384 }
8385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008386 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8389 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008390 }
8391
8392 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008393
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008394 /* We're going to return something now, cancel timer,
8395 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008397 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008398
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008399#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008400 /* If we requested renego but received AppData, resend HelloRequest.
8401 * Do it now, after setting in_offt, to avoid taking this branch
8402 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008404 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008406 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008407 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008409 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008410 return( ret );
8411 }
8412 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008414#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008415 }
8416
8417 n = ( len < ssl->in_msglen )
8418 ? len : ssl->in_msglen;
8419
8420 memcpy( buf, ssl->in_offt, n );
8421 ssl->in_msglen -= n;
8422
8423 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008424 {
8425 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008426 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008427 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008428 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008429 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008430 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008431 /* more data available */
8432 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008433 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008436
Paul Bakker23986e52011-04-24 08:57:21 +00008437 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008438}
8439
8440/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008441 * Send application data to be encrypted by the SSL layer, taking care of max
8442 * fragment length and buffer size.
8443 *
8444 * According to RFC 5246 Section 6.2.1:
8445 *
8446 * Zero-length fragments of Application data MAY be sent as they are
8447 * potentially useful as a traffic analysis countermeasure.
8448 *
8449 * Therefore, it is possible that the input message length is 0 and the
8450 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008451 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008452static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008453 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008454{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008455 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8456 const size_t max_len = (size_t) ret;
8457
8458 if( ret < 0 )
8459 {
8460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8461 return( ret );
8462 }
8463
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008464 if( len > max_len )
8465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008466#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008467 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008470 "maximum fragment length: %d > %d",
8471 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008473 }
8474 else
8475#endif
8476 len = max_len;
8477 }
Paul Bakker887bd502011-06-08 13:10:54 +00008478
Paul Bakker5121ce52009-01-03 21:22:43 +00008479 if( ssl->out_left != 0 )
8480 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008481 /*
8482 * The user has previously tried to send the data and
8483 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8484 * written. In this case, we expect the high-level write function
8485 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008487 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008489 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008490 return( ret );
8491 }
8492 }
Paul Bakker887bd502011-06-08 13:10:54 +00008493 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008494 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008495 /*
8496 * The user is trying to send a message the first time, so we need to
8497 * copy the data into the internal buffers and setup the data structure
8498 * to keep track of partial writes
8499 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008500 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008501 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008502 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008503
Hanno Becker67bc7c32018-08-06 11:33:50 +01008504 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008507 return( ret );
8508 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008509 }
8510
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008511 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008512}
8513
8514/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008515 * Write application data, doing 1/n-1 splitting if necessary.
8516 *
8517 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008518 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008519 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008521#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008522static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008523 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008524{
8525 int ret;
8526
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008527 if( ssl->conf->cbc_record_splitting ==
8528 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008529 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008530 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8531 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8532 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008533 {
8534 return( ssl_write_real( ssl, buf, len ) );
8535 }
8536
8537 if( ssl->split_done == 0 )
8538 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008539 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008540 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008541 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008542 }
8543
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008544 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8545 return( ret );
8546 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008547
8548 return( ret + 1 );
8549}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008550#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008551
8552/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008553 * Write application data (public-facing wrapper)
8554 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008555int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008556{
8557 int ret;
8558
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008560
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008561 if( ssl == NULL || ssl->conf == NULL )
8562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8563
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008564#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008565 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8566 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008567 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008568 return( ret );
8569 }
8570#endif
8571
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008572 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008573 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008574 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008575 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008577 return( ret );
8578 }
8579 }
8580
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008581#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008582 ret = ssl_write_split( ssl, buf, len );
8583#else
8584 ret = ssl_write_real( ssl, buf, len );
8585#endif
8586
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008588
8589 return( ret );
8590}
8591
8592/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008593 * Notify the peer that the connection is being closed
8594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008596{
8597 int ret;
8598
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008599 if( ssl == NULL || ssl->conf == NULL )
8600 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008603
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008604 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008607 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8610 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8611 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008614 return( ret );
8615 }
8616 }
8617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008619
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008620 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008621}
8622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008624{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008625 if( transform == NULL )
8626 return;
8627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008629 deflateEnd( &transform->ctx_deflate );
8630 inflateEnd( &transform->ctx_inflate );
8631#endif
8632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8634 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008636 mbedtls_md_free( &transform->md_ctx_enc );
8637 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008638
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008639 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008640}
8641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008642#if defined(MBEDTLS_X509_CRT_PARSE_C)
8643static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008644{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008646
8647 while( cur != NULL )
8648 {
8649 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008650 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008651 cur = next;
8652 }
8653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008655
Hanno Becker0271f962018-08-16 13:23:47 +01008656#if defined(MBEDTLS_SSL_PROTO_DTLS)
8657
8658static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8659{
8660 unsigned offset;
8661 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8662
8663 if( hs == NULL )
8664 return;
8665
8666 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008667 ssl_buffering_free_slot( ssl, offset );
8668}
8669
8670static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8671 uint8_t slot )
8672{
8673 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8674 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
8675 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008676 {
Hanno Beckere605b192018-08-21 15:59:07 +01008677 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
8678 mbedtls_free( hs_buf->data );
8679 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008680 }
8681}
8682
8683#endif /* MBEDTLS_SSL_PROTO_DTLS */
8684
Gilles Peskine9b562d52018-04-25 20:32:43 +02008685void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008686{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008687 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8688
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008689 if( handshake == NULL )
8690 return;
8691
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008692#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8693 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8694 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008695 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008696 handshake->async_in_progress = 0;
8697 }
8698#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8699
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008700#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8701 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8702 mbedtls_md5_free( &handshake->fin_md5 );
8703 mbedtls_sha1_free( &handshake->fin_sha1 );
8704#endif
8705#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8706#if defined(MBEDTLS_SHA256_C)
8707 mbedtls_sha256_free( &handshake->fin_sha256 );
8708#endif
8709#if defined(MBEDTLS_SHA512_C)
8710 mbedtls_sha512_free( &handshake->fin_sha512 );
8711#endif
8712#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008714#if defined(MBEDTLS_DHM_C)
8715 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008716#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008717#if defined(MBEDTLS_ECDH_C)
8718 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008719#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008720#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008721 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008722#if defined(MBEDTLS_SSL_CLI_C)
8723 mbedtls_free( handshake->ecjpake_cache );
8724 handshake->ecjpake_cache = NULL;
8725 handshake->ecjpake_cache_len = 0;
8726#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008727#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008728
Janos Follath4ae5c292016-02-10 11:27:43 +00008729#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8730 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008731 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008732 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008733#endif
8734
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008735#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8736 if( handshake->psk != NULL )
8737 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008738 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008739 mbedtls_free( handshake->psk );
8740 }
8741#endif
8742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008743#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8744 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008745 /*
8746 * Free only the linked list wrapper, not the keys themselves
8747 * since the belong to the SNI callback
8748 */
8749 if( handshake->sni_key_cert != NULL )
8750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008751 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008752
8753 while( cur != NULL )
8754 {
8755 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008757 cur = next;
8758 }
8759 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008760#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008762#if defined(MBEDTLS_SSL_PROTO_DTLS)
8763 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008764 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008765 ssl_buffering_free( ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01008766 ssl_free_buffered_record( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008767#endif
8768
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008769 mbedtls_platform_zeroize( handshake,
8770 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008771}
8772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008773void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008774{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008775 if( session == NULL )
8776 return;
8777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008779 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781 mbedtls_x509_crt_free( session->peer_cert );
8782 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008783 }
Paul Bakkered27a042013-04-18 22:46:23 +02008784#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008785
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008786#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008788#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008789
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008790 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008791}
8792
Paul Bakker5121ce52009-01-03 21:22:43 +00008793/*
8794 * Free an SSL context
8795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008796void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008797{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008798 if( ssl == NULL )
8799 return;
8800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008802
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008803 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008804 {
Angus Grattond8213d02016-05-25 20:56:48 +10008805 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008806 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008807 }
8808
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008809 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008810 {
Angus Grattond8213d02016-05-25 20:56:48 +10008811 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008813 }
8814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008815#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008816 if( ssl->compress_buf != NULL )
8817 {
Angus Grattond8213d02016-05-25 20:56:48 +10008818 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008819 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008820 }
8821#endif
8822
Paul Bakker48916f92012-09-16 19:57:18 +00008823 if( ssl->transform )
8824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825 mbedtls_ssl_transform_free( ssl->transform );
8826 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008827 }
8828
8829 if( ssl->handshake )
8830 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008831 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8833 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008835 mbedtls_free( ssl->handshake );
8836 mbedtls_free( ssl->transform_negotiate );
8837 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008838 }
8839
Paul Bakkerc0463502013-02-14 11:19:38 +01008840 if( ssl->session )
8841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008842 mbedtls_ssl_session_free( ssl->session );
8843 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008844 }
8845
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008846#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008847 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008848 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008849 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008851 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008852#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008854#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8855 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8858 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008859 }
8860#endif
8861
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008862#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008863 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008864#endif
8865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008867
Paul Bakker86f04f42013-02-14 11:20:09 +01008868 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008869 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008870}
8871
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008872/*
8873 * Initialze mbedtls_ssl_config
8874 */
8875void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8876{
8877 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8878}
8879
Simon Butcherc97b6972015-12-27 23:48:17 +00008880#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008881static int ssl_preset_default_hashes[] = {
8882#if defined(MBEDTLS_SHA512_C)
8883 MBEDTLS_MD_SHA512,
8884 MBEDTLS_MD_SHA384,
8885#endif
8886#if defined(MBEDTLS_SHA256_C)
8887 MBEDTLS_MD_SHA256,
8888 MBEDTLS_MD_SHA224,
8889#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008890#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008891 MBEDTLS_MD_SHA1,
8892#endif
8893 MBEDTLS_MD_NONE
8894};
Simon Butcherc97b6972015-12-27 23:48:17 +00008895#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008896
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008897static int ssl_preset_suiteb_ciphersuites[] = {
8898 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8899 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8900 0
8901};
8902
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008903#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008904static int ssl_preset_suiteb_hashes[] = {
8905 MBEDTLS_MD_SHA256,
8906 MBEDTLS_MD_SHA384,
8907 MBEDTLS_MD_NONE
8908};
8909#endif
8910
8911#if defined(MBEDTLS_ECP_C)
8912static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8913 MBEDTLS_ECP_DP_SECP256R1,
8914 MBEDTLS_ECP_DP_SECP384R1,
8915 MBEDTLS_ECP_DP_NONE
8916};
8917#endif
8918
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008919/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008920 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008921 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008922int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008923 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008924{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008925#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008926 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008927#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008928
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008929 /* Use the functions here so that they are covered in tests,
8930 * but otherwise access member directly for efficiency */
8931 mbedtls_ssl_conf_endpoint( conf, endpoint );
8932 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008933
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008934 /*
8935 * Things that are common to all presets
8936 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008937#if defined(MBEDTLS_SSL_CLI_C)
8938 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8939 {
8940 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8941#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8942 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8943#endif
8944 }
8945#endif
8946
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008947#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008948 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008949#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008950
8951#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8952 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8953#endif
8954
8955#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8956 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8957#endif
8958
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008959#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8960 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8961#endif
8962
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008963#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008964 conf->f_cookie_write = ssl_cookie_write_dummy;
8965 conf->f_cookie_check = ssl_cookie_check_dummy;
8966#endif
8967
8968#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8969 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8970#endif
8971
Janos Follath088ce432017-04-10 12:42:31 +01008972#if defined(MBEDTLS_SSL_SRV_C)
8973 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8974#endif
8975
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008976#if defined(MBEDTLS_SSL_PROTO_DTLS)
8977 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8978 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8979#endif
8980
8981#if defined(MBEDTLS_SSL_RENEGOTIATION)
8982 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008983 memset( conf->renego_period, 0x00, 2 );
8984 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008985#endif
8986
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008987#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8988 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8989 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008990 const unsigned char dhm_p[] =
8991 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8992 const unsigned char dhm_g[] =
8993 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8994
Hanno Beckera90658f2017-10-04 15:29:08 +01008995 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8996 dhm_p, sizeof( dhm_p ),
8997 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008998 {
8999 return( ret );
9000 }
9001 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009002#endif
9003
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009004 /*
9005 * Preset-specific defaults
9006 */
9007 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009008 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009009 /*
9010 * NSA Suite B
9011 */
9012 case MBEDTLS_SSL_PRESET_SUITEB:
9013 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9014 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9015 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9016 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9017
9018 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9019 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9020 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9021 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9022 ssl_preset_suiteb_ciphersuites;
9023
9024#if defined(MBEDTLS_X509_CRT_PARSE_C)
9025 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009026#endif
9027
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009028#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009029 conf->sig_hashes = ssl_preset_suiteb_hashes;
9030#endif
9031
9032#if defined(MBEDTLS_ECP_C)
9033 conf->curve_list = ssl_preset_suiteb_curves;
9034#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009035 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009036
9037 /*
9038 * Default
9039 */
9040 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009041 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9042 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9043 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9044 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9045 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9046 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9047 MBEDTLS_SSL_MIN_MINOR_VERSION :
9048 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009049 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9050 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9051
9052#if defined(MBEDTLS_SSL_PROTO_DTLS)
9053 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9054 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9055#endif
9056
9057 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9058 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9059 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9060 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9061 mbedtls_ssl_list_ciphersuites();
9062
9063#if defined(MBEDTLS_X509_CRT_PARSE_C)
9064 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9065#endif
9066
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009067#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009068 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009069#endif
9070
9071#if defined(MBEDTLS_ECP_C)
9072 conf->curve_list = mbedtls_ecp_grp_id_list();
9073#endif
9074
9075#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9076 conf->dhm_min_bitlen = 1024;
9077#endif
9078 }
9079
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009080 return( 0 );
9081}
9082
9083/*
9084 * Free mbedtls_ssl_config
9085 */
9086void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9087{
9088#if defined(MBEDTLS_DHM_C)
9089 mbedtls_mpi_free( &conf->dhm_P );
9090 mbedtls_mpi_free( &conf->dhm_G );
9091#endif
9092
9093#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9094 if( conf->psk != NULL )
9095 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009096 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009097 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009098 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009099 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009100 }
9101
9102 if( conf->psk_identity != NULL )
9103 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009104 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009105 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009106 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009107 conf->psk_identity_len = 0;
9108 }
9109#endif
9110
9111#if defined(MBEDTLS_X509_CRT_PARSE_C)
9112 ssl_key_cert_free( conf->key_cert );
9113#endif
9114
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009115 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009116}
9117
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009118#if defined(MBEDTLS_PK_C) && \
9119 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009120/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009121 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009123unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009125#if defined(MBEDTLS_RSA_C)
9126 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9127 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009128#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009129#if defined(MBEDTLS_ECDSA_C)
9130 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9131 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009132#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009133 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009134}
9135
Hanno Becker7e5437a2017-04-28 17:15:26 +01009136unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9137{
9138 switch( type ) {
9139 case MBEDTLS_PK_RSA:
9140 return( MBEDTLS_SSL_SIG_RSA );
9141 case MBEDTLS_PK_ECDSA:
9142 case MBEDTLS_PK_ECKEY:
9143 return( MBEDTLS_SSL_SIG_ECDSA );
9144 default:
9145 return( MBEDTLS_SSL_SIG_ANON );
9146 }
9147}
9148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009149mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009150{
9151 switch( sig )
9152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009153#if defined(MBEDTLS_RSA_C)
9154 case MBEDTLS_SSL_SIG_RSA:
9155 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009156#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009157#if defined(MBEDTLS_ECDSA_C)
9158 case MBEDTLS_SSL_SIG_ECDSA:
9159 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009160#endif
9161 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009162 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009163 }
9164}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009165#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009166
Hanno Becker7e5437a2017-04-28 17:15:26 +01009167#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9168 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9169
9170/* Find an entry in a signature-hash set matching a given hash algorithm. */
9171mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9172 mbedtls_pk_type_t sig_alg )
9173{
9174 switch( sig_alg )
9175 {
9176 case MBEDTLS_PK_RSA:
9177 return( set->rsa );
9178 case MBEDTLS_PK_ECDSA:
9179 return( set->ecdsa );
9180 default:
9181 return( MBEDTLS_MD_NONE );
9182 }
9183}
9184
9185/* Add a signature-hash-pair to a signature-hash set */
9186void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9187 mbedtls_pk_type_t sig_alg,
9188 mbedtls_md_type_t md_alg )
9189{
9190 switch( sig_alg )
9191 {
9192 case MBEDTLS_PK_RSA:
9193 if( set->rsa == MBEDTLS_MD_NONE )
9194 set->rsa = md_alg;
9195 break;
9196
9197 case MBEDTLS_PK_ECDSA:
9198 if( set->ecdsa == MBEDTLS_MD_NONE )
9199 set->ecdsa = md_alg;
9200 break;
9201
9202 default:
9203 break;
9204 }
9205}
9206
9207/* Allow exactly one hash algorithm for each signature. */
9208void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9209 mbedtls_md_type_t md_alg )
9210{
9211 set->rsa = md_alg;
9212 set->ecdsa = md_alg;
9213}
9214
9215#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9216 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9217
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009218/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009219 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009221mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009222{
9223 switch( hash )
9224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009225#if defined(MBEDTLS_MD5_C)
9226 case MBEDTLS_SSL_HASH_MD5:
9227 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009228#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009229#if defined(MBEDTLS_SHA1_C)
9230 case MBEDTLS_SSL_HASH_SHA1:
9231 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009232#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009233#if defined(MBEDTLS_SHA256_C)
9234 case MBEDTLS_SSL_HASH_SHA224:
9235 return( MBEDTLS_MD_SHA224 );
9236 case MBEDTLS_SSL_HASH_SHA256:
9237 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009238#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009239#if defined(MBEDTLS_SHA512_C)
9240 case MBEDTLS_SSL_HASH_SHA384:
9241 return( MBEDTLS_MD_SHA384 );
9242 case MBEDTLS_SSL_HASH_SHA512:
9243 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009244#endif
9245 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009246 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009247 }
9248}
9249
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009250/*
9251 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9252 */
9253unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9254{
9255 switch( md )
9256 {
9257#if defined(MBEDTLS_MD5_C)
9258 case MBEDTLS_MD_MD5:
9259 return( MBEDTLS_SSL_HASH_MD5 );
9260#endif
9261#if defined(MBEDTLS_SHA1_C)
9262 case MBEDTLS_MD_SHA1:
9263 return( MBEDTLS_SSL_HASH_SHA1 );
9264#endif
9265#if defined(MBEDTLS_SHA256_C)
9266 case MBEDTLS_MD_SHA224:
9267 return( MBEDTLS_SSL_HASH_SHA224 );
9268 case MBEDTLS_MD_SHA256:
9269 return( MBEDTLS_SSL_HASH_SHA256 );
9270#endif
9271#if defined(MBEDTLS_SHA512_C)
9272 case MBEDTLS_MD_SHA384:
9273 return( MBEDTLS_SSL_HASH_SHA384 );
9274 case MBEDTLS_MD_SHA512:
9275 return( MBEDTLS_SSL_HASH_SHA512 );
9276#endif
9277 default:
9278 return( MBEDTLS_SSL_HASH_NONE );
9279 }
9280}
9281
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009282#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009283/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009284 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009285 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009286 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009287int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009288{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009289 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009290
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009291 if( ssl->conf->curve_list == NULL )
9292 return( -1 );
9293
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009294 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009295 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009296 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009297
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009298 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009299}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009300#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009301
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009302#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009303/*
9304 * Check if a hash proposed by the peer is in our list.
9305 * Return 0 if we're willing to use it, -1 otherwise.
9306 */
9307int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9308 mbedtls_md_type_t md )
9309{
9310 const int *cur;
9311
9312 if( ssl->conf->sig_hashes == NULL )
9313 return( -1 );
9314
9315 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9316 if( *cur == (int) md )
9317 return( 0 );
9318
9319 return( -1 );
9320}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009321#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323#if defined(MBEDTLS_X509_CRT_PARSE_C)
9324int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9325 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009326 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009327 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009328{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009329 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009331 int usage = 0;
9332#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009333#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009334 const char *ext_oid;
9335 size_t ext_len;
9336#endif
9337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009338#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9339 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009340 ((void) cert);
9341 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009342 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009343#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009345#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9346 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009347 {
9348 /* Server part of the key exchange */
9349 switch( ciphersuite->key_exchange )
9350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009351 case MBEDTLS_KEY_EXCHANGE_RSA:
9352 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009353 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009354 break;
9355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9357 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9358 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9359 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009360 break;
9361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009362 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9363 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009364 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009365 break;
9366
9367 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009368 case MBEDTLS_KEY_EXCHANGE_NONE:
9369 case MBEDTLS_KEY_EXCHANGE_PSK:
9370 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9371 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009372 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009373 usage = 0;
9374 }
9375 }
9376 else
9377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009378 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9379 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009380 }
9381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009382 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009383 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009384 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009385 ret = -1;
9386 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009387#else
9388 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009389#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009391#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9392 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009394 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9395 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009396 }
9397 else
9398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009399 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9400 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009401 }
9402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009403 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009404 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009405 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009406 ret = -1;
9407 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009409
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009410 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009411}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009412#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009413
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009414/*
9415 * Convert version numbers to/from wire format
9416 * and, for DTLS, to/from TLS equivalent.
9417 *
9418 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009419 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009420 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9421 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009423void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009424 unsigned char ver[2] )
9425{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009426#if defined(MBEDTLS_SSL_PROTO_DTLS)
9427 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009429 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009430 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9431
9432 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9433 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9434 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009435 else
9436#else
9437 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009438#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009439 {
9440 ver[0] = (unsigned char) major;
9441 ver[1] = (unsigned char) minor;
9442 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009443}
9444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009445void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009446 const unsigned char ver[2] )
9447{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009448#if defined(MBEDTLS_SSL_PROTO_DTLS)
9449 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009450 {
9451 *major = 255 - ver[0] + 2;
9452 *minor = 255 - ver[1] + 1;
9453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009454 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009455 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9456 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009457 else
9458#else
9459 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009460#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009461 {
9462 *major = ver[0];
9463 *minor = ver[1];
9464 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009465}
9466
Simon Butcher99000142016-10-13 17:21:01 +01009467int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9468{
9469#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9470 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9471 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9472
9473 switch( md )
9474 {
9475#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9476#if defined(MBEDTLS_MD5_C)
9477 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009478 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009479#endif
9480#if defined(MBEDTLS_SHA1_C)
9481 case MBEDTLS_SSL_HASH_SHA1:
9482 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9483 break;
9484#endif
9485#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9486#if defined(MBEDTLS_SHA512_C)
9487 case MBEDTLS_SSL_HASH_SHA384:
9488 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9489 break;
9490#endif
9491#if defined(MBEDTLS_SHA256_C)
9492 case MBEDTLS_SSL_HASH_SHA256:
9493 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9494 break;
9495#endif
9496 default:
9497 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9498 }
9499
9500 return 0;
9501#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9502 (void) ssl;
9503 (void) md;
9504
9505 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9506#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9507}
9508
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009509#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9510 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9511int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9512 unsigned char *output,
9513 unsigned char *data, size_t data_len )
9514{
9515 int ret = 0;
9516 mbedtls_md5_context mbedtls_md5;
9517 mbedtls_sha1_context mbedtls_sha1;
9518
9519 mbedtls_md5_init( &mbedtls_md5 );
9520 mbedtls_sha1_init( &mbedtls_sha1 );
9521
9522 /*
9523 * digitally-signed struct {
9524 * opaque md5_hash[16];
9525 * opaque sha_hash[20];
9526 * };
9527 *
9528 * md5_hash
9529 * MD5(ClientHello.random + ServerHello.random
9530 * + ServerParams);
9531 * sha_hash
9532 * SHA(ClientHello.random + ServerHello.random
9533 * + ServerParams);
9534 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009535 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009536 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009537 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009538 goto exit;
9539 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009540 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009541 ssl->handshake->randbytes, 64 ) ) != 0 )
9542 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009543 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009544 goto exit;
9545 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009546 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009547 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009548 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009549 goto exit;
9550 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009551 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009552 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009554 goto exit;
9555 }
9556
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009557 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009558 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009559 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009560 goto exit;
9561 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009562 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009563 ssl->handshake->randbytes, 64 ) ) != 0 )
9564 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009566 goto exit;
9567 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009568 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009569 data_len ) ) != 0 )
9570 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009571 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009572 goto exit;
9573 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009574 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009575 output + 16 ) ) != 0 )
9576 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009578 goto exit;
9579 }
9580
9581exit:
9582 mbedtls_md5_free( &mbedtls_md5 );
9583 mbedtls_sha1_free( &mbedtls_sha1 );
9584
9585 if( ret != 0 )
9586 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9587 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9588
9589 return( ret );
9590
9591}
9592#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9593 MBEDTLS_SSL_PROTO_TLS1_1 */
9594
9595#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9596 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9597int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009598 unsigned char *hash, size_t *hashlen,
9599 unsigned char *data, size_t data_len,
9600 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009601{
9602 int ret = 0;
9603 mbedtls_md_context_t ctx;
9604 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009605 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009606
9607 mbedtls_md_init( &ctx );
9608
9609 /*
9610 * digitally-signed struct {
9611 * opaque client_random[32];
9612 * opaque server_random[32];
9613 * ServerDHParams params;
9614 * };
9615 */
9616 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9617 {
9618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9619 goto exit;
9620 }
9621 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9622 {
9623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9624 goto exit;
9625 }
9626 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9627 {
9628 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9629 goto exit;
9630 }
9631 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9632 {
9633 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9634 goto exit;
9635 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009636 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009637 {
9638 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9639 goto exit;
9640 }
9641
9642exit:
9643 mbedtls_md_free( &ctx );
9644
9645 if( ret != 0 )
9646 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9647 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9648
9649 return( ret );
9650}
9651#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9652 MBEDTLS_SSL_PROTO_TLS1_2 */
9653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654#endif /* MBEDTLS_SSL_TLS_C */