blob: fdd1a71113a97a41fd529bb22b32c4631fa21f91 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46/*
47 * The SSL 3.0 specification was drafted by Netscape in 1996,
48 * and became an IETF standard in 1999.
49 *
50 * http://wp.netscape.com/eng/ssl3/
51 * http://www.ietf.org/rfc/rfc2246.txt
52 * http://www.ietf.org/rfc/rfc4346.txt
53 */
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020059#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000062
SimonBd5800b72016-04-26 07:43:27 +010063#if defined(MBEDTLS_PLATFORM_C)
64#include "mbedtls/platform.h"
65#else
66#include <stdlib.h>
67#define mbedtls_calloc calloc
68#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010069#endif
70
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/debug.h"
72#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020073#include "mbedtls/ssl_internal.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020074
Rich Evans00ab4702015-02-06 13:43:58 +000075#include <string.h>
76
Janos Follath23bdca02016-10-07 14:47:14 +010077#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000078#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020079#endif
80
Paul Bakker34617722014-06-13 17:20:13 +020081/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020083 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
84}
85
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010086/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020090 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010091 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010092#else
93 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010094#endif
95 return( 0 );
96}
97
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098/*
99 * Start a timer.
100 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200103{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200104 if( ssl->f_set_timer == NULL )
105 return;
106
107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
108 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200109}
110
111/*
112 * Return -1 is timer is expired, 0 if it isn't.
113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200115{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200116 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200117 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200118
119 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200120 {
121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200122 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200123 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200124
125 return( 0 );
126}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200127
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200128#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200129/*
130 * Double the retransmit timeout value, within the allowed range,
131 * returning -1 if the maximum value has already been reached.
132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200134{
135 uint32_t new_timeout;
136
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200137 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200138 return( -1 );
139
140 new_timeout = 2 * ssl->handshake->retransmit_timeout;
141
142 /* Avoid arithmetic overflow and range overflow */
143 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200144 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200145 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200146 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200147 }
148
149 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200151 ssl->handshake->retransmit_timeout ) );
152
153 return( 0 );
154}
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200157{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200158 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200160 ssl->handshake->retransmit_timeout ) );
161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200165/*
166 * Convert max_fragment_length codes to length.
167 * RFC 6066 says:
168 * enum{
169 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
170 * } MaxFragmentLength;
171 * and we add 0 -> extension unused
172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173static unsigned int mfl_code_to_length[MBEDTLS_SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200174{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 MBEDTLS_SSL_MAX_CONTENT_LEN, /* MBEDTLS_SSL_MAX_FRAG_LEN_NONE */
176 512, /* MBEDTLS_SSL_MAX_FRAG_LEN_512 */
177 1024, /* MBEDTLS_SSL_MAX_FRAG_LEN_1024 */
178 2048, /* MBEDTLS_SSL_MAX_FRAG_LEN_2048 */
179 4096, /* MBEDTLS_SSL_MAX_FRAG_LEN_4096 */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200180};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200182
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200183#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200185{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_ssl_session_free( dst );
187 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200190 if( src->peer_cert != NULL )
191 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200192 int ret;
193
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200194 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200195 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200196 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200201 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200204 dst->peer_cert = NULL;
205 return( ret );
206 }
207 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200209
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200210#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200211 if( src->ticket != NULL )
212 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200213 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200214 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200215 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200216
217 memcpy( dst->ticket, src->ticket, src->ticket_len );
218 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200219#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200220
221 return( 0 );
222}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200223#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
226int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200227 const unsigned char *key_enc, const unsigned char *key_dec,
228 size_t keylen,
229 const unsigned char *iv_enc, const unsigned char *iv_dec,
230 size_t ivlen,
231 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200232 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
234int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
235int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
236int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
237int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
238#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000239
Paul Bakker5121ce52009-01-03 21:22:43 +0000240/*
241 * Key material generation
242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200244static int ssl3_prf( const unsigned char *secret, size_t slen,
245 const char *label,
246 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000247 unsigned char *dstbuf, size_t dlen )
248{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100249 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000250 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200251 mbedtls_md5_context md5;
252 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000253 unsigned char padding[16];
254 unsigned char sha1sum[20];
255 ((void)label);
256
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200257 mbedtls_md5_init( &md5 );
258 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200259
Paul Bakker5f70b252012-09-13 14:23:06 +0000260 /*
261 * SSLv3:
262 * block =
263 * MD5( secret + SHA1( 'A' + secret + random ) ) +
264 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
265 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
266 * ...
267 */
268 for( i = 0; i < dlen / 16; i++ )
269 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200270 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000271
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100272 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100273 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100274 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100275 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100276 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100277 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100278 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100279 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100280 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100281 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000282
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100283 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100284 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100285 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100286 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100287 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100288 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100289 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100290 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000291 }
292
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100293exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200294 mbedtls_md5_free( &md5 );
295 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_zeroize( padding, sizeof( padding ) );
298 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000299
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100300 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000301}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200305static int tls1_prf( const unsigned char *secret, size_t slen,
306 const char *label,
307 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000308 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000309{
Paul Bakker23986e52011-04-24 08:57:21 +0000310 size_t nb, hs;
311 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200312 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 unsigned char tmp[128];
314 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 const mbedtls_md_info_t *md_info;
316 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100317 int ret;
318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
321 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
324 hs = ( slen + 1 ) / 2;
325 S1 = secret;
326 S2 = secret + slen - hs;
327
328 nb = strlen( label );
329 memcpy( tmp + 20, label, nb );
330 memcpy( tmp + 20 + nb, random, rlen );
331 nb += rlen;
332
333 /*
334 * First compute P_md5(secret,label+random)[0..dlen]
335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100340 return( ret );
341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
343 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
344 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
346 for( i = 0; i < dlen; i += 16 )
347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_md_hmac_reset ( &md_ctx );
349 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
350 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_md_hmac_reset ( &md_ctx );
353 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
354 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
356 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
357
358 for( j = 0; j < k; j++ )
359 dstbuf[i + j] = h_i[j];
360 }
361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100363
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 /*
365 * XOR out with P_sha1(secret,label+random)[0..dlen]
366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
368 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100371 return( ret );
372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
374 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
375 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376
377 for( i = 0; i < dlen; i += 20 )
378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_md_hmac_reset ( &md_ctx );
380 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
381 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_md_hmac_reset ( &md_ctx );
384 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
385 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000386
387 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
388
389 for( j = 0; j < k; j++ )
390 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
391 }
392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_zeroize( tmp, sizeof( tmp ) );
396 mbedtls_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000397
398 return( 0 );
399}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
403static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100404 const unsigned char *secret, size_t slen,
405 const char *label,
406 const unsigned char *random, size_t rlen,
407 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000408{
409 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100410 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000411 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
413 const mbedtls_md_info_t *md_info;
414 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100415 int ret;
416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
420 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100423
424 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000426
427 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100428 memcpy( tmp + md_len, label, nb );
429 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000430 nb += rlen;
431
432 /*
433 * Compute P_<hash>(secret, label + random)[0..dlen]
434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100436 return( ret );
437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
439 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
440 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100441
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100442 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_md_hmac_reset ( &md_ctx );
445 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
446 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_md_hmac_reset ( &md_ctx );
449 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
450 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000451
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100452 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000453
454 for( j = 0; j < k; j++ )
455 dstbuf[i + j] = h_i[j];
456 }
457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_zeroize( tmp, sizeof( tmp ) );
461 mbedtls_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000462
463 return( 0 );
464}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100467static int tls_prf_sha256( const unsigned char *secret, size_t slen,
468 const char *label,
469 const unsigned char *random, size_t rlen,
470 unsigned char *dstbuf, size_t dlen )
471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100473 label, random, rlen, dstbuf, dlen ) );
474}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200478static int tls_prf_sha384( const unsigned char *secret, size_t slen,
479 const char *label,
480 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000481 unsigned char *dstbuf, size_t dlen )
482{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100484 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#endif /* MBEDTLS_SHA512_C */
487#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
492 defined(MBEDTLS_SSL_PROTO_TLS1_1)
493static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200494#endif
Paul Bakker380da532012-04-18 16:10:25 +0000495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496#if defined(MBEDTLS_SSL_PROTO_SSL3)
497static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
498static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200499#endif
500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
502static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
503static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200504#endif
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
507#if defined(MBEDTLS_SHA256_C)
508static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
509static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
510static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200511#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#if defined(MBEDTLS_SHA512_C)
514static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
515static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
516static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100517#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000521{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200522 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 unsigned char keyblk[256];
525 unsigned char *key1;
526 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100527 unsigned char *mac_enc;
528 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000529 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200530 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 const mbedtls_cipher_info_t *cipher_info;
532 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_ssl_session *session = ssl->session_negotiate;
535 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
536 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100541 if( cipher_info == NULL )
542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100544 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100546 }
547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100549 if( md_info == NULL )
550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100552 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100554 }
555
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000557 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559#if defined(MBEDTLS_SSL_PROTO_SSL3)
560 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000561 {
Paul Bakker48916f92012-09-16 19:57:18 +0000562 handshake->tls_prf = ssl3_prf;
563 handshake->calc_verify = ssl_calc_verify_ssl;
564 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000565 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200566 else
567#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
569 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000570 {
Paul Bakker48916f92012-09-16 19:57:18 +0000571 handshake->tls_prf = tls1_prf;
572 handshake->calc_verify = ssl_calc_verify_tls;
573 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000574 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200575 else
576#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
578#if defined(MBEDTLS_SHA512_C)
579 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
580 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000581 {
Paul Bakker48916f92012-09-16 19:57:18 +0000582 handshake->tls_prf = tls_prf_sha384;
583 handshake->calc_verify = ssl_calc_verify_tls_sha384;
584 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000585 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000586 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200587#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588#if defined(MBEDTLS_SHA256_C)
589 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000590 {
Paul Bakker48916f92012-09-16 19:57:18 +0000591 handshake->tls_prf = tls_prf_sha256;
592 handshake->calc_verify = ssl_calc_verify_tls_sha256;
593 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000594 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200595 else
596#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
600 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200601 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000602
603 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 * SSLv3:
605 * master =
606 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
607 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
608 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200609 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200610 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 * master = PRF( premaster, "master secret", randbytes )[0..47]
612 */
Paul Bakker0a597072012-09-25 21:55:46 +0000613 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000616 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
619 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200620 {
621 unsigned char session_hash[48];
622 size_t hash_len;
623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200625
626 ssl->handshake->calc_verify( ssl, session_hash );
627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
629 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200632 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200634 {
635 hash_len = 48;
636 }
637 else
638#endif
639 hash_len = 32;
640 }
641 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200643 hash_len = 36;
644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200646
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100647 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
648 "extended master secret",
649 session_hash, hash_len,
650 session->master, 48 );
651 if( ret != 0 )
652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100654 return( ret );
655 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200656
657 }
658 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200659#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100660 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
661 "master secret",
662 handshake->randbytes, 64,
663 session->master, 48 );
664 if( ret != 0 )
665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100667 return( ret );
668 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 }
672 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
675 /*
676 * Swap the client and server random values.
677 */
Paul Bakker48916f92012-09-16 19:57:18 +0000678 memcpy( tmp, handshake->randbytes, 64 );
679 memcpy( handshake->randbytes, tmp + 32, 32 );
680 memcpy( handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000682
683 /*
684 * SSLv3:
685 * key block =
686 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
687 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
688 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
689 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
690 * ...
691 *
692 * TLSv1:
693 * key block = PRF( master, "key expansion", randbytes )
694 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100695 ret = handshake->tls_prf( session->master, 48, "key expansion",
696 handshake->randbytes, 64, keyblk, 256 );
697 if( ret != 0 )
698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100700 return( ret );
701 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
704 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
705 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
706 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
707 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
711 /*
712 * Determine the appropriate key, IV and MAC length.
713 */
Paul Bakker68884e32013-01-07 18:20:04 +0100714
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200715 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
718 cipher_info->mode == MBEDTLS_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200720 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000721 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200722
Paul Bakker68884e32013-01-07 18:20:04 +0100723 transform->ivlen = 12;
724 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200725
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200726 /* Minimum length is expicit IV + tag */
727 transform->minlen = transform->ivlen - transform->fixed_ivlen
728 + ( transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100730 }
731 else
732 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200733 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
735 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200738 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100739 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200741 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000742 mac_key_len = mbedtls_md_get_size( md_info );
743 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200746 /*
747 * If HMAC is to be truncated, we shall keep the leftmost bytes,
748 * (rfc 6066 page 13 or rfc 2104 section 4),
749 * so we only need to adjust the length here.
750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000754
755#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
756 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000757 * HMAC implementation which also truncates the key
758 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000759 mac_key_len = transform->maclen;
760#endif
761 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200763
764 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100765 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000766
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200767 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200769 transform->minlen = transform->maclen;
770 else
Paul Bakker68884e32013-01-07 18:20:04 +0100771 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200772 /*
773 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100774 * 1. if EtM is in use: one block plus MAC
775 * otherwise: * first multiple of blocklen greater than maclen
776 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
779 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100780 {
781 transform->minlen = transform->maclen
782 + cipher_info->block_size;
783 }
784 else
785#endif
786 {
787 transform->minlen = transform->maclen
788 + cipher_info->block_size
789 - transform->maclen % cipher_info->block_size;
790 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
793 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
794 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200795 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100796 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200797#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
799 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
800 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200801 {
802 transform->minlen += transform->ivlen;
803 }
804 else
805#endif
806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
808 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200809 }
Paul Bakker68884e32013-01-07 18:20:04 +0100810 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000811 }
812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000814 transform->keylen, transform->minlen, transform->ivlen,
815 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000816
817 /*
818 * Finally setup the cipher contexts, IVs and MAC secrets.
819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200821 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000822 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000823 key1 = keyblk + mac_key_len * 2;
824 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000825
Paul Bakker68884e32013-01-07 18:20:04 +0100826 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000827 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000828
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000829 /*
830 * This is not used in TLS v1.1.
831 */
Paul Bakker48916f92012-09-16 19:57:18 +0000832 iv_copy_len = ( transform->fixed_ivlen ) ?
833 transform->fixed_ivlen : transform->ivlen;
834 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
835 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000836 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 }
838 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839#endif /* MBEDTLS_SSL_CLI_C */
840#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200841 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000842 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000843 key1 = keyblk + mac_key_len * 2 + transform->keylen;
844 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Hanno Becker81c7b182017-11-09 18:39:33 +0000846 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100847 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000848
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000849 /*
850 * This is not used in TLS v1.1.
851 */
Paul Bakker48916f92012-09-16 19:57:18 +0000852 iv_copy_len = ( transform->fixed_ivlen ) ?
853 transform->fixed_ivlen : transform->ivlen;
854 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
855 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000856 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100858 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
862 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100863 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#if defined(MBEDTLS_SSL_PROTO_SSL3)
866 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100867 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000868 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
871 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100872 }
873
Hanno Becker81c7b182017-11-09 18:39:33 +0000874 memcpy( transform->mac_enc, mac_enc, mac_key_len );
875 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100876 }
877 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#endif /* MBEDTLS_SSL_PROTO_SSL3 */
879#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
880 defined(MBEDTLS_SSL_PROTO_TLS1_2)
881 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100882 {
Gilles Peskine21701302018-03-19 19:06:08 +0100883 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
884 For AEAD-based ciphersuites, there is nothing to do here. */
885 if( mac_key_len != 0 )
886 {
887 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
888 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
889 }
Paul Bakker68884e32013-01-07 18:20:04 +0100890 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200891 else
892#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
895 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200896 }
Paul Bakker68884e32013-01-07 18:20:04 +0100897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
899 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100904 transform->iv_enc, transform->iv_dec,
905 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100906 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +0000907 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
910 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000911 }
912 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000914
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200915#if defined(MBEDTLS_SSL_EXPORT_KEYS)
916 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100917 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200918 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
919 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +0000920 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100921 iv_copy_len );
922 }
923#endif
924
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200925 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200926 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000927 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200929 return( ret );
930 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200931
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200932 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200933 cipher_info ) ) != 0 )
934 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200935 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200936 return( ret );
937 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200940 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200944 return( ret );
945 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200948 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200952 return( ret );
953 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#if defined(MBEDTLS_CIPHER_MODE_CBC)
956 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
959 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200962 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200963 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
966 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200969 return( ret );
970 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000971 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 mbedtls_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +0000977 // Initialize compression
978 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000980 {
Paul Bakker16770332013-10-11 09:59:44 +0200981 if( ssl->compress_buf == NULL )
982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200984 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +0200985 if( ssl->compress_buf == NULL )
986 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 MBEDTLS_SSL_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200989 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +0200990 }
991 }
992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000994
Paul Bakker48916f92012-09-16 19:57:18 +0000995 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
996 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000997
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200998 if( deflateInit( &transform->ctx_deflate,
999 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001000 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1003 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001004 }
1005 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001009
1010 return( 0 );
1011}
1012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#if defined(MBEDTLS_SSL_PROTO_SSL3)
1014void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001015{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001016 mbedtls_md5_context md5;
1017 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 unsigned char pad_1[48];
1019 unsigned char pad_2[48];
1020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001022
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001023 mbedtls_md5_init( &md5 );
1024 mbedtls_sha1_init( &sha1 );
1025
1026 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1027 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Paul Bakker380da532012-04-18 16:10:25 +00001029 memset( pad_1, 0x36, 48 );
1030 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001032 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1033 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1034 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001035
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001036 mbedtls_md5_starts_ret( &md5 );
1037 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1038 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1039 mbedtls_md5_update_ret( &md5, hash, 16 );
1040 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001042 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1043 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1044 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001045
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001046 mbedtls_sha1_starts_ret( &sha1 );
1047 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1048 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1049 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1050 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001054
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001055 mbedtls_md5_free( &md5 );
1056 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001057
Paul Bakker380da532012-04-18 16:10:25 +00001058 return;
1059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1063void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001064{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001065 mbedtls_md5_context md5;
1066 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001069
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001070 mbedtls_md5_init( &md5 );
1071 mbedtls_sha1_init( &sha1 );
1072
1073 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1074 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001075
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001076 mbedtls_md5_finish_ret( &md5, hash );
1077 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001081
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001082 mbedtls_md5_free( &md5 );
1083 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001084
Paul Bakker380da532012-04-18 16:10:25 +00001085 return;
1086}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1090#if defined(MBEDTLS_SHA256_C)
1091void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001092{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001093 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001094
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001095 mbedtls_sha256_init( &sha256 );
1096
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001098
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001099 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001100 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001104
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001105 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001106
Paul Bakker380da532012-04-18 16:10:25 +00001107 return;
1108}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#if defined(MBEDTLS_SHA512_C)
1112void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001113{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001114 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001115
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001116 mbedtls_sha512_init( &sha512 );
1117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001119
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001120 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001121 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001126 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001127
Paul Bakker5121ce52009-01-03 21:22:43 +00001128 return;
1129}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130#endif /* MBEDTLS_SHA512_C */
1131#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1134int 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 +02001135{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001136 unsigned char *p = ssl->handshake->premaster;
1137 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001138 const unsigned char *psk = ssl->conf->psk;
1139 size_t psk_len = ssl->conf->psk_len;
1140
1141 /* If the psk callback was called, use its result */
1142 if( ssl->handshake->psk != NULL )
1143 {
1144 psk = ssl->handshake->psk;
1145 psk_len = ssl->handshake->psk_len;
1146 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001147
1148 /*
1149 * PMS = struct {
1150 * opaque other_secret<0..2^16-1>;
1151 * opaque psk<0..2^16-1>;
1152 * };
1153 * with "other_secret" depending on the particular key exchange
1154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1156 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001157 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001158 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001160
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001161 *(p++) = (unsigned char)( psk_len >> 8 );
1162 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001163
1164 if( end < p || (size_t)( end - p ) < psk_len )
1165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1166
1167 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001168 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001169 }
1170 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1172#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1173 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001174 {
1175 /*
1176 * other_secret already set by the ClientKeyExchange message,
1177 * and is 48 bytes long
1178 */
Philippe Antoine33e5c322018-07-09 10:39:02 +02001179 if( end - p < 2 )
1180 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1181
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001182 *p++ = 0;
1183 *p++ = 48;
1184 p += 48;
1185 }
1186 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1188#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1189 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001190 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001191 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001192 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001193
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001194 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001196 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001197 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001200 return( ret );
1201 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001202 *(p++) = (unsigned char)( len >> 8 );
1203 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001204 p += len;
1205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001207 }
1208 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1210#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1211 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001212 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001213 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001214 size_t zlen;
1215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001217 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001218 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001221 return( ret );
1222 }
1223
1224 *(p++) = (unsigned char)( zlen >> 8 );
1225 *(p++) = (unsigned char)( zlen );
1226 p += zlen;
1227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001229 }
1230 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1234 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001235 }
1236
1237 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001238 if( end - p < 2 )
1239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001240
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001241 *(p++) = (unsigned char)( psk_len >> 8 );
1242 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001243
1244 if( end < p || (size_t)( end - p ) < psk_len )
1245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1246
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001247 memcpy( p, psk, psk_len );
1248 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001249
1250 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1251
1252 return( 0 );
1253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001257/*
1258 * SSLv3.0 MAC functions
1259 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001260#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001261static void ssl_mac( mbedtls_md_context_t *md_ctx,
1262 const unsigned char *secret,
1263 const unsigned char *buf, size_t len,
1264 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001265 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001266{
1267 unsigned char header[11];
1268 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001269 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1271 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001272
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001273 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001275 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001276 else
Paul Bakker68884e32013-01-07 18:20:04 +01001277 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001278
1279 memcpy( header, ctr, 8 );
1280 header[ 8] = (unsigned char) type;
1281 header[ 9] = (unsigned char)( len >> 8 );
1282 header[10] = (unsigned char)( len );
1283
Paul Bakker68884e32013-01-07 18:20:04 +01001284 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_md_starts( md_ctx );
1286 mbedtls_md_update( md_ctx, secret, md_size );
1287 mbedtls_md_update( md_ctx, padding, padlen );
1288 mbedtls_md_update( md_ctx, header, 11 );
1289 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001290 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Paul Bakker68884e32013-01-07 18:20:04 +01001292 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_md_starts( md_ctx );
1294 mbedtls_md_update( md_ctx, secret, md_size );
1295 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001296 mbedtls_md_update( md_ctx, out, md_size );
1297 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001298}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001302 defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001303#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001304#endif
1305
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02001306/* The function below is only used in the Lucky 13 counter-measure in
1307 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1308#if defined(SSL_SOME_MODES_USE_MAC) && \
1309 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1310 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1311 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1312/* This function makes sure every byte in the memory region is accessed
1313 * (in ascending addresses order) */
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001314static void ssl_read_memory( const unsigned char *p, size_t len )
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02001315{
1316 unsigned char acc = 0;
1317 volatile unsigned char force;
1318
1319 for( ; len != 0; p++, len-- )
1320 acc ^= *p;
1321
1322 force = acc;
1323 (void) force;
1324}
1325#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1326
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001327/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001331{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001333 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001337 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1340 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001341 }
1342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001346 ssl->out_msg, ssl->out_msglen );
1347
Hanno Beckerd33f1ca2017-09-18 10:55:31 +01001348 if( ssl->out_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
1349 {
Hanno Becker184f6752017-10-04 13:47:33 +01001350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1351 (unsigned) ssl->out_msglen,
Hanno Beckerd33f1ca2017-09-18 10:55:31 +01001352 MBEDTLS_SSL_MAX_CONTENT_LEN ) );
1353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1354 }
1355
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001357 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001359#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 if( mode == MBEDTLS_MODE_STREAM ||
1361 ( mode == MBEDTLS_MODE_CBC
1362#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1363 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001364#endif
1365 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_SSL_PROTO_SSL3)
1368 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001369 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001370 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001371
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001372 ssl_mac( &ssl->transform_out->md_ctx_enc,
1373 ssl->transform_out->mac_enc,
1374 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001375 ssl->out_ctr, ssl->out_msgtype,
1376 mac );
1377
1378 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001379 }
1380 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001381#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1383 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1384 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001385 {
Hanno Becker992b6872017-11-09 18:57:39 +00001386 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1389 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1390 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1391 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001392 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001393 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001395
1396 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001397 }
1398 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001399#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1402 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001403 }
1404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001406 ssl->out_msg + ssl->out_msglen,
1407 ssl->transform_out->maclen );
1408
1409 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001410 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001411 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001412#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001413
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001414 /*
1415 * Encrypt
1416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1418 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001420 int ret;
1421 size_t olen = 0;
1422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 "including %d bytes of padding",
1425 ssl->out_msglen, 0 ) );
1426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001428 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001429 ssl->transform_out->ivlen,
1430 ssl->out_msg, ssl->out_msglen,
1431 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001434 return( ret );
1435 }
1436
1437 if( ssl->out_msglen != olen )
1438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1440 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001441 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 }
Paul Bakker68884e32013-01-07 18:20:04 +01001443 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1445#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1446 if( mode == MBEDTLS_MODE_GCM ||
1447 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001448 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001449 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001450 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001451 unsigned char *enc_msg;
1452 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001453 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001455
Paul Bakkerca4ab492012-04-18 14:23:57 +00001456 memcpy( add_data, ssl->out_ctr, 8 );
1457 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001459 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001460 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1461 add_data[12] = ssl->out_msglen & 0xFF;
1462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakkerca4ab492012-04-18 14:23:57 +00001464 add_data, 13 );
1465
Paul Bakker68884e32013-01-07 18:20:04 +01001466 /*
1467 * Generate IV
1468 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001469 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1470 {
1471 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1473 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001474 }
1475
1476 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1477 ssl->out_ctr, 8 );
1478 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001481 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001482
Paul Bakker68884e32013-01-07 18:20:04 +01001483 /*
1484 * Fix pointer positions and message length with added IV
1485 */
1486 enc_msg = ssl->out_msg;
1487 enc_msglen = ssl->out_msglen;
1488 ssl->out_msglen += ssl->transform_out->ivlen -
1489 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker68884e32013-01-07 18:20:04 +01001492 "including %d bytes of padding",
1493 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001494
Paul Bakker68884e32013-01-07 18:20:04 +01001495 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001496 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 if( ( ret = mbedtls_cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001499 ssl->transform_out->iv_enc,
1500 ssl->transform_out->ivlen,
1501 add_data, 13,
1502 enc_msg, enc_msglen,
1503 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001504 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001507 return( ret );
1508 }
1509
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001510 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1513 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001514 }
1515
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001516 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001517 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001520 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001521 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001523#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001525 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001526 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001527 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001528 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001529
Paul Bakker48916f92012-09-16 19:57:18 +00001530 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1531 ssl->transform_out->ivlen;
1532 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 padlen = 0;
1534
1535 for( i = 0; i <= padlen; i++ )
1536 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1537
1538 ssl->out_msglen += padlen + 1;
1539
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001540 enc_msglen = ssl->out_msglen;
1541 enc_msg = ssl->out_msg;
1542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001544 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001545 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1546 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001549 {
1550 /*
1551 * Generate IV
1552 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001553 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001554 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001555 if( ret != 0 )
1556 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001557
Paul Bakker92be97b2013-01-02 17:30:03 +01001558 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001559 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001560
1561 /*
1562 * Fix pointer positions and message length with added IV
1563 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001564 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001565 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001566 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001567 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001571 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001572 ssl->out_msglen, ssl->transform_out->ivlen,
1573 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001576 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001577 ssl->transform_out->ivlen,
1578 enc_msg, enc_msglen,
1579 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001582 return( ret );
1583 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001584
Paul Bakkercca5b812013-08-31 17:40:26 +02001585 if( enc_msglen != olen )
1586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001589 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1592 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001593 {
1594 /*
1595 * Save IV in SSL3 and TLS1
1596 */
1597 memcpy( ssl->transform_out->iv_enc,
1598 ssl->transform_out->cipher_ctx_enc.iv,
1599 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001600 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001601#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001604 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001605 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001606 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1607
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001608 /*
1609 * MAC(MAC_write_key, seq_num +
1610 * TLSCipherText.type +
1611 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001612 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001613 * IV + // except for TLS 1.0
1614 * ENC(content + padding + padding_length));
1615 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001616 unsigned char pseudo_hdr[13];
1617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001619
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001620 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1621 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001622 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1623 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1628 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001629 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001630 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001632
Hanno Becker3d8c9072018-01-05 16:24:22 +00001633 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1634 ssl->transform_out->maclen );
1635
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001636 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001637 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001638 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001640 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001641 else
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001642#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1645 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001646 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001647
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001648 /* Make extra sure authentication was performed, exactly once */
1649 if( auth_done != 1 )
1650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1652 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001653 }
1654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001656
1657 return( 0 );
1658}
1659
Manuel Pégourié-Gonnard41df0f22020-07-28 11:35:39 +02001660#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001661/*
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001662 * Constant-flow conditional memcpy:
1663 * - if c1 == c2, equivalent to memcpy(dst, src, len),
1664 * - otherwise, a no-op,
1665 * but with execution flow independent of the values of c1 and c2.
1666 *
1667 * Use only bit operations to avoid branches that could be used by some
1668 * compilers on some platforms to translate comparison operators.
1669 */
Manuel Pégourié-Gonnard2da9a542020-07-28 11:56:05 +02001670static void mbedtls_ssl_cf_memcpy_if_eq( unsigned char *dst,
1671 const unsigned char *src,
1672 size_t len,
1673 size_t c1, size_t c2 )
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001674{
1675 /* diff = 0 if c1 == c2, non-zero otherwise */
1676 const size_t diff = c1 ^ c2;
1677
1678 /* MSVC has a warning about unary minus on unsigned integer types,
1679 * but this is well-defined and precisely what we want to do here. */
1680#if defined(_MSC_VER)
1681#pragma warning( push )
1682#pragma warning( disable : 4146 )
1683#endif
1684
Manuel Pégourié-Gonnard2f484bd2020-07-28 11:57:25 +02001685 /* diff_msb's most significant bit is equal to c1 != c2 */
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001686 const size_t diff_msb = ( diff | -diff );
1687
1688 /* diff1 = c1 != c2 */
1689 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
1690
1691 /* mask = c1 != c2 ? 0xff : 0x00 */
Manuel Pégourié-Gonnard2f484bd2020-07-28 11:57:25 +02001692 const unsigned char mask = (unsigned char) -diff1;
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001693
1694#if defined(_MSC_VER)
1695#pragma warning( pop )
1696#endif
1697
1698 /* dst[i] = c1 != c2 ? dst[i] : src[i] */
Manuel Pégourié-Gonnarde05e5762020-07-29 10:04:36 +02001699 size_t i;
1700 for( i = 0; i < len; i++ )
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001701 dst[i] = ( dst[i] & mask ) | ( src[i] & ~mask );
1702}
1703
1704/*
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001705 * Compute HMAC of variable-length data with constant flow.
Manuel Pégourié-Gonnard7cf5ebc2020-07-29 12:54:04 +02001706 *
1707 * Only works with MD-5, SHA-1, SHA-256 and SHA-384.
1708 * (Otherwise, computation of block_size needs to be adapted.)
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001709 */
1710int mbedtls_ssl_cf_hmac(
1711 mbedtls_md_context_t *ctx,
1712 const unsigned char *add_data, size_t add_data_len,
1713 const unsigned char *data, size_t data_len_secret,
1714 size_t min_data_len, size_t max_data_len,
1715 unsigned char *output )
1716{
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001717 /*
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001718 * This function breaks the HMAC abstraction and uses the md_clone()
1719 * extension to the MD API in order to get constant-flow behaviour.
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001720 *
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001721 * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
Manuel Pégourié-Gonnardec956b12020-07-28 11:42:31 +02001722 * concatenation, and okey/ikey are the XOR of the key with some fixed bit
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001723 * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001724 *
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001725 * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
1726 * minlen, then cloning the context, and for each byte up to maxlen
1727 * finishing up the hash computation, keeping only the correct result.
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001728 *
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001729 * Then we only need to compute HASH(okey + inner_hash) and we're done.
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001730 */
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001731 const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info );
Manuel Pégourié-Gonnardec956b12020-07-28 11:42:31 +02001732 /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5,
1733 * all of which have the same block size except SHA-384. */
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001734 const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
Manuel Pégourié-Gonnardc9ef5a22020-07-28 11:45:02 +02001735 const unsigned char * const ikey = ctx->hmac_ctx;
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001736 const unsigned char * const okey = ikey + block_size;
1737 const size_t hash_size = mbedtls_md_get_size( ctx->md_info );
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001738
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001739 unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
1740 mbedtls_md_context_t aux;
1741 size_t offset;
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001742 int ret;
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001743
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001744 mbedtls_md_init( &aux );
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001745
1746#define MD_CHK( func_call ) \
1747 do { \
1748 ret = (func_call); \
1749 if( ret != 0 ) \
1750 goto cleanup; \
1751 } while( 0 )
1752
1753 MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) );
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001754
1755 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
1756 * so we can start directly with the message */
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001757 MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) );
1758 MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) );
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001759
1760 /* For each possible length, compute the hash up to that point */
1761 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001762 {
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001763 MD_CHK( mbedtls_md_clone( &aux, ctx ) );
1764 MD_CHK( mbedtls_md_finish( &aux, aux_out ) );
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001765 /* Keep only the correct inner_hash in the output buffer */
1766 mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size,
1767 offset, data_len_secret );
1768
1769 if( offset < max_data_len )
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001770 MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) );
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001771 }
1772
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001773 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001774 MD_CHK( mbedtls_md_starts( ctx ) );
1775 MD_CHK( mbedtls_md_update( ctx, okey, block_size ) );
1776 MD_CHK( mbedtls_md_update( ctx, output, hash_size ) );
1777 MD_CHK( mbedtls_md_finish( ctx, output ) );
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001778
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001779 /* Done, get ready for next time */
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001780 MD_CHK( mbedtls_md_hmac_reset( ctx ) );
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001781
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001782#undef MD_CHK
1783
1784cleanup:
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001785 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001786 return( ret );
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001787}
Manuel Pégourié-Gonnard41df0f22020-07-28 11:35:39 +02001788#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001791{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001792 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001794 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001795#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001796 size_t padlen = 0, correct = 1;
1797#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001800
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001801 if( ssl->session_in == NULL || ssl->transform_in == NULL )
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 );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001805 }
1806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001808
Paul Bakker48916f92012-09-16 19:57:18 +00001809 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001812 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001814 }
1815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1817 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001818 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001819 int ret;
1820 size_t olen = 0;
1821
Paul Bakker68884e32013-01-07 18:20:04 +01001822 padlen = 0;
1823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001825 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001826 ssl->transform_in->ivlen,
1827 ssl->in_msg, ssl->in_msglen,
1828 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001831 return( ret );
1832 }
1833
1834 if( ssl->in_msglen != olen )
1835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1837 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001839 }
Paul Bakker68884e32013-01-07 18:20:04 +01001840 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1842#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1843 if( mode == MBEDTLS_MODE_GCM ||
1844 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001845 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001846 int ret;
1847 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001848 unsigned char *dec_msg;
1849 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001850 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001851 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001853 size_t explicit_iv_len = ssl->transform_in->ivlen -
1854 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001855
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001856 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001859 "+ taglen (%d)", ssl->in_msglen,
1860 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001862 }
1863 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1864
Paul Bakker68884e32013-01-07 18:20:04 +01001865 dec_msg = ssl->in_msg;
1866 dec_msg_result = ssl->in_msg;
1867 ssl->in_msglen = dec_msglen;
1868
1869 memcpy( add_data, ssl->in_ctr, 8 );
1870 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001872 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001873 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1874 add_data[12] = ssl->in_msglen & 0xFF;
1875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakker68884e32013-01-07 18:20:04 +01001877 add_data, 13 );
1878
1879 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1880 ssl->in_iv,
1881 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
Paul Bakker68884e32013-01-07 18:20:04 +01001884 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001886
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001887 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001888 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001891 ssl->transform_in->iv_dec,
1892 ssl->transform_in->ivlen,
1893 add_data, 13,
1894 dec_msg, dec_msglen,
1895 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001896 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1901 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001902
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001903 return( ret );
1904 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001905 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001906
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001907 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1910 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001911 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001912 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001913 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001915#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001917 {
Paul Bakker45829992013-01-03 14:52:21 +01001918 /*
1919 * Decrypt and check the padding
1920 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001921 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001922 unsigned char *dec_msg;
1923 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001924 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001925 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001926 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001927
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 /*
Paul Bakker45829992013-01-03 14:52:21 +01001929 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1932 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001933 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001934#endif
Paul Bakker45829992013-01-03 14:52:21 +01001935
1936 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1937 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001940 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1941 ssl->transform_in->ivlen,
1942 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001944 }
1945
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001946 dec_msglen = ssl->in_msglen;
1947 dec_msg = ssl->in_msg;
1948 dec_msg_result = ssl->in_msg;
1949
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001950 /*
1951 * Authenticate before decrypt if enabled
1952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1954 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001955 {
Hanno Becker992b6872017-11-09 18:57:39 +00001956 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001957 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001960
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001961 dec_msglen -= ssl->transform_in->maclen;
1962 ssl->in_msglen -= ssl->transform_in->maclen;
1963
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001964 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1965 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1966 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1967 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1972 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001973 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001974 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001978 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001979 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001980 ssl->transform_in->maclen );
1981
Hanno Becker992b6872017-11-09 18:57:39 +00001982 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1983 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001988 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001989 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001990 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001992
1993 /*
1994 * Check length sanity
1995 */
1996 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001999 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002001 }
2002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002004 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002005 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002006 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002008 {
Paul Bakker48916f92012-09-16 19:57:18 +00002009 dec_msglen -= ssl->transform_in->ivlen;
2010 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002011
Paul Bakker48916f92012-09-16 19:57:18 +00002012 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002013 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002014 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002018 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002019 ssl->transform_in->ivlen,
2020 dec_msg, dec_msglen,
2021 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002024 return( ret );
2025 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002026
Paul Bakkercca5b812013-08-31 17:40:26 +02002027 if( dec_msglen != olen )
2028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2030 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002031 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2034 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002035 {
2036 /*
2037 * Save IV in SSL3 and TLS1
2038 */
2039 memcpy( ssl->transform_in->iv_dec,
2040 ssl->transform_in->cipher_ctx_dec.iv,
2041 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002042 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002044
2045 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002046
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002047 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002048 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050#if defined(MBEDTLS_SSL_DEBUG_ALL)
2051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002052 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002053#endif
Paul Bakker45829992013-01-03 14:52:21 +01002054 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002055 correct = 0;
2056 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058#if defined(MBEDTLS_SSL_PROTO_SSL3)
2059 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 {
Paul Bakker48916f92012-09-16 19:57:18 +00002061 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063#if defined(MBEDTLS_SSL_DEBUG_ALL)
2064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002066 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002067#endif
Paul Bakker45829992013-01-03 14:52:21 +01002068 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 }
2070 }
2071 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2073#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2074 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2075 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002076 {
2077 /*
Paul Bakker45829992013-01-03 14:52:21 +01002078 * TLSv1+: always check the padding up to the first failure
2079 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002081 size_t pad_count = 0, real_count = 1;
Angus Gratton1ba8e912018-06-19 15:57:50 +10002082 size_t padding_idx = ssl->in_msglen - padlen;
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 Gratton1ba8e912018-06-19 15:57:50 +10002086 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002087 *
Angus Gratton1ba8e912018-06-19 15:57:50 +10002088 * 2. padding_idx > MBEDTLS_SSL_MAX_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 Gratton1ba8e912018-06-19 15:57:50 +10002094 correct &= ( padlen <= ssl->in_msglen );
2095 correct &= ( padding_idx <= MBEDTLS_SSL_MAX_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 Gratton1ba8e912018-06-19 15:57:50 +10002100 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002101 {
Angus Gratton1ba8e912018-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é-Gonnard8ebb88d2020-07-28 09:55:33 +02002126#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2129 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002130 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002131
Manuel Pégourié-Gonnard7c344322018-07-10 11:15:36 +02002132#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard7c344322018-07-10 11:15:36 +02002135#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002136
2137 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002138 * Authenticate if not done yet.
2139 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002141#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002142 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002143 {
Hanno Becker992b6872017-11-09 18:57:39 +00002144 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002145
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002146 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002147
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002148 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2149 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151#if defined(MBEDTLS_SSL_PROTO_SSL3)
2152 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002153 {
2154 ssl_mac( &ssl->transform_in->md_ctx_dec,
2155 ssl->transform_in->mac_dec,
2156 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002157 ssl->in_ctr, ssl->in_msgtype,
2158 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002159 }
2160 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2162#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2163 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2164 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002165 {
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002166 int ret;
2167 unsigned char add_data[13];
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002168
2169 /*
2170 * The next two sizes are the minimum and maximum values of
2171 * in_msglen over all padlen values.
2172 *
2173 * They're independent of padlen, since we previously did
2174 * in_msglen -= padlen.
2175 *
2176 * Note that max_len + maclen is never more than the buffer
2177 * length, as we previously did in_msglen -= maclen too.
2178 */
2179 const size_t max_len = ssl->in_msglen + padlen;
2180 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2181
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002182 memcpy( add_data + 0, ssl->in_ctr, 8 );
2183 memcpy( add_data + 8, ssl->in_hdr, 3 );
2184 memcpy( add_data + 11, ssl->in_len, 2 );
2185
2186 ret = mbedtls_ssl_cf_hmac( &ssl->transform_in->md_ctx_dec,
2187 add_data, sizeof( add_data ),
2188 ssl->in_msg, ssl->in_msglen,
2189 min_len, max_len,
2190 mac_expect );
2191 if( ret != 0 )
Gilles Peskinebb07ca02018-06-06 17:23:31 +02002192 {
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
2194 return( ret );
Gilles Peskinebb07ca02018-06-06 17:23:31 +02002195 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002196
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002197 /* Make sure we access all the memory that could contain the MAC,
2198 * before we check it in the next code block. This makes the
2199 * synchronisation requirements for just-in-time Prime+Probe
2200 * attacks much tighter and hopefully impractical. */
2201 ssl_read_memory( ssl->in_msg + min_len,
2202 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002203 }
2204 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2206 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2209 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002210 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002211
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002212#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002213 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2214 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2215 ssl->transform_in->maclen );
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002216#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002217
Hanno Becker992b6872017-11-09 18:57:39 +00002218 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2219 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221#if defined(MBEDTLS_SSL_DEBUG_ALL)
2222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002223#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002224 correct = 0;
2225 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002226 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002227 }
Hanno Beckerca31b472018-10-17 14:43:14 +01002228
2229 /*
2230 * Finally check the correct flag
2231 */
2232 if( correct == 0 )
2233 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002234#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002235
2236 /* Make extra sure authentication was performed, exactly once */
2237 if( auth_done != 1 )
2238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2240 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002241 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002242
2243 if( ssl->in_msglen == 0 )
2244 {
Angus Grattonb91cb6e2018-06-19 15:58:22 +10002245#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2246 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2247 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2248 {
2249 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2251 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2252 }
2253#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2254
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 ssl->nb_zero++;
2256
2257 /*
2258 * Three or more empty messages may be a DoS attack
2259 * (excessive CPU consumption).
2260 */
2261 if( ssl->nb_zero > 3 )
2262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 }
2267 }
2268 else
2269 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002272 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002273 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002274 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002275 }
2276 else
2277#endif
2278 {
2279 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2280 if( ++ssl->in_ctr[i - 1] != 0 )
2281 break;
2282
2283 /* The loop goes to its end iff the counter is wrapping */
2284 if( i == ssl_ep_len( ssl ) )
2285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2287 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002288 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002289 }
2290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002292
2293 return( 0 );
2294}
2295
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002296#undef MAC_NONE
2297#undef MAC_PLAINTEXT
2298#undef MAC_CIPHERTEXT
2299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002301/*
2302 * Compression/decompression functions
2303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002305{
2306 int ret;
2307 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002308 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002309 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002310 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002313
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002314 if( len_pre == 0 )
2315 return( 0 );
2316
Paul Bakker2770fbd2012-07-03 13:30:23 +00002317 memcpy( msg_pre, ssl->out_msg, len_pre );
2318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002320 ssl->out_msglen ) );
2321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002323 ssl->out_msg, ssl->out_msglen );
2324
Paul Bakker48916f92012-09-16 19:57:18 +00002325 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2326 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2327 ssl->transform_out->ctx_deflate.next_out = msg_post;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002328 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002329
Paul Bakker48916f92012-09-16 19:57:18 +00002330 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002331 if( ret != Z_OK )
2332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2334 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002335 }
2336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 ssl->out_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002338 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002341 ssl->out_msglen ) );
2342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002344 ssl->out_msg, ssl->out_msglen );
2345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002347
2348 return( 0 );
2349}
2350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002352{
2353 int ret;
2354 unsigned char *msg_post = ssl->in_msg;
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002355 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002356 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002357 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002360
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002361 if( len_pre == 0 )
2362 return( 0 );
2363
Paul Bakker2770fbd2012-07-03 13:30:23 +00002364 memcpy( msg_pre, ssl->in_msg, len_pre );
2365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002367 ssl->in_msglen ) );
2368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002370 ssl->in_msg, ssl->in_msglen );
2371
Paul Bakker48916f92012-09-16 19:57:18 +00002372 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2373 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2374 ssl->transform_in->ctx_inflate.next_out = msg_post;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002375 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002376 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002377
Paul Bakker48916f92012-09-16 19:57:18 +00002378 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002379 if( ret != Z_OK )
2380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2382 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002383 }
2384
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002385 ssl->in_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002386 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002389 ssl->in_msglen ) );
2390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002392 ssl->in_msg, ssl->in_msglen );
2393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002395
2396 return( 0 );
2397}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2401static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403#if defined(MBEDTLS_SSL_PROTO_DTLS)
2404static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002405{
2406 /* If renegotiation is not enforced, retransmit until we would reach max
2407 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002408 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002409 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002410 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002411 unsigned char doublings = 1;
2412
2413 while( ratio != 0 )
2414 {
2415 ++doublings;
2416 ratio >>= 1;
2417 }
2418
2419 if( ++ssl->renego_records_seen > doublings )
2420 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002422 return( 0 );
2423 }
2424 }
2425
2426 return( ssl_write_hello_request( ssl ) );
2427}
2428#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002430
Paul Bakker5121ce52009-01-03 21:22:43 +00002431/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002432 * Fill the input message buffer by appending data to it.
2433 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002434 *
2435 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2436 * available (from this read and/or a previous one). Otherwise, an error code
2437 * is returned (possibly EOF or WANT_READ).
2438 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002439 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2440 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2441 * since we always read a whole datagram at once.
2442 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002443 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002444 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002447{
Paul Bakker23986e52011-04-24 08:57:21 +00002448 int ret;
2449 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002453 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002456 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002458 }
2459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 if( nb_want > MBEDTLS_SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2463 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002464 }
2465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002467 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002469 uint32_t timeout;
2470
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002471 /* Just to be sure */
2472 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2473 {
2474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2475 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2476 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2477 }
2478
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002479 /*
2480 * The point is, we need to always read a full datagram at once, so we
2481 * sometimes read more then requested, and handle the additional data.
2482 * It could be the rest of the current record (while fetching the
2483 * header) and/or some other records in the same datagram.
2484 */
2485
2486 /*
2487 * Move to the next record in the already read datagram if applicable
2488 */
2489 if( ssl->next_record_offset != 0 )
2490 {
2491 if( ssl->in_left < ssl->next_record_offset )
2492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2494 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002495 }
2496
2497 ssl->in_left -= ssl->next_record_offset;
2498
2499 if( ssl->in_left != 0 )
2500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002502 ssl->next_record_offset ) );
2503 memmove( ssl->in_hdr,
2504 ssl->in_hdr + ssl->next_record_offset,
2505 ssl->in_left );
2506 }
2507
2508 ssl->next_record_offset = 0;
2509 }
2510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002513
2514 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002515 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002516 */
2517 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002520 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002521 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002522
2523 /*
Antonin Décimo8fd91562019-01-23 15:24:37 +01002524 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002525 * are not at the beginning of a new record, the caller did something
2526 * wrong.
2527 */
2528 if( ssl->in_left != 0 )
2529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2531 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002532 }
2533
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002534 /*
2535 * Don't even try to read if time's out already.
2536 * This avoids by-passing the timer when repeatedly receiving messages
2537 * that will end up being dropped.
2538 */
2539 if( ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002540 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002541 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543 len = MBEDTLS_SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002546 timeout = ssl->handshake->retransmit_timeout;
2547 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002548 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002551
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002552 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002553 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2554 timeout );
2555 else
2556 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002559
2560 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002562 }
2563
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002564 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002567 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002570 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002571 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002574 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002575 }
2576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002580 return( ret );
2581 }
2582
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002583 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002584 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002586 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002588 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002589 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002592 return( ret );
2593 }
2594
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002595 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002596 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002598 }
2599
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 if( ret < 0 )
2601 return( ret );
2602
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002603 ssl->in_left = ret;
2604 }
2605 else
2606#endif
2607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002609 ssl->in_left, nb_want ) );
2610
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002611 while( ssl->in_left < nb_want )
2612 {
2613 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002614
2615 if( ssl_check_timer( ssl ) != 0 )
2616 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2617 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002618 {
2619 if( ssl->f_recv_timeout != NULL )
2620 {
2621 ret = ssl->f_recv_timeout( ssl->p_bio,
2622 ssl->in_hdr + ssl->in_left, len,
2623 ssl->conf->read_timeout );
2624 }
2625 else
2626 {
2627 ret = ssl->f_recv( ssl->p_bio,
2628 ssl->in_hdr + ssl->in_left, len );
2629 }
2630 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002633 ssl->in_left, nb_want ) );
2634 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002635
2636 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002638
2639 if( ret < 0 )
2640 return( ret );
2641
makise-homura329fe7e2020-08-24 18:39:56 +03002642 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad1603b11af862018-03-19 07:18:13 -07002643 {
Jaeden Amerob5f53b12018-04-03 12:09:45 +01002644 MBEDTLS_SSL_DEBUG_MSG( 1,
2645 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160329ed80f2018-04-02 07:34:26 -07002646 ret, (unsigned long)len ) );
mohammad1603b11af862018-03-19 07:18:13 -07002647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2648 }
2649
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002650 ssl->in_left += ret;
2651 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 }
2653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
2656 return( 0 );
2657}
2658
2659/*
2660 * Flush any data not yet written
2661 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002663{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002664 int ret;
2665 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002669 if( ssl->f_send == NULL )
2670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002672 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002674 }
2675
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002676 /* Avoid incrementing counter if data is flushed */
2677 if( ssl->out_left == 0 )
2678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002680 return( 0 );
2681 }
2682
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 while( ssl->out_left > 0 )
2684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2686 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) +
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002689 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002690 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
2694 if( ret <= 0 )
2695 return( ret );
2696
makise-homura329fe7e2020-08-24 18:39:56 +03002697 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16036085c722018-02-22 04:29:04 -08002698 {
Jaeden Amerob5f53b12018-04-03 12:09:45 +01002699 MBEDTLS_SSL_DEBUG_MSG( 1,
2700 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160329ed80f2018-04-02 07:34:26 -07002701 ret, (unsigned long)ssl->out_left ) );
mohammad16036085c722018-02-22 04:29:04 -08002702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2703 }
2704
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 ssl->out_left -= ret;
2706 }
2707
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002708 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002709 if( ++ssl->out_ctr[i - 1] != 0 )
2710 break;
2711
2712 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002713 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2716 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002717 }
2718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
2721 return( 0 );
2722}
2723
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002724/*
2725 * Functions to handle the DTLS retransmission state machine
2726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002728/*
2729 * Append current handshake message to current outgoing flight
2730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002732{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 mbedtls_ssl_flight_item *msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002734
2735 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002736 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002737 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002740 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002741 }
2742
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002743 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002744 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002747 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002748 }
2749
2750 /* Copy current handshake message with headers */
2751 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2752 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002753 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002754 msg->next = NULL;
2755
2756 /* Append to the current flight */
2757 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002758 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002759 else
2760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002762 while( cur->next != NULL )
2763 cur = cur->next;
2764 cur->next = msg;
2765 }
2766
2767 return( 0 );
2768}
2769
2770/*
2771 * Free the current flight of handshake messages
2772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002774{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 mbedtls_ssl_flight_item *cur = flight;
2776 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002777
2778 while( cur != NULL )
2779 {
2780 next = cur->next;
2781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 mbedtls_free( cur->p );
2783 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002784
2785 cur = next;
2786 }
2787}
2788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2790static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002791#endif
2792
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002793/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002794 * Swap transform_out and out_ctr with the alternative ones
2795 */
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002796static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002797{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002799 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002800#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2801 int ret;
2802#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002803
2804 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002807 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002808 }
2809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002811
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002812 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002813 tmp_transform = ssl->transform_out;
2814 ssl->transform_out = ssl->handshake->alt_transform_out;
2815 ssl->handshake->alt_transform_out = tmp_transform;
2816
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002817 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002818 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2819 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2820 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002821
2822 /* Adjust to the newly activated transform */
2823 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002825 {
2826 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2827 ssl->transform_out->fixed_ivlen;
2828 }
2829 else
2830 ssl->out_msg = ssl->out_iv;
2831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2833 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2838 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002839 }
2840 }
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002841#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
2842
2843 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002844}
2845
2846/*
2847 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002848 *
2849 * Need to remember the current message in case flush_output returns
2850 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002851 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002854{
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002855 int ret;
2856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002862
2863 ssl->handshake->cur_msg = ssl->handshake->flight;
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002864 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
2865 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002868 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002869
2870 while( ssl->handshake->cur_msg != NULL )
2871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872 mbedtls_ssl_flight_item *cur = ssl->handshake->cur_msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002873
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002874 /* Swap epochs before sending Finished: we can't do it after
2875 * sending ChangeCipherSpec, in case write returns WANT_READ.
2876 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2878 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002879 {
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002880 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
2881 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002882 }
2883
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002884 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002885 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002886 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002887
2888 ssl->handshake->cur_msg = cur->next;
2889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 MBEDTLS_SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002895 return( ret );
2896 }
2897 }
2898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2900 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002901 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002904 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2905 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002908
2909 return( 0 );
2910}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002911
2912/*
2913 * To be called when the last message of an incoming flight is received.
2914 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002916{
2917 /* We won't need to resend that one any more */
2918 ssl_flight_free( ssl->handshake->flight );
2919 ssl->handshake->flight = NULL;
2920 ssl->handshake->cur_msg = NULL;
2921
2922 /* The next incoming flight will start with this msg_seq */
2923 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2924
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002925 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002926 ssl_set_timer( ssl, 0 );
2927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2929 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002932 }
2933 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002935}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002936
2937/*
2938 * To be called when the last message of an outgoing flight is send.
2939 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002941{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002942 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002943 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2946 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002949 }
2950 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002952}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002954
Paul Bakker5121ce52009-01-03 21:22:43 +00002955/*
2956 * Record layer functions
2957 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002958
2959/*
2960 * Write current record.
2961 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002964{
Nicholas Wilsonf0021642016-04-13 11:51:05 +01002965 int ret, done = 0, out_msg_type;
Paul Bakker23986e52011-04-24 08:57:21 +00002966 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002971 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002972 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002974 {
2975 ; /* Skip special handshake treatment when resending */
2976 }
2977 else
2978#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002980 {
Nicholas Wilsonf0021642016-04-13 11:51:05 +01002981 out_msg_type = ssl->out_msg[0];
2982
2983 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002984 ssl->handshake == NULL )
2985 {
2986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2987 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2988 }
2989
Paul Bakker5121ce52009-01-03 21:22:43 +00002990 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2991 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2992 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2993
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002994 /*
2995 * DTLS has additional fields in the Handshake layer,
2996 * between the length field and the actual payload:
2997 * uint16 message_seq;
2998 * uint24 fragment_offset;
2999 * uint24 fragment_length;
3000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003002 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003003 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003004 /* Make room for the additional DTLS fields */
Hanno Becker9648f8b2017-09-18 10:55:54 +01003005 if( MBEDTLS_SSL_MAX_CONTENT_LEN - ssl->out_msglen < 8 )
3006 {
3007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3008 "size %u, maximum %u",
3009 (unsigned) ( ssl->in_hslen - 4 ),
3010 (unsigned) ( MBEDTLS_SSL_MAX_CONTENT_LEN - 12 ) ) );
3011 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3012 }
3013
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003014 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003015 ssl->out_msglen += 8;
3016 len += 8;
3017
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003018 /* Write message_seq and update it, except for HelloRequest */
Nicholas Wilsonf0021642016-04-13 11:51:05 +01003019 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003020 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003021 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3022 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3023 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003024 }
3025 else
3026 {
3027 ssl->out_msg[4] = 0;
3028 ssl->out_msg[5] = 0;
3029 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003030
3031 /* We don't fragment, so frag_offset = 0 and frag_len = len */
3032 memset( ssl->out_msg + 6, 0x00, 3 );
3033 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003034 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003036
Nicholas Wilsonf0021642016-04-13 11:51:05 +01003037 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003038 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003039 }
3040
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003041 /* Save handshake and CCS messages for resending */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003043 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003044 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045 ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING &&
3046 ( ssl->out_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ||
3047 ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003048 {
3049 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003052 return( ret );
3053 }
3054 }
3055#endif
3056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003058 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003060 {
3061 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003064 return( ret );
3065 }
3066
3067 len = ssl->out_msglen;
3068 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3072 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 ret = mbedtls_ssl_hw_record_write( ssl );
3077 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3080 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003081 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003082
3083 if( ret == 0 )
3084 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003085 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003087 if( !done )
3088 {
3089 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003091 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003092
3093 ssl->out_len[0] = (unsigned char)( len >> 8 );
3094 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003095
Paul Bakker48916f92012-09-16 19:57:18 +00003096 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003097 {
3098 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003101 return( ret );
3102 }
3103
3104 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003105 ssl->out_len[0] = (unsigned char)( len >> 8 );
3106 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003107 }
3108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00003110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00003112 "version = [%d:%d], msglen = %d",
3113 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003114 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3117 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003118 }
3119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003123 return( ret );
3124 }
3125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003127
3128 return( 0 );
3129}
3130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003132/*
3133 * Mark bits in bitmask (used for DTLS HS reassembly)
3134 */
3135static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3136{
3137 unsigned int start_bits, end_bits;
3138
3139 start_bits = 8 - ( offset % 8 );
3140 if( start_bits != 8 )
3141 {
3142 size_t first_byte_idx = offset / 8;
3143
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003144 /* Special case */
3145 if( len <= start_bits )
3146 {
3147 for( ; len != 0; len-- )
3148 mask[first_byte_idx] |= 1 << ( start_bits - len );
3149
3150 /* Avoid potential issues with offset or len becoming invalid */
3151 return;
3152 }
3153
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003154 offset += start_bits; /* Now offset % 8 == 0 */
3155 len -= start_bits;
3156
3157 for( ; start_bits != 0; start_bits-- )
3158 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3159 }
3160
3161 end_bits = len % 8;
3162 if( end_bits != 0 )
3163 {
3164 size_t last_byte_idx = ( offset + len ) / 8;
3165
3166 len -= end_bits; /* Now len % 8 == 0 */
3167
3168 for( ; end_bits != 0; end_bits-- )
3169 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3170 }
3171
3172 memset( mask + offset / 8, 0xFF, len / 8 );
3173}
3174
3175/*
3176 * Check that bitmask is full
3177 */
3178static int ssl_bitmask_check( unsigned char *mask, size_t len )
3179{
3180 size_t i;
3181
3182 for( i = 0; i < len / 8; i++ )
3183 if( mask[i] != 0xFF )
3184 return( -1 );
3185
3186 for( i = 0; i < len % 8; i++ )
3187 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3188 return( -1 );
3189
3190 return( 0 );
3191}
3192
3193/*
3194 * Reassemble fragmented DTLS handshake messages.
3195 *
3196 * Use a temporary buffer for reassembly, divided in two parts:
3197 * - the first holds the reassembled message (including handshake header),
3198 * - the second holds a bitmask indicating which parts of the message
3199 * (excluding headers) have been received so far.
3200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003202{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003203 unsigned char *msg, *bitmask;
3204 size_t frag_len, frag_off;
3205 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3206
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003207 if( ssl->handshake == NULL )
3208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3210 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003211 }
3212
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003213 /*
3214 * For first fragment, check size and allocate buffer
3215 */
3216 if( ssl->handshake->hs_msg == NULL )
3217 {
3218 size_t alloc_len;
3219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003221 msg_len ) );
3222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 if( ssl->in_hslen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3226 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003227 }
3228
3229 /* The bitmask needs one bit per byte of message excluding header */
3230 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3231
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003232 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003233 if( ssl->handshake->hs_msg == NULL )
3234 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003236 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003237 }
3238
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003239 /* Prepare final header: copy msg_type, length and message_seq,
3240 * then add standardised fragment_offset and fragment_length */
3241 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3242 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3243 memcpy( ssl->handshake->hs_msg + 9,
3244 ssl->handshake->hs_msg + 1, 3 );
3245 }
3246 else
3247 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003248 /* Make sure msg_type and length are consistent */
3249 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3252 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003253 }
3254 }
3255
3256 msg = ssl->handshake->hs_msg + 12;
3257 bitmask = msg + msg_len;
3258
3259 /*
3260 * Check and copy current fragment
3261 */
3262 frag_off = ( ssl->in_msg[6] << 16 ) |
3263 ( ssl->in_msg[7] << 8 ) |
3264 ssl->in_msg[8];
3265 frag_len = ( ssl->in_msg[9] << 16 ) |
3266 ( ssl->in_msg[10] << 8 ) |
3267 ssl->in_msg[11];
3268
3269 if( frag_off + frag_len > msg_len )
3270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003272 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003274 }
3275
3276 if( frag_len + 12 > ssl->in_msglen )
3277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003279 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003281 }
3282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003284 frag_off, frag_len ) );
3285
3286 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3287 ssl_bitmask_set( bitmask, frag_off, frag_len );
3288
3289 /*
3290 * Do we have the complete message by now?
3291 * If yes, finalize it, else ask to read the next record.
3292 */
3293 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003296 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003297 }
3298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003300
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003301 if( frag_len + 12 < ssl->in_msglen )
3302 {
3303 /*
3304 * We'got more handshake messages in the same record.
3305 * This case is not handled now because no know implementation does
3306 * that and it's hard to test, so we prefer to fail cleanly for now.
3307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3309 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003310 }
3311
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003312 if( ssl->in_left > ssl->next_record_offset )
3313 {
3314 /*
3315 * We've got more data in the buffer after the current record,
3316 * that we don't want to overwrite. Move it before writing the
3317 * reassembled message, and adjust in_left and next_record_offset.
3318 */
3319 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3320 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3321 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3322
3323 /* First compute and check new lengths */
3324 ssl->next_record_offset = new_remain - ssl->in_hdr;
3325 ssl->in_left = ssl->next_record_offset + remain_len;
3326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327 if( ssl->in_left > MBEDTLS_SSL_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003328 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3331 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003332 }
3333
3334 memmove( new_remain, cur_remain, remain_len );
3335 }
3336
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003337 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3338
Hanno Beckerd82e0c02018-10-15 13:22:22 +01003339 mbedtls_zeroize( ssl->handshake->hs_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003341 ssl->handshake->hs_msg = NULL;
3342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003344 ssl->in_msg, ssl->in_hslen );
3345
3346 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003347}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003349
Simon Butcher99000142016-10-13 17:21:01 +01003350int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003351{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003355 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003357 }
3358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003360 ( ssl->in_msg[1] << 16 ) |
3361 ( ssl->in_msg[2] << 8 ) |
3362 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003365 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003366 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003369 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003370 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003371 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003372 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003373
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003374 /* ssl->handshake is NULL when receiving ClientHello for renego */
3375 if( ssl->handshake != NULL &&
3376 recv_msg_seq != ssl->handshake->in_msg_seq )
3377 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003378 /* Retransmit only on last message from previous flight, to avoid
3379 * too many retransmissions.
3380 * Besides, No sane server ever retransmits HelloVerifyRequest */
3381 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003385 "message_seq = %d, start_of_flight = %d",
3386 recv_msg_seq,
3387 ssl->handshake->in_flight_start_seq ) );
3388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003392 return( ret );
3393 }
3394 }
3395 else
3396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003398 "message_seq = %d, expected = %d",
3399 recv_msg_seq,
3400 ssl->handshake->in_msg_seq ) );
3401 }
3402
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003403 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003404 }
3405 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003406
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003407 /* Reassemble if current message is fragmented or reassembly is
3408 * already in progress */
3409 if( ssl->in_msglen < ssl->in_hslen ||
3410 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3411 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3412 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003415
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003416 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003419 return( ret );
3420 }
3421 }
3422 }
3423 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003425 /* With TLS we don't handle fragmentation (for now) */
3426 if( ssl->in_msglen < ssl->in_hslen )
3427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3429 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003430 }
3431
Simon Butcher99000142016-10-13 17:21:01 +01003432 return( 0 );
3433}
3434
3435void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3436{
3437
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003438 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3439 ssl->handshake != NULL )
3440 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003441 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003442 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003443
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003444 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003446 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003447 ssl->handshake != NULL )
3448 {
3449 ssl->handshake->in_msg_seq++;
3450 }
3451#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003452}
3453
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003454/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003455 * DTLS anti-replay: RFC 6347 4.1.2.6
3456 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003457 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3458 * Bit n is set iff record number in_window_top - n has been seen.
3459 *
3460 * Usually, in_window_top is the last record number seen and the lsb of
3461 * in_window is set. The only exception is the initial state (record number 0
3462 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3465static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003466{
3467 ssl->in_window_top = 0;
3468 ssl->in_window = 0;
3469}
3470
3471static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3472{
3473 return( ( (uint64_t) buf[0] << 40 ) |
3474 ( (uint64_t) buf[1] << 32 ) |
3475 ( (uint64_t) buf[2] << 24 ) |
3476 ( (uint64_t) buf[3] << 16 ) |
3477 ( (uint64_t) buf[4] << 8 ) |
3478 ( (uint64_t) buf[5] ) );
3479}
3480
3481/*
3482 * Return 0 if sequence number is acceptable, -1 otherwise
3483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003485{
3486 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3487 uint64_t bit;
3488
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003489 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003490 return( 0 );
3491
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003492 if( rec_seqnum > ssl->in_window_top )
3493 return( 0 );
3494
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003495 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003496
3497 if( bit >= 64 )
3498 return( -1 );
3499
3500 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3501 return( -1 );
3502
3503 return( 0 );
3504}
3505
3506/*
3507 * Update replay window on new validated record
3508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003510{
3511 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3512
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003513 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003514 return;
3515
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003516 if( rec_seqnum > ssl->in_window_top )
3517 {
3518 /* Update window_top and the contents of the window */
3519 uint64_t shift = rec_seqnum - ssl->in_window_top;
3520
3521 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003522 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003523 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003524 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003525 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003526 ssl->in_window |= 1;
3527 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003528
3529 ssl->in_window_top = rec_seqnum;
3530 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003531 else
3532 {
3533 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003534 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003535
3536 if( bit < 64 ) /* Always true, but be extra sure */
3537 ssl->in_window |= (uint64_t) 1 << bit;
3538 }
3539}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003541
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003542#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003543/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003544static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3545
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003546/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003547 * Without any SSL context, check if a datagram looks like a ClientHello with
3548 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003549 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003550 *
3551 * - if cookie is valid, return 0
3552 * - if ClientHello looks superficially valid but cookie is not,
3553 * fill obuf and set olen, then
3554 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3555 * - otherwise return a specific error code
3556 */
3557static int ssl_check_dtls_clihlo_cookie(
3558 mbedtls_ssl_cookie_write_t *f_cookie_write,
3559 mbedtls_ssl_cookie_check_t *f_cookie_check,
3560 void *p_cookie,
3561 const unsigned char *cli_id, size_t cli_id_len,
3562 const unsigned char *in, size_t in_len,
3563 unsigned char *obuf, size_t buf_len, size_t *olen )
3564{
3565 size_t sid_len, cookie_len;
3566 unsigned char *p;
3567
3568 if( f_cookie_write == NULL || f_cookie_check == NULL )
3569 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3570
3571 /*
3572 * Structure of ClientHello with record and handshake headers,
3573 * and expected values. We don't need to check a lot, more checks will be
3574 * done when actually parsing the ClientHello - skipping those checks
3575 * avoids code duplication and does not make cookie forging any easier.
3576 *
3577 * 0-0 ContentType type; copied, must be handshake
3578 * 1-2 ProtocolVersion version; copied
3579 * 3-4 uint16 epoch; copied, must be 0
3580 * 5-10 uint48 sequence_number; copied
3581 * 11-12 uint16 length; (ignored)
3582 *
3583 * 13-13 HandshakeType msg_type; (ignored)
3584 * 14-16 uint24 length; (ignored)
3585 * 17-18 uint16 message_seq; copied
3586 * 19-21 uint24 fragment_offset; copied, must be 0
3587 * 22-24 uint24 fragment_length; (ignored)
3588 *
3589 * 25-26 ProtocolVersion client_version; (ignored)
3590 * 27-58 Random random; (ignored)
3591 * 59-xx SessionID session_id; 1 byte len + sid_len content
3592 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3593 * ...
3594 *
3595 * Minimum length is 61 bytes.
3596 */
3597 if( in_len < 61 ||
3598 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3599 in[3] != 0 || in[4] != 0 ||
3600 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3601 {
3602 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3603 }
3604
3605 sid_len = in[59];
3606 if( sid_len > in_len - 61 )
3607 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3608
3609 cookie_len = in[60 + sid_len];
3610 if( cookie_len > in_len - 60 )
3611 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3612
3613 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3614 cli_id, cli_id_len ) == 0 )
3615 {
3616 /* Valid cookie */
3617 return( 0 );
3618 }
3619
3620 /*
3621 * If we get here, we've got an invalid cookie, let's prepare HVR.
3622 *
3623 * 0-0 ContentType type; copied
3624 * 1-2 ProtocolVersion version; copied
3625 * 3-4 uint16 epoch; copied
3626 * 5-10 uint48 sequence_number; copied
3627 * 11-12 uint16 length; olen - 13
3628 *
3629 * 13-13 HandshakeType msg_type; hello_verify_request
3630 * 14-16 uint24 length; olen - 25
3631 * 17-18 uint16 message_seq; copied
3632 * 19-21 uint24 fragment_offset; copied
3633 * 22-24 uint24 fragment_length; olen - 25
3634 *
3635 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3636 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3637 *
3638 * Minimum length is 28.
3639 */
3640 if( buf_len < 28 )
3641 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3642
3643 /* Copy most fields and adapt others */
3644 memcpy( obuf, in, 25 );
3645 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3646 obuf[25] = 0xfe;
3647 obuf[26] = 0xff;
3648
3649 /* Generate and write actual cookie */
3650 p = obuf + 28;
3651 if( f_cookie_write( p_cookie,
3652 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3653 {
3654 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3655 }
3656
3657 *olen = p - obuf;
3658
3659 /* Go back and fill length fields */
3660 obuf[27] = (unsigned char)( *olen - 28 );
3661
3662 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3663 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3664 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3665
3666 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3667 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3668
3669 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3670}
3671
3672/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003673 * Handle possible client reconnect with the same UDP quadruplet
3674 * (RFC 6347 Section 4.2.8).
3675 *
3676 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3677 * that looks like a ClientHello.
3678 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003679 * - if the input looks like a ClientHello without cookies,
3680 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003681 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003682 * - if the input looks like a ClientHello with a valid cookie,
3683 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003684 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003685 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003686 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003687 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003688 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3689 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003690 */
3691static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3692{
3693 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003694 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003695
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003696 /* Use out_msg as temporary buffer for writing out HelloVerifyRequest,
3697 * because the output buffer's already around. Don't use out_buf though,
3698 * as we don't want to overwrite out_ctr. */
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003699 ret = ssl_check_dtls_clihlo_cookie(
3700 ssl->conf->f_cookie_write,
3701 ssl->conf->f_cookie_check,
3702 ssl->conf->p_cookie,
3703 ssl->cli_id, ssl->cli_id_len,
3704 ssl->in_buf, ssl->in_left,
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003705 ssl->out_msg, MBEDTLS_SSL_MAX_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003706
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003707 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3708
3709 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003710 {
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003711 int send_ret;
3712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3713 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003714 ssl->out_msg, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003715 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003716 * If the error is permanent we'll catch it later,
3717 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003718 send_ret = ssl->f_send( ssl->p_bio, ssl->out_msg, len );
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003719 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3720 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003721
3722 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003723 }
3724
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003725 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003726 {
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003728 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3729 {
3730 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3731 return( ret );
3732 }
3733
3734 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003735 }
3736
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003737 return( ret );
3738}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003739#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003740
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003741/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003742 * ContentType type;
3743 * ProtocolVersion version;
3744 * uint16 epoch; // DTLS only
3745 * uint48 sequence_number; // DTLS only
3746 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003747 *
3748 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003749 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003750 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3751 *
3752 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003753 * 1. proceed with the record if this function returns 0
3754 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3755 * 3. return CLIENT_RECONNECT if this function return that value
3756 * 4. drop the whole datagram if this function returns anything else.
3757 * Point 2 is needed when the peer is resending, and we have already received
3758 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003761{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003762 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 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 +02003765
Paul Bakker5121ce52009-01-03 21:22:43 +00003766 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003767 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003768 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003771 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003772 ssl->in_msgtype,
3773 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003774
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003775 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3777 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3778 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3779 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003782
3783#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01003784 /* Silently ignore invalid DTLS records as recommended by RFC 6347
3785 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003786 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3787#endif /* MBEDTLS_SSL_PROTO_DTLS */
3788 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3789 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
3790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003792 }
3793
3794 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003795 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3798 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003799 }
3800
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003801 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3804 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003805 }
3806
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003807 /* Check length against the size of our buffer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003808 if( ssl->in_msglen > MBEDTLS_SSL_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003809 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3812 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003813 }
3814
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003815 /*
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003816 * DTLS-related tests.
3817 * Check epoch before checking length constraint because
3818 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3819 * message gets duplicated before the corresponding Finished message,
3820 * the second ChangeCipherSpec should be discarded because it belongs
3821 * to an old epoch, but not because its length is shorter than
3822 * the minimum record length for packets using the new record transform.
3823 * Note that these two kinds of failures are handled differently,
3824 * as an unexpected record is silently skipped but an invalid
3825 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003826 */
3827#if defined(MBEDTLS_SSL_PROTO_DTLS)
3828 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3829 {
3830 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3831
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003832 /* Check epoch (and sequence number) with DTLS */
3833 if( rec_epoch != ssl->in_epoch )
3834 {
3835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3836 "expected %d, received %d",
3837 ssl->in_epoch, rec_epoch ) );
3838
3839#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3840 /*
3841 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3842 * access the first byte of record content (handshake type), as we
3843 * have an active transform (possibly iv_len != 0), so use the
3844 * fact that the record header len is 13 instead.
3845 */
3846 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3847 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3848 rec_epoch == 0 &&
3849 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3850 ssl->in_left > 13 &&
3851 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3852 {
3853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3854 "from the same port" ) );
3855 return( ssl_handle_possible_reconnect( ssl ) );
3856 }
3857 else
3858#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3859 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3860 }
3861
3862#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3863 /* Replay detection only works for the current epoch */
3864 if( rec_epoch == ssl->in_epoch &&
3865 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3866 {
3867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3868 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3869 }
3870#endif
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003871
3872 /* Drop unexpected ChangeCipherSpec messages */
3873 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3874 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3875 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3876 {
3877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3878 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3879 }
3880
3881 /* Drop unexpected ApplicationData records,
3882 * except at the beginning of renegotiations */
3883 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
3884 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
3885#if defined(MBEDTLS_SSL_RENEGOTIATION)
3886 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
3887 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
3888#endif
3889 )
3890 {
3891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3892 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3893 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003894 }
3895#endif /* MBEDTLS_SSL_PROTO_DTLS */
3896
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003897
3898 /* Check length against bounds of the current transform and version */
3899 if( ssl->transform_in == NULL )
3900 {
3901 if( ssl->in_msglen < 1 ||
3902 ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
3903 {
3904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3905 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3906 }
3907 }
3908 else
3909 {
3910 if( ssl->in_msglen < ssl->transform_in->minlen )
3911 {
3912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3913 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3914 }
3915
3916#if defined(MBEDTLS_SSL_PROTO_SSL3)
3917 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3918 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_MAX_CONTENT_LEN )
3919 {
3920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3921 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3922 }
3923#endif
3924#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3925 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3926 /*
3927 * TLS encrypted messages can have up to 256 bytes of padding
3928 */
3929 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
3930 ssl->in_msglen > ssl->transform_in->minlen +
3931 MBEDTLS_SSL_MAX_CONTENT_LEN + 256 )
3932 {
3933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3934 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3935 }
3936#endif
3937 }
3938
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003939 return( 0 );
3940}
Paul Bakker5121ce52009-01-03 21:22:43 +00003941
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003942/*
3943 * If applicable, decrypt (and decompress) record content
3944 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003945static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003946{
3947 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
3950 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003952#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3953 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003957 ret = mbedtls_ssl_hw_record_read( ssl );
3958 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003960 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3961 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003962 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003963
3964 if( ret == 0 )
3965 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003966 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003968 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003969 {
3970 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003973 return( ret );
3974 }
3975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00003977 ssl->in_msg, ssl->in_msglen );
3978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003979 if( ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3982 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003983 }
3984 }
3985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003987 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003989 {
3990 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003993 return( ret );
3994 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00003995 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003999 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004002 }
4003#endif
4004
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004005 return( 0 );
4006}
4007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004008static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004009
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004010/*
4011 * Read a record.
4012 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004013 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4014 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4015 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004017int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004018{
4019 int ret;
4020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004022
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004023 if( ssl->keep_current_message == 0 )
4024 {
4025 do {
Simon Butcher99000142016-10-13 17:21:01 +01004026
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004027 if( ( ret = mbedtls_ssl_read_record_layer( ssl ) ) != 0 )
4028 {
4029 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4030 return( ret );
4031 }
4032
4033 ret = mbedtls_ssl_handle_message_type( ssl );
4034
4035 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret );
4036
4037 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004038 {
4039 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4040 return( ret );
4041 }
4042
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004043 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4044 {
4045 mbedtls_ssl_update_handshake_status( ssl );
4046 }
Simon Butcher99000142016-10-13 17:21:01 +01004047 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004048 else
Simon Butcher99000142016-10-13 17:21:01 +01004049 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4051 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004052 }
4053
4054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4055
4056 return( 0 );
4057}
4058
4059int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4060{
4061 int ret;
4062
Hanno Becker4a810fb2017-05-24 16:27:30 +01004063 /*
4064 * Step A
4065 *
4066 * Consume last content-layer message and potentially
4067 * update in_msglen which keeps track of the contents'
4068 * consumption state.
4069 *
4070 * (1) Handshake messages:
4071 * Remove last handshake message, move content
4072 * and adapt in_msglen.
4073 *
4074 * (2) Alert messages:
4075 * Consume whole record content, in_msglen = 0.
4076 *
4077 * NOTE: This needs to be fixed, since like for
4078 * handshake messages it is allowed to have
4079 * multiple alerts witin a single record.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004080 * Internal reference IOTSSL-1321.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004081 *
4082 * (3) Change cipher spec:
4083 * Consume whole record content, in_msglen = 0.
4084 *
4085 * (4) Application data:
4086 * Don't do anything - the record layer provides
4087 * the application data as a stream transport
4088 * and consumes through mbedtls_ssl_read only.
4089 *
4090 */
4091
4092 /* Case (1): Handshake messages */
4093 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004094 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004095 /* Hard assertion to be sure that no application data
4096 * is in flight, as corrupting ssl->in_msglen during
4097 * ssl->in_offt != NULL is fatal. */
4098 if( ssl->in_offt != NULL )
4099 {
4100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4101 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4102 }
4103
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004104 /*
4105 * Get next Handshake message in the current record
4106 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004107
Hanno Becker4a810fb2017-05-24 16:27:30 +01004108 /* Notes:
4109 * (1) in_hslen is *NOT* necessarily the size of the
4110 * current handshake content: If DTLS handshake
4111 * fragmentation is used, that's the fragment
4112 * size instead. Using the total handshake message
4113 * size here is FAULTY and should be changed at
4114 * some point. Internal reference IOTSSL-1414.
4115 * (2) While it doesn't seem to cause problems, one
4116 * has to be very careful not to assume that in_hslen
4117 * is always <= in_msglen in a sensible communication.
4118 * Again, it's wrong for DTLS handshake fragmentation.
4119 * The following check is therefore mandatory, and
4120 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004121 * Additionally, ssl->in_hslen might be arbitrarily out of
4122 * bounds after handling a DTLS message with an unexpected
4123 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004124 */
4125 if( ssl->in_hslen < ssl->in_msglen )
4126 {
4127 ssl->in_msglen -= ssl->in_hslen;
4128 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4129 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004130
Hanno Becker4a810fb2017-05-24 16:27:30 +01004131 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4132 ssl->in_msg, ssl->in_msglen );
4133 }
4134 else
4135 {
4136 ssl->in_msglen = 0;
4137 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004138
Hanno Becker4a810fb2017-05-24 16:27:30 +01004139 ssl->in_hslen = 0;
4140 }
4141 /* Case (4): Application data */
4142 else if( ssl->in_offt != NULL )
4143 {
4144 return( 0 );
4145 }
4146 /* Everything else (CCS & Alerts) */
4147 else
4148 {
4149 ssl->in_msglen = 0;
4150 }
4151
4152 /*
4153 * Step B
4154 *
4155 * Fetch and decode new record if current one is fully consumed.
4156 *
4157 */
4158
4159 if( ssl->in_msglen > 0 )
4160 {
4161 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004162 return( 0 );
4163 }
4164
Hanno Becker4a810fb2017-05-24 16:27:30 +01004165 /* Need to fetch a new record */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004166
Simon Butcher99000142016-10-13 17:21:01 +01004167#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004168read_record_header:
Simon Butcher99000142016-10-13 17:21:01 +01004169#endif
4170
Hanno Becker4a810fb2017-05-24 16:27:30 +01004171 /* Current record either fully processed or to be discarded. */
4172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004175 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004176 return( ret );
4177 }
4178
4179 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004181#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004182 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4183 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004184 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004185 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4186 {
4187 /* Skip unexpected record (but not whole datagram) */
4188 ssl->next_record_offset = ssl->in_msglen
4189 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004190
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4192 "(header)" ) );
4193 }
4194 else
4195 {
4196 /* Skip invalid record and the rest of the datagram */
4197 ssl->next_record_offset = 0;
4198 ssl->in_left = 0;
4199
4200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4201 "(header)" ) );
4202 }
4203
4204 /* Get next record */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004205 goto read_record_header;
4206 }
4207#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004208 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004209 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004210
4211 /*
4212 * Read and optionally decrypt the message contents
4213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4215 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004218 return( ret );
4219 }
4220
4221 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004223 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004225 else
4226#endif
4227 ssl->in_left = 0;
4228
4229 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004231#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004232 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004233 {
4234 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004235 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4236 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004237 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004238 /* Except when waiting for Finished as a bad mac here
4239 * probably means something went wrong in the handshake
4240 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4241 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4242 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4243 {
4244#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4245 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4246 {
4247 mbedtls_ssl_send_alert_message( ssl,
4248 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4249 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4250 }
4251#endif
4252 return( ret );
4253 }
4254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004255#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004256 if( ssl->conf->badmac_limit != 0 &&
4257 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4260 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004261 }
4262#endif
4263
Hanno Becker4a810fb2017-05-24 16:27:30 +01004264 /* As above, invalid records cause
4265 * dismissal of the whole datagram. */
4266
4267 ssl->next_record_offset = 0;
4268 ssl->in_left = 0;
4269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004271 goto read_record_header;
4272 }
4273
4274 return( ret );
4275 }
4276 else
4277#endif
4278 {
4279 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4281 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004283 mbedtls_ssl_send_alert_message( ssl,
4284 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4285 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004286 }
4287#endif
4288 return( ret );
4289 }
4290 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004291
4292 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004293 * When we sent the last flight of the handshake, we MUST respond to a
4294 * retransmit of the peer's previous flight with a retransmit. (In
4295 * practice, only the Finished message will make it, other messages
4296 * including CCS use the old transform so they're dropped as invalid.)
4297 *
4298 * If the record we received is not a handshake message, however, it
4299 * means the peer received our last flight so we can clean up
4300 * handshake info.
4301 *
4302 * This check needs to be done before prepare_handshake() due to an edge
4303 * case: if the client immediately requests renegotiation, this
4304 * finishes the current handshake first, avoiding the new ClientHello
4305 * being mistaken for an ancient message in the current handshake.
4306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004307#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004308 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004309 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004310 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4313 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004317 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004320 return( ret );
4321 }
4322
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01004323 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004324 }
4325 else
4326 {
4327 ssl_handshake_wrapup_free_hs_transform( ssl );
4328 }
4329 }
4330#endif
4331
Simon Butcher99000142016-10-13 17:21:01 +01004332 return( 0 );
4333}
4334
4335int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4336{
4337 int ret;
4338
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004339 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004340 * Handle particular types of records
4341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004343 {
Simon Butcher99000142016-10-13 17:21:01 +01004344 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4345 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004346 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004347 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004348 }
4349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004350 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004351 {
Angus Gratton8946b0d2018-06-20 15:43:50 +10004352 if( ssl->in_msglen != 2 )
4353 {
4354 /* Note: Standard allows for more than one 2 byte alert
4355 to be packed in a single message, but Mbed TLS doesn't
4356 currently support this. */
4357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4358 ssl->in_msglen ) );
4359 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4360 }
4361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004363 ssl->in_msg[0], ssl->in_msg[1] ) );
4364
4365 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004366 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004368 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004371 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004372 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004373 }
4374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004375 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4376 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4379 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004380 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004381
4382#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4383 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4384 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4385 {
4386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4387 /* Will be handled when trying to parse ServerHello */
4388 return( 0 );
4389 }
4390#endif
4391
4392#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4393 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4394 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4395 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4396 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4397 {
4398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4399 /* Will be handled in mbedtls_ssl_parse_certificate() */
4400 return( 0 );
4401 }
4402#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4403
4404 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004405 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004406 }
4407
Paul Bakker5121ce52009-01-03 21:22:43 +00004408 return( 0 );
4409}
4410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004412{
4413 int ret;
4414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004415 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4416 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4417 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004418 {
4419 return( ret );
4420 }
4421
4422 return( 0 );
4423}
4424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004425int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004426 unsigned char level,
4427 unsigned char message )
4428{
4429 int ret;
4430
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004431 if( ssl == NULL || ssl->conf == NULL )
4432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004437 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004438 ssl->out_msglen = 2;
4439 ssl->out_msg[0] = level;
4440 ssl->out_msg[1] = message;
4441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004445 return( ret );
4446 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004448
4449 return( 0 );
4450}
4451
Paul Bakker5121ce52009-01-03 21:22:43 +00004452/*
4453 * Handshake functions
4454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4456 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4457 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4458 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4459 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4460 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4461 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004462/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004463int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004464{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004465 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4470 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004471 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4472 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004475 ssl->state++;
4476 return( 0 );
4477 }
4478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4480 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004481}
4482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004483int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004484{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004485 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004489 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4490 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004491 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4492 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004495 ssl->state++;
4496 return( 0 );
4497 }
4498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4500 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004501}
Gilles Peskinef9828522017-05-03 12:28:43 +02004502
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004503#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004504/* Some certificate support -> implement write and parse */
4505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004507{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004509 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510 const mbedtls_x509_crt *crt;
4511 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004515 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4516 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004517 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4518 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004521 ssl->state++;
4522 return( 0 );
4523 }
4524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004525#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004526 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004527 {
4528 if( ssl->client_auth == 0 )
4529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004531 ssl->state++;
4532 return( 0 );
4533 }
4534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004536 /*
4537 * If using SSLv3 and got no cert, send an Alert message
4538 * (otherwise an empty Certificate message will be sent).
4539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004540 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4541 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004542 {
4543 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4545 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4546 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004549 goto write_msg;
4550 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004552 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553#endif /* MBEDTLS_SSL_CLI_C */
4554#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004555 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004557 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4560 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004561 }
4562 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004563#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004566
4567 /*
4568 * 0 . 0 handshake type
4569 * 1 . 3 handshake length
4570 * 4 . 6 length of all certs
4571 * 7 . 9 length of cert. 1
4572 * 10 . n-1 peer certificate
4573 * n . n+2 length of cert. 2
4574 * n+3 . ... upper level cert, etc.
4575 */
4576 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004578
Paul Bakker29087132010-03-21 21:03:34 +00004579 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004580 {
4581 n = crt->raw.len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004582 if( n > MBEDTLS_SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
4585 i + 3 + n, MBEDTLS_SSL_MAX_CONTENT_LEN ) );
4586 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004587 }
4588
4589 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4590 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4591 ssl->out_msg[i + 2] = (unsigned char)( n );
4592
4593 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4594 i += n; crt = crt->next;
4595 }
4596
4597 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4598 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4599 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4600
4601 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004602 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4603 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004604
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004605#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004606write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004607#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004608
4609 ssl->state++;
4610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004611 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004613 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004614 return( ret );
4615 }
4616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004618
Paul Bakkered27a042013-04-18 22:46:23 +02004619 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004620}
4621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004622int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004623{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004624 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004625 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004626 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004627 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004628 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004632 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4633 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004634 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4635 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004638 ssl->state++;
4639 return( 0 );
4640 }
4641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004642#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004643 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004644 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4645 {
4646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4647 ssl->state++;
4648 return( 0 );
4649 }
4650
4651#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4652 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4653 authmode = ssl->handshake->sni_authmode;
4654#endif
4655
4656 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4657 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004658 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004659 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004661 ssl->state++;
4662 return( 0 );
4663 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004664#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004666 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004667 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004668 /* mbedtls_ssl_read_record may have sent an alert already. We
4669 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004671 return( ret );
4672 }
4673
4674 ssl->state++;
4675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004676#if defined(MBEDTLS_SSL_SRV_C)
4677#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004678 /*
4679 * Check if the client sent an empty certificate
4680 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004681 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004682 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004683 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004684 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004685 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4686 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4687 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004690
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004691 /* The client was asked for a certificate but didn't send
4692 one. The client should know what's going on, so we
4693 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004694 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004695 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004696 return( 0 );
4697 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004698 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004699 }
4700 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004701#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004703#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4704 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004705 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004706 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004708 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4709 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4710 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4711 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004714
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004715 /* The client was asked for a certificate but didn't send
4716 one. The client should know what's going on, so we
4717 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004718 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004719 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004720 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004721 else
4722 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004723 }
4724 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004725#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4726 MBEDTLS_SSL_PROTO_TLS1_2 */
4727#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004729 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004732 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4733 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004734 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004735 }
4736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004737 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4738 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004741 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4742 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004743 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004744 }
4745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004746 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004747
Paul Bakker5121ce52009-01-03 21:22:43 +00004748 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004750 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004751 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004752
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004753 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004754 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004757 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4758 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004760 }
4761
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004762 /* In case we tried to reuse a session but it failed */
4763 if( ssl->session_negotiate->peer_cert != NULL )
4764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4766 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004767 }
4768
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004769 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004770 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004771 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004774 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4775 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004776 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004777 }
4778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004779 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004780
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004781 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004782
4783 while( i < ssl->in_hslen )
4784 {
Philippe Antoine33e5c322018-07-09 10:39:02 +02004785 if ( i + 3 > ssl->in_hslen ) {
4786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4787 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4788 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4789 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
4790 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004791 if( ssl->in_msg[i] != 0 )
4792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004794 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4795 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004797 }
4798
4799 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4800 | (unsigned int) ssl->in_msg[i + 2];
4801 i += 3;
4802
4803 if( n < 128 || i + n > ssl->in_hslen )
4804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004806 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4807 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004809 }
4810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004812 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004813 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004814 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004815 case 0: /*ok*/
4816 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
4817 /* Ignore certificate with an unknown algorithm: maybe a
4818 prior certificate was already trusted. */
4819 break;
4820
4821 case MBEDTLS_ERR_X509_ALLOC_FAILED:
4822 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
4823 goto crt_parse_der_failed;
4824
4825 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
4826 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4827 goto crt_parse_der_failed;
4828
4829 default:
4830 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4831 crt_parse_der_failed:
4832 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004834 return( ret );
4835 }
4836
4837 i += n;
4838 }
4839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004840 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004841
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004842 /*
4843 * On client, make sure the server cert doesn't change during renego to
4844 * avoid "triple handshake" attack: https://secure-resumption.com/
4845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004846#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004847 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004848 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004849 {
4850 if( ssl->session->peer_cert == NULL )
4851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004853 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4854 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004855 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004856 }
4857
4858 if( ssl->session->peer_cert->raw.len !=
4859 ssl->session_negotiate->peer_cert->raw.len ||
4860 memcmp( ssl->session->peer_cert->raw.p,
4861 ssl->session_negotiate->peer_cert->raw.p,
4862 ssl->session->peer_cert->raw.len ) != 0 )
4863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004865 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4866 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004867 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004868 }
4869 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004870#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004871
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004872 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004873 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004874 mbedtls_x509_crt *ca_chain;
4875 mbedtls_x509_crl *ca_crl;
4876
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004877#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004878 if( ssl->handshake->sni_ca_chain != NULL )
4879 {
4880 ca_chain = ssl->handshake->sni_ca_chain;
4881 ca_crl = ssl->handshake->sni_ca_crl;
4882 }
4883 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004884#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004885 {
4886 ca_chain = ssl->conf->ca_chain;
4887 ca_crl = ssl->conf->ca_crl;
4888 }
4889
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004890 /*
4891 * Main check: verify certificate
4892 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004893 ret = mbedtls_x509_crt_verify_with_profile(
4894 ssl->session_negotiate->peer_cert,
4895 ca_chain, ca_crl,
4896 ssl->conf->cert_profile,
4897 ssl->hostname,
4898 &ssl->session_negotiate->verify_result,
4899 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004900
4901 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004903 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004904 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004905
4906 /*
4907 * Secondary checks: always done, but change 'ret' only if it was 0
4908 */
4909
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004910#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004913
4914 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004915 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004916 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004917 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004918 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
4919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004921 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004922 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004923 }
4924 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004925#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004927 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004928 ciphersuite_info,
4929 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004930 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004933 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004935 }
4936
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004937 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
4938 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
4939 * with details encoded in the verification flags. All other kinds
4940 * of error codes, including those from the user provided f_vrfy
4941 * functions, are treated as fatal and lead to a failure of
4942 * ssl_parse_certificate even if verification was optional. */
4943 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
4944 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
4945 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
4946 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004947 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004948 }
4949
4950 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
4951 {
4952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
4953 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
4954 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004955
4956 if( ret != 0 )
4957 {
4958 /* The certificate may have been rejected for several reasons.
4959 Pick one and send the corresponding alert. Which alert to send
4960 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004961 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
4962 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
4963 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
4964 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4965 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
4966 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4967 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
4968 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4969 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
4970 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4971 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
4972 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4973 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
4974 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4975 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
4976 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
4977 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
4978 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
4979 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
4980 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02004981 else
4982 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004983 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4984 alert );
4985 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004986
Hanno Beckere6706e62017-05-15 16:05:15 +01004987#if defined(MBEDTLS_DEBUG_C)
4988 if( ssl->session_negotiate->verify_result != 0 )
4989 {
4990 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
4991 ssl->session_negotiate->verify_result ) );
4992 }
4993 else
4994 {
4995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
4996 }
4997#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004998 }
4999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005001
5002 return( ret );
5003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005004#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5005 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5006 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5007 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5008 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5009 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5010 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005012int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005013{
5014 int ret;
5015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005018 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005019 ssl->out_msglen = 1;
5020 ssl->out_msg[0] = 1;
5021
Paul Bakker5121ce52009-01-03 21:22:43 +00005022 ssl->state++;
5023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005024 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005027 return( ret );
5028 }
5029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005031
5032 return( 0 );
5033}
5034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005035int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005036{
5037 int ret;
5038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005041 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005044 return( ret );
5045 }
5046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005047 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005050 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5051 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005052 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005053 }
5054
5055 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005058 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5059 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005061 }
5062
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005063 /*
5064 * Switch to our negotiated transform and session parameters for inbound
5065 * data.
5066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005068 ssl->transform_in = ssl->transform_negotiate;
5069 ssl->session_in = ssl->session_negotiate;
5070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005072 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005075 ssl_dtls_replay_reset( ssl );
5076#endif
5077
5078 /* Increment epoch */
5079 if( ++ssl->in_epoch == 0 )
5080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005082 /* This is highly unlikely to happen for legitimate reasons, so
5083 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005085 }
5086 }
5087 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005088#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005089 memset( ssl->in_ctr, 0, 8 );
5090
5091 /*
5092 * Set the in_msg pointer to the correct location based on IV length
5093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005094 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005095 {
5096 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
5097 ssl->transform_negotiate->fixed_ivlen;
5098 }
5099 else
5100 ssl->in_msg = ssl->in_iv;
5101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005102#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5103 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005105 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005108 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5109 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005110 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005111 }
5112 }
5113#endif
5114
Paul Bakker5121ce52009-01-03 21:22:43 +00005115 ssl->state++;
5116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005118
5119 return( 0 );
5120}
5121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005122void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5123 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005124{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005125 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005127#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5128 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5129 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005130 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005131 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005132#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005133#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5134#if defined(MBEDTLS_SHA512_C)
5135 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005136 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5137 else
5138#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005139#if defined(MBEDTLS_SHA256_C)
5140 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005141 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005142 else
5143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005144#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005147 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005148 }
Paul Bakker380da532012-04-18 16:10:25 +00005149}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005151void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005152{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005153#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5154 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005155 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5156 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005157#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005158#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5159#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005160 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005161#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005163 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005164#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005165#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005166}
5167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005168static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005169 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005170{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005171#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5172 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005173 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5174 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005176#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5177#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005178 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005179#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005180#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005181 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005182#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005183#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005184}
5185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005186#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5187 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5188static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005189 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005190{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005191 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5192 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005193}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005194#endif
Paul Bakker380da532012-04-18 16:10:25 +00005195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005196#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5197#if defined(MBEDTLS_SHA256_C)
5198static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005199 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005200{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005201 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005202}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005203#endif
Paul Bakker380da532012-04-18 16:10:25 +00005204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005205#if defined(MBEDTLS_SHA512_C)
5206static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005207 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005208{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005209 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005210}
Paul Bakker769075d2012-11-24 11:26:46 +01005211#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005214#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005215static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005216 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005217{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005218 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005219 mbedtls_md5_context md5;
5220 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005221
Paul Bakker5121ce52009-01-03 21:22:43 +00005222 unsigned char padbuf[48];
5223 unsigned char md5sum[16];
5224 unsigned char sha1sum[20];
5225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005226 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005227 if( !session )
5228 session = ssl->session;
5229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005231
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005232 mbedtls_md5_init( &md5 );
5233 mbedtls_sha1_init( &sha1 );
5234
5235 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5236 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005237
5238 /*
5239 * SSLv3:
5240 * hash =
5241 * MD5( master + pad2 +
5242 * MD5( handshake + sender + master + pad1 ) )
5243 * + SHA1( master + pad2 +
5244 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005245 */
5246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005247#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005248 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5249 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005250#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005252#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005253 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5254 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005255#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005257 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005258 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005259
Paul Bakker1ef83d62012-04-11 12:09:53 +00005260 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005261
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005262 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5263 mbedtls_md5_update_ret( &md5, session->master, 48 );
5264 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5265 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005266
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005267 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5268 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5269 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5270 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005271
Paul Bakker1ef83d62012-04-11 12:09:53 +00005272 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005273
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005274 mbedtls_md5_starts_ret( &md5 );
5275 mbedtls_md5_update_ret( &md5, session->master, 48 );
5276 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5277 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5278 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005279
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005280 mbedtls_sha1_starts_ret( &sha1 );
5281 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5282 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5283 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5284 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005286 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005287
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005288 mbedtls_md5_free( &md5 );
5289 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005291 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
5292 mbedtls_zeroize( md5sum, sizeof( md5sum ) );
5293 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005296}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005297#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005300static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005301 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005302{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005303 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005304 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005305 mbedtls_md5_context md5;
5306 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005307 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005310 if( !session )
5311 session = ssl->session;
5312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005314
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005315 mbedtls_md5_init( &md5 );
5316 mbedtls_sha1_init( &sha1 );
5317
5318 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5319 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005320
Paul Bakker1ef83d62012-04-11 12:09:53 +00005321 /*
5322 * TLSv1:
5323 * hash = PRF( master, finished_label,
5324 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5325 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005327#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005328 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5329 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005330#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005333 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5334 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005335#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005338 ? "client finished"
5339 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005340
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005341 mbedtls_md5_finish_ret( &md5, padbuf );
5342 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005343
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005344 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005345 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005348
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005349 mbedtls_md5_free( &md5 );
5350 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005352 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005356#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005358#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5359#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005360static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005362{
5363 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005364 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005365 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005366 unsigned char padbuf[32];
5367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005369 if( !session )
5370 session = ssl->session;
5371
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005372 mbedtls_sha256_init( &sha256 );
5373
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005375
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005376 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005377
5378 /*
5379 * TLSv1.2:
5380 * hash = PRF( master, finished_label,
5381 * Hash( handshake ) )[0.11]
5382 */
5383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005384#if !defined(MBEDTLS_SHA256_ALT)
5385 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005386 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005387#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005390 ? "client finished"
5391 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005392
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005393 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005394
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005395 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005396 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005399
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005400 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005405}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005408#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005409static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005411{
5412 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005413 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005414 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005415 unsigned char padbuf[48];
5416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005417 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005418 if( !session )
5419 session = ssl->session;
5420
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005421 mbedtls_sha512_init( &sha512 );
5422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005424
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005425 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005426
5427 /*
5428 * TLSv1.2:
5429 * hash = PRF( master, finished_label,
5430 * Hash( handshake ) )[0.11]
5431 */
5432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005433#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005434 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5435 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005436#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005438 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005439 ? "client finished"
5440 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005441
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005442 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005443
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005444 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005445 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005447 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005448
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005449 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455#endif /* MBEDTLS_SHA512_C */
5456#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005459{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005461
5462 /*
5463 * Free our handshake params
5464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465 mbedtls_ssl_handshake_free( ssl->handshake );
5466 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005467 ssl->handshake = NULL;
5468
5469 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005470 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005471 */
5472 if( ssl->transform )
5473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005474 mbedtls_ssl_transform_free( ssl->transform );
5475 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005476 }
5477 ssl->transform = ssl->transform_negotiate;
5478 ssl->transform_negotiate = NULL;
5479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005481}
5482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005483void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005484{
5485 int resume = ssl->handshake->resume;
5486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489#if defined(MBEDTLS_SSL_RENEGOTIATION)
5490 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005493 ssl->renego_records_seen = 0;
5494 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005495#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005496
5497 /*
5498 * Free the previous session and switch in the current one
5499 */
Paul Bakker0a597072012-09-25 21:55:46 +00005500 if( ssl->session )
5501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005502#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005503 /* RFC 7366 3.1: keep the EtM state */
5504 ssl->session_negotiate->encrypt_then_mac =
5505 ssl->session->encrypt_then_mac;
5506#endif
5507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508 mbedtls_ssl_session_free( ssl->session );
5509 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005510 }
5511 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005512 ssl->session_negotiate = NULL;
5513
Paul Bakker0a597072012-09-25 21:55:46 +00005514 /*
5515 * Add cache entry
5516 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005517 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005518 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005519 resume == 0 )
5520 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005521 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005523 }
Paul Bakker0a597072012-09-25 21:55:46 +00005524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005525#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005526 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005527 ssl->handshake->flight != NULL )
5528 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005529 /* Cancel handshake timer */
5530 ssl_set_timer( ssl, 0 );
5531
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005532 /* Keep last flight around in case we need to resend it:
5533 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005534 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005535 }
5536 else
5537#endif
5538 ssl_handshake_wrapup_free_hs_transform( ssl );
5539
Paul Bakker48916f92012-09-16 19:57:18 +00005540 ssl->state++;
5541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005543}
5544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005545int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005546{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005547 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005550
Paul Bakker92be97b2013-01-02 17:30:03 +01005551 /*
5552 * Set the out_msg pointer to the correct location based on IV length
5553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005554 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005555 {
5556 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5557 ssl->transform_negotiate->fixed_ivlen;
5558 }
5559 else
5560 ssl->out_msg = ssl->out_iv;
5561
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005562 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005563
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005564 /*
5565 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5566 * may define some other value. Currently (early 2016), no defined
5567 * ciphersuite does this (and this is unlikely to change as activity has
5568 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005572#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005573 ssl->verify_data_len = hash_len;
5574 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005575#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005576
Paul Bakker5121ce52009-01-03 21:22:43 +00005577 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005578 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5579 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005580
5581 /*
5582 * In case of session resuming, invert the client and server
5583 * ChangeCipherSpec messages order.
5584 */
Paul Bakker0a597072012-09-25 21:55:46 +00005585 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005587#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005588 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005590#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005591#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005592 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005594#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005595 }
5596 else
5597 ssl->state++;
5598
Paul Bakker48916f92012-09-16 19:57:18 +00005599 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005600 * Switch to our negotiated transform and session parameters for outbound
5601 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005603 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005606 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005607 {
5608 unsigned char i;
5609
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005610 /* Remember current epoch settings for resending */
5611 ssl->handshake->alt_transform_out = ssl->transform_out;
5612 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5613
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005614 /* Set sequence_number to zero */
5615 memset( ssl->out_ctr + 2, 0, 6 );
5616
5617 /* Increment epoch */
5618 for( i = 2; i > 0; i-- )
5619 if( ++ssl->out_ctr[i - 1] != 0 )
5620 break;
5621
5622 /* The loop goes to its end iff the counter is wrapping */
5623 if( i == 0 )
5624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5626 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005627 }
5628 }
5629 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005631 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005632
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005633 ssl->transform_out = ssl->transform_negotiate;
5634 ssl->session_out = ssl->session_negotiate;
5635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5637 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005639 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5642 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005643 }
5644 }
5645#endif
5646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005647#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005648 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005650#endif
5651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005652 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005655 return( ret );
5656 }
5657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005659
5660 return( 0 );
5661}
5662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005663#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005664#define SSL_MAX_HASH_LEN 36
5665#else
5666#define SSL_MAX_HASH_LEN 12
5667#endif
5668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005669int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005670{
Paul Bakker23986e52011-04-24 08:57:21 +00005671 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005672 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005673 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005676
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005677 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005681 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005682 return( ret );
5683 }
5684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005688 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5689 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005690 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005691 }
5692
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005693 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694#if defined(MBEDTLS_SSL_PROTO_SSL3)
5695 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005696 hash_len = 36;
5697 else
5698#endif
5699 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5702 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005705 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5706 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005708 }
5709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005711 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005714 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5715 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005717 }
5718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005720 ssl->verify_data_len = hash_len;
5721 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005722#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005723
Paul Bakker0a597072012-09-25 21:55:46 +00005724 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005726#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005727 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005729#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005731 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005733#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005734 }
5735 else
5736 ssl->state++;
5737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005739 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005741#endif
5742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005744
5745 return( 0 );
5746}
5747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005748static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005749{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005752#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5753 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5754 mbedtls_md5_init( &handshake->fin_md5 );
5755 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005756 mbedtls_md5_starts_ret( &handshake->fin_md5 );
5757 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005758#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5760#if defined(MBEDTLS_SHA256_C)
5761 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005762 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005763#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005764#if defined(MBEDTLS_SHA512_C)
5765 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005766 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005767#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005769
5770 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005771
5772#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5773 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5774 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5775#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777#if defined(MBEDTLS_DHM_C)
5778 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005779#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780#if defined(MBEDTLS_ECDH_C)
5781 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005782#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02005783#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005784 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02005785#if defined(MBEDTLS_SSL_CLI_C)
5786 handshake->ecjpake_cache = NULL;
5787 handshake->ecjpake_cache_len = 0;
5788#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005789#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005790
5791#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5792 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5793#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005794}
5795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005797{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5801 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803 mbedtls_md_init( &transform->md_ctx_enc );
5804 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005805}
5806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005808{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005809 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005810}
5811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005813{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005814 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005815 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005816 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005817 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005819 if( ssl->handshake )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820 mbedtls_ssl_handshake_free( ssl->handshake );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005821
5822 /*
5823 * Either the pointers are now NULL or cleared properly and can be freed.
5824 * Now allocate missing structures.
5825 */
5826 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005827 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005828 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005829 }
Paul Bakker48916f92012-09-16 19:57:18 +00005830
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005831 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005832 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005833 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005834 }
Paul Bakker48916f92012-09-16 19:57:18 +00005835
Paul Bakker82788fb2014-10-20 13:59:19 +02005836 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005837 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005838 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005839 }
Paul Bakker48916f92012-09-16 19:57:18 +00005840
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005841 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005842 if( ssl->handshake == NULL ||
5843 ssl->transform_negotiate == NULL ||
5844 ssl->session_negotiate == NULL )
5845 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848 mbedtls_free( ssl->handshake );
5849 mbedtls_free( ssl->transform_negotiate );
5850 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005851
5852 ssl->handshake = NULL;
5853 ssl->transform_negotiate = NULL;
5854 ssl->session_negotiate = NULL;
5855
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005856 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005857 }
5858
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005859 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005860 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005861 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005862 ssl_handshake_params_init( ssl->handshake );
5863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005865 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5866 {
5867 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005868
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005869 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5870 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5871 else
5872 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005873
5874 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005875 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005876#endif
5877
Paul Bakker48916f92012-09-16 19:57:18 +00005878 return( 0 );
5879}
5880
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005881#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005882/* Dummy cookie callbacks for defaults */
5883static int ssl_cookie_write_dummy( void *ctx,
5884 unsigned char **p, unsigned char *end,
5885 const unsigned char *cli_id, size_t cli_id_len )
5886{
5887 ((void) ctx);
5888 ((void) p);
5889 ((void) end);
5890 ((void) cli_id);
5891 ((void) cli_id_len);
5892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005893 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005894}
5895
5896static int ssl_cookie_check_dummy( void *ctx,
5897 const unsigned char *cookie, size_t cookie_len,
5898 const unsigned char *cli_id, size_t cli_id_len )
5899{
5900 ((void) ctx);
5901 ((void) cookie);
5902 ((void) cookie_len);
5903 ((void) cli_id);
5904 ((void) cli_id_len);
5905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005906 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005907}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005908#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005909
Paul Bakker5121ce52009-01-03 21:22:43 +00005910/*
5911 * Initialize an SSL context
5912 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005913void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
5914{
5915 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
5916}
5917
5918/*
5919 * Setup an SSL context
5920 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005921int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02005922 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00005923{
Paul Bakker48916f92012-09-16 19:57:18 +00005924 int ret;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005925 const size_t len = MBEDTLS_SSL_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005926
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005927 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00005928
5929 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005930 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00005931 */
k-stachowiakc2eddee2018-07-09 10:39:20 +02005932 ssl->in_buf = NULL;
5933 ssl->out_buf = NULL;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005934 if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL ||
5935 ( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005936 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", len ) );
k-stachowiak2c161142018-07-31 16:52:32 +02005938 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiakc2eddee2018-07-09 10:39:20 +02005939 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005940 }
5941
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005942#if defined(MBEDTLS_SSL_PROTO_DTLS)
5943 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5944 {
5945 ssl->out_hdr = ssl->out_buf;
5946 ssl->out_ctr = ssl->out_buf + 3;
5947 ssl->out_len = ssl->out_buf + 11;
5948 ssl->out_iv = ssl->out_buf + 13;
5949 ssl->out_msg = ssl->out_buf + 13;
5950
5951 ssl->in_hdr = ssl->in_buf;
5952 ssl->in_ctr = ssl->in_buf + 3;
5953 ssl->in_len = ssl->in_buf + 11;
5954 ssl->in_iv = ssl->in_buf + 13;
5955 ssl->in_msg = ssl->in_buf + 13;
5956 }
5957 else
5958#endif
5959 {
5960 ssl->out_ctr = ssl->out_buf;
5961 ssl->out_hdr = ssl->out_buf + 8;
5962 ssl->out_len = ssl->out_buf + 11;
5963 ssl->out_iv = ssl->out_buf + 13;
5964 ssl->out_msg = ssl->out_buf + 13;
5965
5966 ssl->in_ctr = ssl->in_buf;
5967 ssl->in_hdr = ssl->in_buf + 8;
5968 ssl->in_len = ssl->in_buf + 11;
5969 ssl->in_iv = ssl->in_buf + 13;
5970 ssl->in_msg = ssl->in_buf + 13;
5971 }
5972
Paul Bakker48916f92012-09-16 19:57:18 +00005973 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiakc2eddee2018-07-09 10:39:20 +02005974 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005975
5976 return( 0 );
k-stachowiakc2eddee2018-07-09 10:39:20 +02005977
5978error:
5979 mbedtls_free( ssl->in_buf );
5980 mbedtls_free( ssl->out_buf );
5981
5982 ssl->conf = NULL;
5983
5984 ssl->in_buf = NULL;
5985 ssl->out_buf = NULL;
5986
5987 ssl->in_hdr = NULL;
5988 ssl->in_ctr = NULL;
5989 ssl->in_len = NULL;
5990 ssl->in_iv = NULL;
5991 ssl->in_msg = NULL;
5992
5993 ssl->out_hdr = NULL;
5994 ssl->out_ctr = NULL;
5995 ssl->out_len = NULL;
5996 ssl->out_iv = NULL;
5997 ssl->out_msg = NULL;
5998
k-stachowiak2c161142018-07-31 16:52:32 +02005999 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006000}
6001
6002/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006003 * Reset an initialized and used SSL context for re-use while retaining
6004 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006005 *
6006 * If partial is non-zero, keep data in the input buffer and client ID.
6007 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006008 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006009static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006010{
Paul Bakker48916f92012-09-16 19:57:18 +00006011 int ret;
6012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006014
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006015 /* Cancel any possibly running timer */
6016 ssl_set_timer( ssl, 0 );
6017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018#if defined(MBEDTLS_SSL_RENEGOTIATION)
6019 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006020 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006021
6022 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6024 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006025#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006027
Paul Bakker7eb013f2011-10-06 12:37:39 +00006028 ssl->in_offt = NULL;
6029
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006030 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006031 ssl->in_msgtype = 0;
6032 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006033 if( partial == 0 )
6034 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006036 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006037 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006040 ssl_dtls_replay_reset( ssl );
6041#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006042
6043 ssl->in_hslen = 0;
6044 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006045
6046 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006047
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006048 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006049 ssl->out_msgtype = 0;
6050 ssl->out_msglen = 0;
6051 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6053 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006054 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006055#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006056
Paul Bakker48916f92012-09-16 19:57:18 +00006057 ssl->transform_in = NULL;
6058 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006059
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006060 ssl->session_in = NULL;
6061 ssl->session_out = NULL;
6062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 memset( ssl->out_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006064
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006065 if( partial == 0 )
6066 memset( ssl->in_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00006067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6069 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6072 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6075 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006076 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006077 }
6078#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006079
Paul Bakker48916f92012-09-16 19:57:18 +00006080 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006082 mbedtls_ssl_transform_free( ssl->transform );
6083 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006084 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006085 }
Paul Bakker48916f92012-09-16 19:57:18 +00006086
Paul Bakkerc0463502013-02-14 11:19:38 +01006087 if( ssl->session )
6088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006089 mbedtls_ssl_session_free( ssl->session );
6090 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006091 ssl->session = NULL;
6092 }
6093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006094#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006095 ssl->alpn_chosen = NULL;
6096#endif
6097
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006098#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006099 if( partial == 0 )
6100 {
6101 mbedtls_free( ssl->cli_id );
6102 ssl->cli_id = NULL;
6103 ssl->cli_id_len = 0;
6104 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006105#endif
6106
Paul Bakker48916f92012-09-16 19:57:18 +00006107 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6108 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006109
6110 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006111}
6112
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006113/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006114 * Reset an initialized and used SSL context for re-use while retaining
6115 * all application-set variables, function pointers and data.
6116 */
6117int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6118{
6119 return( ssl_session_reset_int( ssl, 0 ) );
6120}
6121
6122/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006123 * SSL set accessors
6124 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006125void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006126{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006127 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006128}
6129
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006130void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006131{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006132 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006133}
6134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006135#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006136void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006137{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006138 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006139}
6140#endif
6141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006143void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006144{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006145 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006146}
6147#endif
6148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006150void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006151{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006152 conf->hs_timeout_min = min;
6153 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006154}
6155#endif
6156
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006157void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006158{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006159 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006160}
6161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006162#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006163void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006164 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006165 void *p_vrfy )
6166{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006167 conf->f_vrfy = f_vrfy;
6168 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006169}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006171
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006172void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006173 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006174 void *p_rng )
6175{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006176 conf->f_rng = f_rng;
6177 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006178}
6179
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006180void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006181 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006182 void *p_dbg )
6183{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006184 conf->f_dbg = f_dbg;
6185 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006186}
6187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006189 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006190 mbedtls_ssl_send_t *f_send,
6191 mbedtls_ssl_recv_t *f_recv,
6192 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006193{
6194 ssl->p_bio = p_bio;
6195 ssl->f_send = f_send;
6196 ssl->f_recv = f_recv;
6197 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006198}
6199
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006200void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006201{
6202 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006203}
6204
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006205void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6206 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006207 mbedtls_ssl_set_timer_t *f_set_timer,
6208 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006209{
6210 ssl->p_timer = p_timer;
6211 ssl->f_set_timer = f_set_timer;
6212 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006213
6214 /* Make sure we start with no timer running */
6215 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006216}
6217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006219void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006220 void *p_cache,
6221 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6222 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006223{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006224 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006225 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006226 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006227}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230#if defined(MBEDTLS_SSL_CLI_C)
6231int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006232{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006233 int ret;
6234
6235 if( ssl == NULL ||
6236 session == NULL ||
6237 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006238 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006240 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006241 }
6242
6243 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6244 return( ret );
6245
Paul Bakker0a597072012-09-25 21:55:46 +00006246 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006247
6248 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006249}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006250#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006251
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006252void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006253 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006254{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006255 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6256 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6257 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6258 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006259}
6260
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006261void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006262 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006263 int major, int minor )
6264{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006265 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006266 return;
6267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006269 return;
6270
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006271 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006272}
6273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006274#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006275void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006276 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006277{
6278 conf->cert_profile = profile;
6279}
6280
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006281/* Append a new keycert entry to a (possibly empty) list */
6282static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6283 mbedtls_x509_crt *cert,
6284 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006285{
niisatoa35dbf12018-06-25 19:05:48 +09006286 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006287
niisatoa35dbf12018-06-25 19:05:48 +09006288 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6289 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006290 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006291
niisatoa35dbf12018-06-25 19:05:48 +09006292 new_cert->cert = cert;
6293 new_cert->key = key;
6294 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006295
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006296 /* Update head is the list was null, else add to the end */
6297 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006298 {
niisatoa35dbf12018-06-25 19:05:48 +09006299 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006300 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006301 else
6302 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006303 mbedtls_ssl_key_cert *cur = *head;
6304 while( cur->next != NULL )
6305 cur = cur->next;
niisatoa35dbf12018-06-25 19:05:48 +09006306 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006307 }
6308
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006309 return( 0 );
6310}
6311
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006312int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006313 mbedtls_x509_crt *own_cert,
6314 mbedtls_pk_context *pk_key )
6315{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006316 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006317}
6318
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006319void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006320 mbedtls_x509_crt *ca_chain,
6321 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006322{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006323 conf->ca_chain = ca_chain;
6324 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006325}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006327
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006328#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6329int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6330 mbedtls_x509_crt *own_cert,
6331 mbedtls_pk_context *pk_key )
6332{
6333 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6334 own_cert, pk_key ) );
6335}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006336
6337void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6338 mbedtls_x509_crt *ca_chain,
6339 mbedtls_x509_crl *ca_crl )
6340{
6341 ssl->handshake->sni_ca_chain = ca_chain;
6342 ssl->handshake->sni_ca_crl = ca_crl;
6343}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006344
6345void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6346 int authmode )
6347{
6348 ssl->handshake->sni_authmode = authmode;
6349}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006350#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6351
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006352#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006353/*
6354 * Set EC J-PAKE password for current handshake
6355 */
6356int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6357 const unsigned char *pw,
6358 size_t pw_len )
6359{
6360 mbedtls_ecjpake_role role;
6361
Janos Follath8eb64132016-06-03 15:40:57 +01006362 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006363 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6364
6365 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6366 role = MBEDTLS_ECJPAKE_SERVER;
6367 else
6368 role = MBEDTLS_ECJPAKE_CLIENT;
6369
6370 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6371 role,
6372 MBEDTLS_MD_SHA256,
6373 MBEDTLS_ECP_DP_SECP256R1,
6374 pw, pw_len ) );
6375}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006376#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006379int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006380 const unsigned char *psk, size_t psk_len,
6381 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006382{
Paul Bakker6db455e2013-09-18 17:29:31 +02006383 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6387 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006388
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006389 /* Identity len will be encoded on two bytes */
6390 if( ( psk_identity_len >> 16 ) != 0 ||
6391 psk_identity_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
6392 {
6393 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6394 }
6395
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006396 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006397 {
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006398 mbedtls_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006399
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006400 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006401 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006402 conf->psk_len = 0;
6403 }
6404 if( conf->psk_identity != NULL )
6405 {
6406 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006407 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006408 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006409 }
6410
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006411 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6412 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006413 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006414 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006415 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006416 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006417 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006418 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006419 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006420
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006421 conf->psk_len = psk_len;
6422 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006423
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006424 memcpy( conf->psk, psk, conf->psk_len );
6425 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006426
6427 return( 0 );
6428}
6429
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006430int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6431 const unsigned char *psk, size_t psk_len )
6432{
6433 if( psk == NULL || ssl->handshake == NULL )
6434 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6435
6436 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6437 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6438
6439 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006440 {
6441 mbedtls_zeroize( ssl->handshake->psk, ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006442 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006443 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006444 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006445
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006446 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006447 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006448
6449 ssl->handshake->psk_len = psk_len;
6450 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6451
6452 return( 0 );
6453}
6454
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006455void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006457 size_t),
6458 void *p_psk )
6459{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006460 conf->f_psk = f_psk;
6461 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006462}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006463#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006464
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006465#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006466
6467#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006468int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006469{
6470 int ret;
6471
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006472 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6473 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6474 {
6475 mbedtls_mpi_free( &conf->dhm_P );
6476 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006477 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006478 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006479
6480 return( 0 );
6481}
Hanno Becker470a8c42017-10-04 15:28:46 +01006482#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006483
Hanno Beckera90658f2017-10-04 15:29:08 +01006484int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6485 const unsigned char *dhm_P, size_t P_len,
6486 const unsigned char *dhm_G, size_t G_len )
6487{
6488 int ret;
6489
6490 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6491 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6492 {
6493 mbedtls_mpi_free( &conf->dhm_P );
6494 mbedtls_mpi_free( &conf->dhm_G );
6495 return( ret );
6496 }
6497
6498 return( 0 );
6499}
Paul Bakker5121ce52009-01-03 21:22:43 +00006500
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006501int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006502{
6503 int ret;
6504
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006505 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6506 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6507 {
6508 mbedtls_mpi_free( &conf->dhm_P );
6509 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006510 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006511 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006512
6513 return( 0 );
6514}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006515#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006516
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006517#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6518/*
6519 * Set the minimum length for Diffie-Hellman parameters
6520 */
6521void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6522 unsigned int bitlen )
6523{
6524 conf->dhm_min_bitlen = bitlen;
6525}
6526#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6527
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006528#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006529/*
6530 * Set allowed/preferred hashes for handshake signatures
6531 */
6532void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6533 const int *hashes )
6534{
6535 conf->sig_hashes = hashes;
6536}
Hanno Becker947194e2017-04-07 13:25:49 +01006537#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006538
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006539#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006540/*
6541 * Set the allowed elliptic curves
6542 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006543void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006544 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006545{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006546 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006547}
Hanno Becker947194e2017-04-07 13:25:49 +01006548#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006549
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006550#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006552{
Hanno Becker947194e2017-04-07 13:25:49 +01006553 /* Initialize to suppress unnecessary compiler warning */
6554 size_t hostname_len = 0;
6555
6556 /* Check if new hostname is valid before
6557 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006558 if( hostname != NULL )
6559 {
6560 hostname_len = strlen( hostname );
6561
6562 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6564 }
6565
6566 /* Now it's clear that we will overwrite the old hostname,
6567 * so we can free it safely */
6568
6569 if( ssl->hostname != NULL )
6570 {
6571 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
6572 mbedtls_free( ssl->hostname );
6573 }
6574
6575 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006576
Paul Bakker5121ce52009-01-03 21:22:43 +00006577 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006578 {
6579 ssl->hostname = NULL;
6580 }
6581 else
6582 {
6583 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006584 if( ssl->hostname == NULL )
6585 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006586
Hanno Becker947194e2017-04-07 13:25:49 +01006587 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006588
Hanno Becker947194e2017-04-07 13:25:49 +01006589 ssl->hostname[hostname_len] = '\0';
6590 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006591
6592 return( 0 );
6593}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006594#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006595
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006596#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006597void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006599 const unsigned char *, size_t),
6600 void *p_sni )
6601{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006602 conf->f_sni = f_sni;
6603 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006604}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006605#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006608int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006609{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006610 size_t cur_len, tot_len;
6611 const char **p;
6612
6613 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006614 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6615 * MUST NOT be truncated."
6616 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006617 */
6618 tot_len = 0;
6619 for( p = protos; *p != NULL; p++ )
6620 {
6621 cur_len = strlen( *p );
6622 tot_len += cur_len;
6623
Ronald Crona32236c2020-04-23 16:41:44 +02006624 if( ( cur_len == 0 ) ||
6625 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
6626 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006628 }
6629
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006630 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006631
6632 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006633}
6634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006636{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006637 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006638}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006640
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006641void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006642{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006643 conf->max_major_ver = major;
6644 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006645}
6646
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006647void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006648{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006649 conf->min_major_ver = major;
6650 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006651}
6652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006654void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006655{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006656 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006657}
6658#endif
6659
Janos Follath088ce432017-04-10 12:42:31 +01006660#if defined(MBEDTLS_SSL_SRV_C)
6661void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6662 char cert_req_ca_list )
6663{
6664 conf->cert_req_ca_list = cert_req_ca_list;
6665}
6666#endif
6667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006668#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006669void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006670{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006671 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006672}
6673#endif
6674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006676void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006677{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006678 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006679}
6680#endif
6681
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006682#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006683void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006684{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006685 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006686}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006687#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006690int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006691{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
6693 mfl_code_to_length[mfl_code] > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006696 }
6697
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006698 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006699
6700 return( 0 );
6701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006705void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006706{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006707 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006708}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006709#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006712void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006713{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006714 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006715}
6716#endif
6717
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006718void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006719{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006720 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006721}
6722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006724void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006725{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006726 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006727}
6728
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006729void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006730{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006731 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006732}
6733
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006734void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006735 const unsigned char period[8] )
6736{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006737 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006738}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006741#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006742#if defined(MBEDTLS_SSL_CLI_C)
6743void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006744{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006745 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006746}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006747#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006748
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006749#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006750void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6751 mbedtls_ssl_ticket_write_t *f_ticket_write,
6752 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6753 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006754{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006755 conf->f_ticket_write = f_ticket_write;
6756 conf->f_ticket_parse = f_ticket_parse;
6757 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006758}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006759#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006761
Robert Cragie4feb7ae2015-10-02 13:33:37 +01006762#if defined(MBEDTLS_SSL_EXPORT_KEYS)
6763void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
6764 mbedtls_ssl_export_keys_t *f_export_keys,
6765 void *p_export_keys )
6766{
6767 conf->f_export_keys = f_export_keys;
6768 conf->p_export_keys = p_export_keys;
6769}
6770#endif
6771
Paul Bakker5121ce52009-01-03 21:22:43 +00006772/*
6773 * SSL get accessors
6774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006776{
6777 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6778}
6779
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006780uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006781{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006782 if( ssl->session != NULL )
6783 return( ssl->session->verify_result );
6784
6785 if( ssl->session_negotiate != NULL )
6786 return( ssl->session_negotiate->verify_result );
6787
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006788 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006789}
6790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006792{
Paul Bakker926c8e42013-03-06 10:23:34 +01006793 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006794 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006797}
6798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006800{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006802 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006803 {
6804 switch( ssl->minor_ver )
6805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006806 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006807 return( "DTLSv1.0" );
6808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006810 return( "DTLSv1.2" );
6811
6812 default:
6813 return( "unknown (DTLS)" );
6814 }
6815 }
6816#endif
6817
Paul Bakker43ca69c2011-01-15 17:35:19 +00006818 switch( ssl->minor_ver )
6819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006821 return( "SSLv3.0" );
6822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006824 return( "TLSv1.0" );
6825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006827 return( "TLSv1.1" );
6828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006829 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00006830 return( "TLSv1.2" );
6831
Paul Bakker43ca69c2011-01-15 17:35:19 +00006832 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006833 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00006834 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00006835}
6836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006838{
Hanno Becker12f7ede2018-08-17 15:28:19 +01006839 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006841 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006842
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006843 if( transform == NULL )
6844 return( (int) mbedtls_ssl_hdr_len( ssl ) );
6845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846#if defined(MBEDTLS_ZLIB_SUPPORT)
6847 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
6848 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006849#endif
6850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006853 case MBEDTLS_MODE_GCM:
6854 case MBEDTLS_MODE_CCM:
6855 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006856 transform_expansion = transform->minlen;
6857 break;
6858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859 case MBEDTLS_MODE_CBC:
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006860
6861 block_size = mbedtls_cipher_get_block_size(
6862 &transform->cipher_ctx_enc );
6863
Hanno Becker12f7ede2018-08-17 15:28:19 +01006864 /* Expansion due to the addition of the MAC. */
6865 transform_expansion += transform->maclen;
6866
6867 /* Expansion due to the addition of CBC padding;
6868 * Theoretically up to 256 bytes, but we never use
6869 * more than the block size of the underlying cipher. */
6870 transform_expansion += block_size;
6871
6872 /* For TLS 1.1 or higher, an explicit IV is added
6873 * after the record header. */
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006874#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
6875 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker12f7ede2018-08-17 15:28:19 +01006876 transform_expansion += block_size;
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006877#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker12f7ede2018-08-17 15:28:19 +01006878
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006879 break;
6880
6881 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006884 }
6885
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006886 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006887}
6888
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02006889#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
6890size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
6891{
6892 size_t max_len;
6893
6894 /*
6895 * Assume mfl_code is correct since it was checked when set
6896 */
6897 max_len = mfl_code_to_length[ssl->conf->mfl_code];
6898
6899 /*
6900 * Check if a smaller max length was negotiated
6901 */
6902 if( ssl->session_out != NULL &&
6903 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6904 {
6905 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6906 }
6907
6908 return max_len;
6909}
6910#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
6911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912#if defined(MBEDTLS_X509_CRT_PARSE_C)
6913const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00006914{
6915 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006916 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006917
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006918 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006919}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00006921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922#if defined(MBEDTLS_SSL_CLI_C)
6923int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006924{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006925 if( ssl == NULL ||
6926 dst == NULL ||
6927 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006928 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006931 }
6932
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006933 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006934}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006936
Paul Bakker5121ce52009-01-03 21:22:43 +00006937/*
Paul Bakker1961b702013-01-25 14:49:24 +01006938 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00006939 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006941{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006943
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006944 if( ssl == NULL || ssl->conf == NULL )
6945 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006948 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006949 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006950#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006952 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006954#endif
6955
Paul Bakker1961b702013-01-25 14:49:24 +01006956 return( ret );
6957}
6958
6959/*
6960 * Perform the SSL handshake
6961 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01006963{
6964 int ret = 0;
6965
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006966 if( ssl == NULL || ssl->conf == NULL )
6967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01006970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01006972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01006974
6975 if( ret != 0 )
6976 break;
6977 }
6978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006980
6981 return( ret );
6982}
6983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984#if defined(MBEDTLS_SSL_RENEGOTIATION)
6985#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006986/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006987 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00006988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006990{
6991 int ret;
6992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006994
6995 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6997 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007002 return( ret );
7003 }
7004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007006
7007 return( 0 );
7008}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007010
7011/*
7012 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013 * - any side: calling mbedtls_ssl_renegotiate(),
7014 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7015 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007016 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007017 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007019 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007021{
7022 int ret;
7023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007025
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007026 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7027 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007028
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007029 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7030 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007032 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007033 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007034 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007035 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007036 ssl->handshake->out_msg_seq = 1;
7037 else
7038 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007039 }
7040#endif
7041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007042 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7043 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007048 return( ret );
7049 }
7050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007052
7053 return( 0 );
7054}
7055
7056/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007057 * Renegotiate current connection on client,
7058 * or request renegotiation on server
7059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007060int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007063
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007064 if( ssl == NULL || ssl->conf == NULL )
7065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007068 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007069 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7072 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007074 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007075
7076 /* Did we already try/start sending HelloRequest? */
7077 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007079
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007080 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007081 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007085 /*
7086 * On client, either start the renegotiation process or,
7087 * if already in progress, continue the handshake
7088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007089 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007093
7094 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007097 return( ret );
7098 }
7099 }
7100 else
7101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007105 return( ret );
7106 }
7107 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007109
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007110 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007111}
7112
7113/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007114 * Check record counters and renegotiate if they're above the limit.
7115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007117{
Andres AG2196c7f2016-12-15 17:01:16 +00007118 size_t ep_len = ssl_ep_len( ssl );
7119 int in_ctr_cmp;
7120 int out_ctr_cmp;
7121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7123 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007124 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007125 {
7126 return( 0 );
7127 }
7128
Andres AG2196c7f2016-12-15 17:01:16 +00007129 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7130 ssl->conf->renego_period + ep_len, 8 - ep_len );
7131 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
7132 ssl->conf->renego_period + ep_len, 8 - ep_len );
7133
7134 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007135 {
7136 return( 0 );
7137 }
7138
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007141}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007143
7144/*
7145 * Receive application data decrypted from the SSL layer
7146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007148{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007149 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007150 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007151
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007152 if( ssl == NULL || ssl->conf == NULL )
7153 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007158 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007161 return( ret );
7162
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007163 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007167 return( ret );
7168 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007169 }
7170#endif
7171
Hanno Becker4a810fb2017-05-24 16:27:30 +01007172 /*
7173 * Check if renegotiation is necessary and/or handshake is
7174 * in process. If yes, perform/continue, and fall through
7175 * if an unexpected packet is received while the client
7176 * is waiting for the ServerHello.
7177 *
7178 * (There is no equivalent to the last condition on
7179 * the server-side as it is not treated as within
7180 * a handshake while waiting for the ClientHello
7181 * after a renegotiation request.)
7182 */
7183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007185 ret = ssl_check_ctr_renegotiate( ssl );
7186 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7187 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007190 return( ret );
7191 }
7192#endif
7193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007197 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7198 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007201 return( ret );
7202 }
7203 }
7204
7205 if( ssl->in_offt == NULL )
7206 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007207 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007208 if( ssl->f_get_timer != NULL &&
7209 ssl->f_get_timer( ssl->p_timer ) == -1 )
7210 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007211 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007212 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007213
Hanno Becker4a810fb2017-05-24 16:27:30 +01007214 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007215 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007216 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7217 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007218
Hanno Becker4a810fb2017-05-24 16:27:30 +01007219 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7220 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007221 }
7222
7223 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007225 {
7226 /*
7227 * OpenSSL sends empty messages to randomize the IV
7228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007231 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007232 return( 0 );
7233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007234 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007235 return( ret );
7236 }
7237 }
7238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007239 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007242
Hanno Becker4a810fb2017-05-24 16:27:30 +01007243 /*
7244 * - For client-side, expect SERVER_HELLO_REQUEST.
7245 * - For server-side, expect CLIENT_HELLO.
7246 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7247 */
7248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007250 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007252 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007255
7256 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007258 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007259 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007260#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007262 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007263#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007264
Hanno Becker4a810fb2017-05-24 16:27:30 +01007265#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007266 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007270
7271 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007273 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007274 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007275#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007277 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007278#endif /* MBEDTLS_SSL_SRV_C */
7279
Hanno Becker21df7f92017-10-17 11:03:26 +01007280#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007281 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007282 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7283 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7284 ssl->conf->allow_legacy_renegotiation ==
7285 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7286 {
7287 /*
7288 * Accept renegotiation request
7289 */
Paul Bakker48916f92012-09-16 19:57:18 +00007290
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007291 /* DTLS clients need to know renego is server-initiated */
7292#if defined(MBEDTLS_SSL_PROTO_DTLS)
7293 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7294 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7295 {
7296 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7297 }
7298#endif
7299 ret = ssl_start_renegotiation( ssl );
7300 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7301 ret != 0 )
7302 {
7303 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7304 return( ret );
7305 }
7306 }
7307 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007308#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007309 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007310 /*
7311 * Refuse renegotiation
7312 */
7313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316#if defined(MBEDTLS_SSL_PROTO_SSL3)
7317 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007318 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007319 /* SSLv3 does not have a "no_renegotiation" warning, so
7320 we send a fatal alert and abort the connection. */
7321 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7322 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7323 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007324 }
7325 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007326#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7327#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7328 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7329 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7332 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7333 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007334 {
7335 return( ret );
7336 }
Paul Bakker48916f92012-09-16 19:57:18 +00007337 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007338 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7340 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7343 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007344 }
Paul Bakker48916f92012-09-16 19:57:18 +00007345 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007346
Hanno Becker4a810fb2017-05-24 16:27:30 +01007347 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00007348 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007349#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007351 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007352 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007353 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007354 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007357 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007359 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007360 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007361 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7365 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007368 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007369 }
7370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7374 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007375 }
7376
7377 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007378
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007379 /* We're going to return something now, cancel timer,
7380 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007382 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007383
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007384#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007385 /* If we requested renego but received AppData, resend HelloRequest.
7386 * Do it now, after setting in_offt, to avoid taking this branch
7387 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007389 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007391 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007392 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007394 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007395 return( ret );
7396 }
7397 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007399#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007400 }
7401
7402 n = ( len < ssl->in_msglen )
7403 ? len : ssl->in_msglen;
7404
7405 memcpy( buf, ssl->in_offt, n );
7406 ssl->in_msglen -= n;
7407
gabor-mezei-armef738752020-07-15 10:55:00 +02007408 /* Zeroising the plaintext buffer to erase unused application data
7409 from the memory. */
7410 mbedtls_zeroize( ssl->in_offt, n );
7411
Paul Bakker5121ce52009-01-03 21:22:43 +00007412 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007413 {
7414 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007415 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007416 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007417 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007418 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007419 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007420 /* more data available */
7421 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007422 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007425
Paul Bakker23986e52011-04-24 08:57:21 +00007426 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007427}
7428
7429/*
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007430 * Send application data to be encrypted by the SSL layer, taking care of max
7431 * fragment length and buffer size.
7432 *
7433 * According to RFC 5246 Section 6.2.1:
7434 *
7435 * Zero-length fragments of Application data MAY be sent as they are
7436 * potentially useful as a traffic analysis countermeasure.
7437 *
7438 * Therefore, it is possible that the input message length is 0 and the
7439 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007440 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007441static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007442 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007443{
Paul Bakker23986e52011-04-24 08:57:21 +00007444 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007446 size_t max_len = mbedtls_ssl_get_max_frag_len( ssl );
Florin0b7b83f2017-07-22 09:01:44 +02007447#else
7448 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
7449#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007450 if( len > max_len )
7451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007453 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007456 "maximum fragment length: %d > %d",
7457 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007459 }
7460 else
7461#endif
7462 len = max_len;
7463 }
Paul Bakker887bd502011-06-08 13:10:54 +00007464
Paul Bakker5121ce52009-01-03 21:22:43 +00007465 if( ssl->out_left != 0 )
7466 {
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007467 /*
7468 * The user has previously tried to send the data and
7469 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7470 * written. In this case, we expect the high-level write function
7471 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007473 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007475 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007476 return( ret );
7477 }
7478 }
Paul Bakker887bd502011-06-08 13:10:54 +00007479 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007480 {
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007481 /*
7482 * The user is trying to send a message the first time, so we need to
7483 * copy the data into the internal buffers and setup the data structure
7484 * to keep track of partial writes
7485 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007486 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007487 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007488 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007490 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007492 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007493 return( ret );
7494 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007495 }
7496
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007497 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007498}
7499
7500/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007501 * Write application data, doing 1/n-1 splitting if necessary.
7502 *
7503 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007504 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01007505 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007508static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007509 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007510{
7511 int ret;
7512
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007513 if( ssl->conf->cbc_record_splitting ==
7514 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007515 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7517 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7518 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007519 {
7520 return( ssl_write_real( ssl, buf, len ) );
7521 }
7522
7523 if( ssl->split_done == 0 )
7524 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007525 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007526 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007527 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007528 }
7529
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007530 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7531 return( ret );
7532 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007533
7534 return( ret + 1 );
7535}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007537
7538/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007539 * Write application data (public-facing wrapper)
7540 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007541int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007542{
7543 int ret;
7544
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007546
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007547 if( ssl == NULL || ssl->conf == NULL )
7548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7549
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007550#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007551 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7552 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007553 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007554 return( ret );
7555 }
7556#endif
7557
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007558 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007559 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007560 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007561 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007562 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007563 return( ret );
7564 }
7565 }
7566
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007567#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007568 ret = ssl_write_split( ssl, buf, len );
7569#else
7570 ret = ssl_write_real( ssl, buf, len );
7571#endif
7572
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007574
7575 return( ret );
7576}
7577
7578/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007579 * Notify the peer that the connection is being closed
7580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007582{
7583 int ret;
7584
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007585 if( ssl == NULL || ssl->conf == NULL )
7586 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007589
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007590 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007595 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7596 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7597 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007600 return( ret );
7601 }
7602 }
7603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007605
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007606 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007607}
7608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00007610{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007611 if( transform == NULL )
7612 return;
7613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00007615 deflateEnd( &transform->ctx_deflate );
7616 inflateEnd( &transform->ctx_inflate );
7617#endif
7618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619 mbedtls_cipher_free( &transform->cipher_ctx_enc );
7620 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02007621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622 mbedtls_md_free( &transform->md_ctx_enc );
7623 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02007624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625 mbedtls_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007626}
7627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628#if defined(MBEDTLS_X509_CRT_PARSE_C)
7629static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007630{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007632
7633 while( cur != NULL )
7634 {
7635 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007637 cur = next;
7638 }
7639}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake )
Paul Bakker48916f92012-09-16 19:57:18 +00007643{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007644 if( handshake == NULL )
7645 return;
7646
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007647#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7648 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7649 mbedtls_md5_free( &handshake->fin_md5 );
7650 mbedtls_sha1_free( &handshake->fin_sha1 );
7651#endif
7652#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7653#if defined(MBEDTLS_SHA256_C)
7654 mbedtls_sha256_free( &handshake->fin_sha256 );
7655#endif
7656#if defined(MBEDTLS_SHA512_C)
7657 mbedtls_sha512_free( &handshake->fin_sha512 );
7658#endif
7659#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007661#if defined(MBEDTLS_DHM_C)
7662 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007663#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664#if defined(MBEDTLS_ECDH_C)
7665 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007666#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007667#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007668 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007669#if defined(MBEDTLS_SSL_CLI_C)
7670 mbedtls_free( handshake->ecjpake_cache );
7671 handshake->ecjpake_cache = NULL;
7672 handshake->ecjpake_cache_len = 0;
7673#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007674#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02007675
Janos Follath4ae5c292016-02-10 11:27:43 +00007676#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
7677 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02007678 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007679 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007680#endif
7681
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007682#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7683 if( handshake->psk != NULL )
7684 {
7685 mbedtls_zeroize( handshake->psk, handshake->psk_len );
7686 mbedtls_free( handshake->psk );
7687 }
7688#endif
7689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007690#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7691 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007692 /*
7693 * Free only the linked list wrapper, not the keys themselves
7694 * since the belong to the SNI callback
7695 */
7696 if( handshake->sni_key_cert != NULL )
7697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007699
7700 while( cur != NULL )
7701 {
7702 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007704 cur = next;
7705 }
7706 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709#if defined(MBEDTLS_SSL_PROTO_DTLS)
7710 mbedtls_free( handshake->verify_cookie );
7711 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007712 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007713#endif
7714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007715 mbedtls_zeroize( handshake, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007716}
7717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007719{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007720 if( session == NULL )
7721 return;
7722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007724 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726 mbedtls_x509_crt_free( session->peer_cert );
7727 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007728 }
Paul Bakkered27a042013-04-18 22:46:23 +02007729#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007730
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007731#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007732 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007733#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735 mbedtls_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007736}
7737
Paul Bakker5121ce52009-01-03 21:22:43 +00007738/*
7739 * Free an SSL context
7740 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007741void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007742{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007743 if( ssl == NULL )
7744 return;
7745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007747
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007748 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750 mbedtls_zeroize( ssl->out_buf, MBEDTLS_SSL_BUFFER_LEN );
7751 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007752 }
7753
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007754 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756 mbedtls_zeroize( ssl->in_buf, MBEDTLS_SSL_BUFFER_LEN );
7757 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007758 }
7759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02007761 if( ssl->compress_buf != NULL )
7762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763 mbedtls_zeroize( ssl->compress_buf, MBEDTLS_SSL_BUFFER_LEN );
7764 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02007765 }
7766#endif
7767
Paul Bakker48916f92012-09-16 19:57:18 +00007768 if( ssl->transform )
7769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770 mbedtls_ssl_transform_free( ssl->transform );
7771 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007772 }
7773
7774 if( ssl->handshake )
7775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007776 mbedtls_ssl_handshake_free( ssl->handshake );
7777 mbedtls_ssl_transform_free( ssl->transform_negotiate );
7778 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007780 mbedtls_free( ssl->handshake );
7781 mbedtls_free( ssl->transform_negotiate );
7782 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007783 }
7784
Paul Bakkerc0463502013-02-14 11:19:38 +01007785 if( ssl->session )
7786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787 mbedtls_ssl_session_free( ssl->session );
7788 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007789 }
7790
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02007791#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02007792 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007793 {
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007794 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00007796 }
Paul Bakker0be444a2013-08-27 21:55:01 +02007797#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7800 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
7803 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00007804 }
7805#endif
7806
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007807#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007808 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007809#endif
7810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00007812
Paul Bakker86f04f42013-02-14 11:20:09 +01007813 /* Actually clear after last debug message */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007814 mbedtls_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007815}
7816
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007817/*
7818 * Initialze mbedtls_ssl_config
7819 */
7820void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
7821{
7822 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
7823}
7824
Simon Butcherc97b6972015-12-27 23:48:17 +00007825#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007826static int ssl_preset_default_hashes[] = {
7827#if defined(MBEDTLS_SHA512_C)
7828 MBEDTLS_MD_SHA512,
7829 MBEDTLS_MD_SHA384,
7830#endif
7831#if defined(MBEDTLS_SHA256_C)
7832 MBEDTLS_MD_SHA256,
7833 MBEDTLS_MD_SHA224,
7834#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02007835#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007836 MBEDTLS_MD_SHA1,
7837#endif
7838 MBEDTLS_MD_NONE
7839};
Simon Butcherc97b6972015-12-27 23:48:17 +00007840#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007841
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007842static int ssl_preset_suiteb_ciphersuites[] = {
7843 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
7844 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
7845 0
7846};
7847
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007848#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007849static int ssl_preset_suiteb_hashes[] = {
7850 MBEDTLS_MD_SHA256,
7851 MBEDTLS_MD_SHA384,
7852 MBEDTLS_MD_NONE
7853};
7854#endif
7855
7856#if defined(MBEDTLS_ECP_C)
7857static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007858#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007859 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007860#endif
7861#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007862 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007863#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007864 MBEDTLS_ECP_DP_NONE
7865};
7866#endif
7867
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007868/*
Tillmann Karras588ad502015-09-25 04:27:22 +02007869 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007870 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007871int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007872 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007873{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007874#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007875 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007876#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007877
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02007878 /* Use the functions here so that they are covered in tests,
7879 * but otherwise access member directly for efficiency */
7880 mbedtls_ssl_conf_endpoint( conf, endpoint );
7881 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007882
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007883 /*
7884 * Things that are common to all presets
7885 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007886#if defined(MBEDTLS_SSL_CLI_C)
7887 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7888 {
7889 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
7890#if defined(MBEDTLS_SSL_SESSION_TICKETS)
7891 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
7892#endif
7893 }
7894#endif
7895
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007896#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007897 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007898#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007899
7900#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7901 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
7902#endif
7903
7904#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7905 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
7906#endif
7907
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007908#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7909 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7910#endif
7911
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007912#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007913 conf->f_cookie_write = ssl_cookie_write_dummy;
7914 conf->f_cookie_check = ssl_cookie_check_dummy;
7915#endif
7916
7917#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7918 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7919#endif
7920
Janos Follath088ce432017-04-10 12:42:31 +01007921#if defined(MBEDTLS_SSL_SRV_C)
7922 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
7923#endif
7924
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007925#if defined(MBEDTLS_SSL_PROTO_DTLS)
7926 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7927 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7928#endif
7929
7930#if defined(MBEDTLS_SSL_RENEGOTIATION)
7931 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00007932 memset( conf->renego_period, 0x00, 2 );
7933 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007934#endif
7935
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007936#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
7937 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7938 {
Hanno Becker00d0a682017-10-04 13:14:29 +01007939 const unsigned char dhm_p[] =
7940 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
7941 const unsigned char dhm_g[] =
7942 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
7943
Hanno Beckera90658f2017-10-04 15:29:08 +01007944 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
7945 dhm_p, sizeof( dhm_p ),
7946 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007947 {
7948 return( ret );
7949 }
7950 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007951#endif
7952
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007953 /*
7954 * Preset-specific defaults
7955 */
7956 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007957 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007958 /*
7959 * NSA Suite B
7960 */
7961 case MBEDTLS_SSL_PRESET_SUITEB:
7962 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7963 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7964 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7965 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7966
7967 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7968 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7969 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7970 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7971 ssl_preset_suiteb_ciphersuites;
7972
7973#if defined(MBEDTLS_X509_CRT_PARSE_C)
7974 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007975#endif
7976
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007977#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007978 conf->sig_hashes = ssl_preset_suiteb_hashes;
7979#endif
7980
7981#if defined(MBEDTLS_ECP_C)
7982 conf->curve_list = ssl_preset_suiteb_curves;
7983#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007984 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007985
7986 /*
7987 * Default
7988 */
7989 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03007990 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
7991 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
7992 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7993 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7994 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
7995 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
7996 MBEDTLS_SSL_MIN_MINOR_VERSION :
7997 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007998 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7999 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8000
8001#if defined(MBEDTLS_SSL_PROTO_DTLS)
8002 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8003 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8004#endif
8005
8006 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8007 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8008 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8009 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8010 mbedtls_ssl_list_ciphersuites();
8011
8012#if defined(MBEDTLS_X509_CRT_PARSE_C)
8013 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8014#endif
8015
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008016#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008017 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008018#endif
8019
8020#if defined(MBEDTLS_ECP_C)
8021 conf->curve_list = mbedtls_ecp_grp_id_list();
8022#endif
8023
8024#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8025 conf->dhm_min_bitlen = 1024;
8026#endif
8027 }
8028
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008029 return( 0 );
8030}
8031
8032/*
8033 * Free mbedtls_ssl_config
8034 */
8035void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8036{
8037#if defined(MBEDTLS_DHM_C)
8038 mbedtls_mpi_free( &conf->dhm_P );
8039 mbedtls_mpi_free( &conf->dhm_G );
8040#endif
8041
8042#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8043 if( conf->psk != NULL )
8044 {
8045 mbedtls_zeroize( conf->psk, conf->psk_len );
8046 mbedtls_zeroize( conf->psk_identity, conf->psk_identity_len );
8047 mbedtls_free( conf->psk );
8048 mbedtls_free( conf->psk_identity );
8049 conf->psk_len = 0;
8050 conf->psk_identity_len = 0;
8051 }
8052#endif
8053
8054#if defined(MBEDTLS_X509_CRT_PARSE_C)
8055 ssl_key_cert_free( conf->key_cert );
8056#endif
8057
8058 mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
8059}
8060
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008061#if defined(MBEDTLS_PK_C) && \
8062 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008063/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008064 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008066unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008068#if defined(MBEDTLS_RSA_C)
8069 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8070 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008071#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008072#if defined(MBEDTLS_ECDSA_C)
8073 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8074 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008075#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008077}
8078
Hanno Becker7e5437a2017-04-28 17:15:26 +01008079unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8080{
8081 switch( type ) {
8082 case MBEDTLS_PK_RSA:
8083 return( MBEDTLS_SSL_SIG_RSA );
8084 case MBEDTLS_PK_ECDSA:
8085 case MBEDTLS_PK_ECKEY:
8086 return( MBEDTLS_SSL_SIG_ECDSA );
8087 default:
8088 return( MBEDTLS_SSL_SIG_ANON );
8089 }
8090}
8091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008092mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008093{
8094 switch( sig )
8095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096#if defined(MBEDTLS_RSA_C)
8097 case MBEDTLS_SSL_SIG_RSA:
8098 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008099#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100#if defined(MBEDTLS_ECDSA_C)
8101 case MBEDTLS_SSL_SIG_ECDSA:
8102 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008103#endif
8104 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008106 }
8107}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008108#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008109
Hanno Becker7e5437a2017-04-28 17:15:26 +01008110#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8111 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8112
8113/* Find an entry in a signature-hash set matching a given hash algorithm. */
8114mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8115 mbedtls_pk_type_t sig_alg )
8116{
8117 switch( sig_alg )
8118 {
8119 case MBEDTLS_PK_RSA:
8120 return( set->rsa );
8121 case MBEDTLS_PK_ECDSA:
8122 return( set->ecdsa );
8123 default:
8124 return( MBEDTLS_MD_NONE );
8125 }
8126}
8127
8128/* Add a signature-hash-pair to a signature-hash set */
8129void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8130 mbedtls_pk_type_t sig_alg,
8131 mbedtls_md_type_t md_alg )
8132{
8133 switch( sig_alg )
8134 {
8135 case MBEDTLS_PK_RSA:
8136 if( set->rsa == MBEDTLS_MD_NONE )
8137 set->rsa = md_alg;
8138 break;
8139
8140 case MBEDTLS_PK_ECDSA:
8141 if( set->ecdsa == MBEDTLS_MD_NONE )
8142 set->ecdsa = md_alg;
8143 break;
8144
8145 default:
8146 break;
8147 }
8148}
8149
8150/* Allow exactly one hash algorithm for each signature. */
8151void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8152 mbedtls_md_type_t md_alg )
8153{
8154 set->rsa = md_alg;
8155 set->ecdsa = md_alg;
8156}
8157
8158#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8159 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8160
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008161/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008162 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008165{
8166 switch( hash )
8167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008168#if defined(MBEDTLS_MD5_C)
8169 case MBEDTLS_SSL_HASH_MD5:
8170 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008171#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172#if defined(MBEDTLS_SHA1_C)
8173 case MBEDTLS_SSL_HASH_SHA1:
8174 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176#if defined(MBEDTLS_SHA256_C)
8177 case MBEDTLS_SSL_HASH_SHA224:
8178 return( MBEDTLS_MD_SHA224 );
8179 case MBEDTLS_SSL_HASH_SHA256:
8180 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008181#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008182#if defined(MBEDTLS_SHA512_C)
8183 case MBEDTLS_SSL_HASH_SHA384:
8184 return( MBEDTLS_MD_SHA384 );
8185 case MBEDTLS_SSL_HASH_SHA512:
8186 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008187#endif
8188 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008189 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008190 }
8191}
8192
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008193/*
8194 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8195 */
8196unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8197{
8198 switch( md )
8199 {
8200#if defined(MBEDTLS_MD5_C)
8201 case MBEDTLS_MD_MD5:
8202 return( MBEDTLS_SSL_HASH_MD5 );
8203#endif
8204#if defined(MBEDTLS_SHA1_C)
8205 case MBEDTLS_MD_SHA1:
8206 return( MBEDTLS_SSL_HASH_SHA1 );
8207#endif
8208#if defined(MBEDTLS_SHA256_C)
8209 case MBEDTLS_MD_SHA224:
8210 return( MBEDTLS_SSL_HASH_SHA224 );
8211 case MBEDTLS_MD_SHA256:
8212 return( MBEDTLS_SSL_HASH_SHA256 );
8213#endif
8214#if defined(MBEDTLS_SHA512_C)
8215 case MBEDTLS_MD_SHA384:
8216 return( MBEDTLS_SSL_HASH_SHA384 );
8217 case MBEDTLS_MD_SHA512:
8218 return( MBEDTLS_SSL_HASH_SHA512 );
8219#endif
8220 default:
8221 return( MBEDTLS_SSL_HASH_NONE );
8222 }
8223}
8224
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008225#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008226/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008227 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008228 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008229 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008230int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008231{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008233
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008234 if( ssl->conf->curve_list == NULL )
8235 return( -1 );
8236
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008237 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008238 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008239 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008240
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008241 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008242}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008243#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008244
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008245#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008246/*
8247 * Check if a hash proposed by the peer is in our list.
8248 * Return 0 if we're willing to use it, -1 otherwise.
8249 */
8250int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8251 mbedtls_md_type_t md )
8252{
8253 const int *cur;
8254
8255 if( ssl->conf->sig_hashes == NULL )
8256 return( -1 );
8257
8258 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8259 if( *cur == (int) md )
8260 return( 0 );
8261
8262 return( -1 );
8263}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008264#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266#if defined(MBEDTLS_X509_CRT_PARSE_C)
8267int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8268 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008269 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008270 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008271{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008272 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008274 int usage = 0;
8275#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008277 const char *ext_oid;
8278 size_t ext_len;
8279#endif
8280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008281#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8282 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008283 ((void) cert);
8284 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008285 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008286#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008288#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8289 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008290 {
8291 /* Server part of the key exchange */
8292 switch( ciphersuite->key_exchange )
8293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008294 case MBEDTLS_KEY_EXCHANGE_RSA:
8295 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008296 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008297 break;
8298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8300 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8301 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8302 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008303 break;
8304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8306 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008307 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008308 break;
8309
8310 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311 case MBEDTLS_KEY_EXCHANGE_NONE:
8312 case MBEDTLS_KEY_EXCHANGE_PSK:
8313 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8314 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008315 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008316 usage = 0;
8317 }
8318 }
8319 else
8320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008321 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8322 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008323 }
8324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008325 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008326 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008327 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008328 ret = -1;
8329 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008330#else
8331 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8335 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8338 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008339 }
8340 else
8341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8343 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008344 }
8345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008347 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008348 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008349 ret = -1;
8350 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008352
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008353 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008356
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008357/*
8358 * Convert version numbers to/from wire format
8359 * and, for DTLS, to/from TLS equivalent.
8360 *
8361 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008362 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008363 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8364 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008366void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008367 unsigned char ver[2] )
8368{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008369#if defined(MBEDTLS_SSL_PROTO_DTLS)
8370 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008373 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8374
8375 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8376 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8377 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008378 else
8379#else
8380 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008381#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008382 {
8383 ver[0] = (unsigned char) major;
8384 ver[1] = (unsigned char) minor;
8385 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008386}
8387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008389 const unsigned char ver[2] )
8390{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391#if defined(MBEDTLS_SSL_PROTO_DTLS)
8392 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008393 {
8394 *major = 255 - ver[0] + 2;
8395 *minor = 255 - ver[1] + 1;
8396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008397 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008398 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8399 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008400 else
8401#else
8402 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008403#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008404 {
8405 *major = ver[0];
8406 *minor = ver[1];
8407 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008408}
8409
Simon Butcher99000142016-10-13 17:21:01 +01008410int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8411{
8412#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8413 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8414 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8415
8416 switch( md )
8417 {
8418#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8419#if defined(MBEDTLS_MD5_C)
8420 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008421 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008422#endif
8423#if defined(MBEDTLS_SHA1_C)
8424 case MBEDTLS_SSL_HASH_SHA1:
8425 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8426 break;
8427#endif
8428#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8429#if defined(MBEDTLS_SHA512_C)
8430 case MBEDTLS_SSL_HASH_SHA384:
8431 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8432 break;
8433#endif
8434#if defined(MBEDTLS_SHA256_C)
8435 case MBEDTLS_SSL_HASH_SHA256:
8436 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8437 break;
8438#endif
8439 default:
8440 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8441 }
8442
8443 return 0;
8444#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8445 (void) ssl;
8446 (void) md;
8447
8448 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8449#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8450}
8451
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008452#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8453 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8454int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8455 unsigned char *output,
8456 unsigned char *data, size_t data_len )
8457{
8458 int ret = 0;
8459 mbedtls_md5_context mbedtls_md5;
8460 mbedtls_sha1_context mbedtls_sha1;
8461
8462 mbedtls_md5_init( &mbedtls_md5 );
8463 mbedtls_sha1_init( &mbedtls_sha1 );
8464
8465 /*
8466 * digitally-signed struct {
8467 * opaque md5_hash[16];
8468 * opaque sha_hash[20];
8469 * };
8470 *
8471 * md5_hash
8472 * MD5(ClientHello.random + ServerHello.random
8473 * + ServerParams);
8474 * sha_hash
8475 * SHA(ClientHello.random + ServerHello.random
8476 * + ServerParams);
8477 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008478 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008479 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008481 goto exit;
8482 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008483 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008484 ssl->handshake->randbytes, 64 ) ) != 0 )
8485 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008486 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008487 goto exit;
8488 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008489 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008490 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008491 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008492 goto exit;
8493 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008494 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008495 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008496 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008497 goto exit;
8498 }
8499
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008500 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008501 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008502 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008503 goto exit;
8504 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008505 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008506 ssl->handshake->randbytes, 64 ) ) != 0 )
8507 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008508 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008509 goto exit;
8510 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008511 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008512 data_len ) ) != 0 )
8513 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008514 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008515 goto exit;
8516 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008517 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008518 output + 16 ) ) != 0 )
8519 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008520 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008521 goto exit;
8522 }
8523
8524exit:
8525 mbedtls_md5_free( &mbedtls_md5 );
8526 mbedtls_sha1_free( &mbedtls_sha1 );
8527
8528 if( ret != 0 )
8529 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8530 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8531
8532 return( ret );
8533
8534}
8535#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
8536 MBEDTLS_SSL_PROTO_TLS1_1 */
8537
8538#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8539 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8540int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8541 unsigned char *output,
8542 unsigned char *data, size_t data_len,
8543 mbedtls_md_type_t md_alg )
8544{
8545 int ret = 0;
8546 mbedtls_md_context_t ctx;
8547 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8548
8549 mbedtls_md_init( &ctx );
8550
8551 /*
8552 * digitally-signed struct {
8553 * opaque client_random[32];
8554 * opaque server_random[32];
8555 * ServerDHParams params;
8556 * };
8557 */
8558 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8559 {
8560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8561 goto exit;
8562 }
8563 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8564 {
8565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8566 goto exit;
8567 }
8568 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8569 {
8570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8571 goto exit;
8572 }
8573 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8574 {
8575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8576 goto exit;
8577 }
8578 if( ( ret = mbedtls_md_finish( &ctx, output ) ) != 0 )
8579 {
8580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8581 goto exit;
8582 }
8583
8584exit:
8585 mbedtls_md_free( &ctx );
8586
8587 if( ret != 0 )
8588 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8589 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8590
8591 return( ret );
8592}
8593#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
8594 MBEDTLS_SSL_PROTO_TLS1_2 */
8595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596#endif /* MBEDTLS_SSL_TLS_C */