blob: 8c9925b5800900861e1591279be3f6acb4582e31 [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é-Gonnard3b490a02020-08-25 11:18:11 +02001788
1789/*
1790 * Constant-flow memcpy from variable position in buffer.
1791 * - functionally equivalent to memcpy(dst, src + offset_secret, len)
1792 * - but with execution flow independant from the value of offset_secret.
1793 */
1794void mbedtls_ssl_cf_memcpy_offset( unsigned char *dst,
1795 const unsigned char *src_base,
1796 size_t offset_secret,
1797 size_t offset_min, size_t offset_max,
1798 size_t len )
1799{
1800 /* WIP - THIS IS NOT ACTUALLY CONSTANT-FLOW!
1801 * This is just to be able to write tests and check they work. */
1802 ssl_read_memory( src_base + offset_min, offset_max - offset_min + len );
1803 memcpy( dst, src_base + offset_secret, len );
1804}
Manuel Pégourié-Gonnard41df0f22020-07-28 11:35:39 +02001805#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001808{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001809 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001811 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001812#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001813 size_t padlen = 0, correct = 1;
1814#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001817
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001818 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1821 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001822 }
1823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001825
Paul Bakker48916f92012-09-16 19:57:18 +00001826 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001829 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001831 }
1832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1834 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001835 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001836 int ret;
1837 size_t olen = 0;
1838
Paul Bakker68884e32013-01-07 18:20:04 +01001839 padlen = 0;
1840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001842 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001843 ssl->transform_in->ivlen,
1844 ssl->in_msg, ssl->in_msglen,
1845 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001848 return( ret );
1849 }
1850
1851 if( ssl->in_msglen != olen )
1852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1854 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001855 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001856 }
Paul Bakker68884e32013-01-07 18:20:04 +01001857 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1859#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1860 if( mode == MBEDTLS_MODE_GCM ||
1861 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001862 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001863 int ret;
1864 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001865 unsigned char *dec_msg;
1866 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001867 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001868 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001870 size_t explicit_iv_len = ssl->transform_in->ivlen -
1871 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001872
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001873 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001876 "+ taglen (%d)", ssl->in_msglen,
1877 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001879 }
1880 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1881
Paul Bakker68884e32013-01-07 18:20:04 +01001882 dec_msg = ssl->in_msg;
1883 dec_msg_result = ssl->in_msg;
1884 ssl->in_msglen = dec_msglen;
1885
1886 memcpy( add_data, ssl->in_ctr, 8 );
1887 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001889 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001890 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1891 add_data[12] = ssl->in_msglen & 0xFF;
1892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakker68884e32013-01-07 18:20:04 +01001894 add_data, 13 );
1895
1896 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1897 ssl->in_iv,
1898 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
Paul Bakker68884e32013-01-07 18:20:04 +01001901 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001903
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001904 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001905 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001906 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001908 ssl->transform_in->iv_dec,
1909 ssl->transform_in->ivlen,
1910 add_data, 13,
1911 dec_msg, dec_msglen,
1912 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001913 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1918 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001919
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001920 return( ret );
1921 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001922 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001923
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001924 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1927 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001928 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001929 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001932#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001934 {
Paul Bakker45829992013-01-03 14:52:21 +01001935 /*
1936 * Decrypt and check the padding
1937 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001938 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001939 unsigned char *dec_msg;
1940 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001941 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001942 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001943 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001944
Paul Bakker5121ce52009-01-03 21:22:43 +00001945 /*
Paul Bakker45829992013-01-03 14:52:21 +01001946 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1949 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001950 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001951#endif
Paul Bakker45829992013-01-03 14:52:21 +01001952
1953 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1954 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001957 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1958 ssl->transform_in->ivlen,
1959 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001961 }
1962
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001963 dec_msglen = ssl->in_msglen;
1964 dec_msg = ssl->in_msg;
1965 dec_msg_result = ssl->in_msg;
1966
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001967 /*
1968 * Authenticate before decrypt if enabled
1969 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1971 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001972 {
Hanno Becker992b6872017-11-09 18:57:39 +00001973 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001974 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001977
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001978 dec_msglen -= ssl->transform_in->maclen;
1979 ssl->in_msglen -= ssl->transform_in->maclen;
1980
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001981 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1982 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1983 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1984 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1989 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001990 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001991 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001995 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001996 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001997 ssl->transform_in->maclen );
1998
Hanno Becker992b6872017-11-09 18:57:39 +00001999 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2000 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002005 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002006 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002007 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002009
2010 /*
2011 * Check length sanity
2012 */
2013 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002016 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002018 }
2019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002021 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002022 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002025 {
Paul Bakker48916f92012-09-16 19:57:18 +00002026 dec_msglen -= ssl->transform_in->ivlen;
2027 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002028
Paul Bakker48916f92012-09-16 19:57:18 +00002029 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002030 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002031 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002035 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002036 ssl->transform_in->ivlen,
2037 dec_msg, dec_msglen,
2038 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002041 return( ret );
2042 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002043
Paul Bakkercca5b812013-08-31 17:40:26 +02002044 if( dec_msglen != olen )
2045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2047 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002048 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2051 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002052 {
2053 /*
2054 * Save IV in SSL3 and TLS1
2055 */
2056 memcpy( ssl->transform_in->iv_dec,
2057 ssl->transform_in->cipher_ctx_dec.iv,
2058 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002060#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002061
2062 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002063
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002064 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002065 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002067#if defined(MBEDTLS_SSL_DEBUG_ALL)
2068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002069 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002070#endif
Paul Bakker45829992013-01-03 14:52:21 +01002071 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002072 correct = 0;
2073 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075#if defined(MBEDTLS_SSL_PROTO_SSL3)
2076 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002077 {
Paul Bakker48916f92012-09-16 19:57:18 +00002078 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080#if defined(MBEDTLS_SSL_DEBUG_ALL)
2081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002083 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002084#endif
Paul Bakker45829992013-01-03 14:52:21 +01002085 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 }
2087 }
2088 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2090#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2091 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2092 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 {
2094 /*
Paul Bakker45829992013-01-03 14:52:21 +01002095 * TLSv1+: always check the padding up to the first failure
2096 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002098 size_t pad_count = 0, real_count = 1;
Angus Gratton1ba8e912018-06-19 15:57:50 +10002099 size_t padding_idx = ssl->in_msglen - padlen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002100
Paul Bakker956c9e02013-12-19 14:42:28 +01002101 /*
2102 * Padding is guaranteed to be incorrect if:
Angus Gratton1ba8e912018-06-19 15:57:50 +10002103 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002104 *
Angus Gratton1ba8e912018-06-19 15:57:50 +10002105 * 2. padding_idx > MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002106 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002107 *
2108 * In both cases we reset padding_idx to a safe value (0) to
2109 * prevent out-of-buffer reads.
2110 */
Angus Gratton1ba8e912018-06-19 15:57:50 +10002111 correct &= ( padlen <= ssl->in_msglen );
2112 correct &= ( padding_idx <= MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002113 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002114
2115 padding_idx *= correct;
2116
Angus Gratton1ba8e912018-06-19 15:57:50 +10002117 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002118 {
Angus Gratton1ba8e912018-06-19 15:57:50 +10002119 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002120 pad_count += real_count *
2121 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2122 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002123
2124 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002127 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002129#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002130 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002132 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2134 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2137 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002138 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002139
2140 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002142 else
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02002143#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2146 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002147 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002148
Manuel Pégourié-Gonnard7c344322018-07-10 11:15:36 +02002149#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard7c344322018-07-10 11:15:36 +02002152#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002153
2154 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002155 * Authenticate if not done yet.
2156 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002158#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002159 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002160 {
Hanno Becker992b6872017-11-09 18:57:39 +00002161 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnardbf7a49e2020-08-25 11:07:25 +02002162 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002163
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002164 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002165
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002166 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2167 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169#if defined(MBEDTLS_SSL_PROTO_SSL3)
2170 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002171 {
2172 ssl_mac( &ssl->transform_in->md_ctx_dec,
2173 ssl->transform_in->mac_dec,
2174 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002175 ssl->in_ctr, ssl->in_msgtype,
2176 mac_expect );
Manuel Pégourié-Gonnardbf7a49e2020-08-25 11:07:25 +02002177 memcpy( mac_peer, ssl->in_msg + ssl->in_msglen,
2178 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002179 }
2180 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2182#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2183 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2184 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002185 {
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002186 int ret;
2187 unsigned char add_data[13];
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002188
2189 /*
2190 * The next two sizes are the minimum and maximum values of
2191 * in_msglen over all padlen values.
2192 *
2193 * They're independent of padlen, since we previously did
2194 * in_msglen -= padlen.
2195 *
2196 * Note that max_len + maclen is never more than the buffer
2197 * length, as we previously did in_msglen -= maclen too.
2198 */
2199 const size_t max_len = ssl->in_msglen + padlen;
2200 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2201
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002202 memcpy( add_data + 0, ssl->in_ctr, 8 );
2203 memcpy( add_data + 8, ssl->in_hdr, 3 );
2204 memcpy( add_data + 11, ssl->in_len, 2 );
2205
2206 ret = mbedtls_ssl_cf_hmac( &ssl->transform_in->md_ctx_dec,
2207 add_data, sizeof( add_data ),
2208 ssl->in_msg, ssl->in_msglen,
2209 min_len, max_len,
2210 mac_expect );
2211 if( ret != 0 )
Gilles Peskinebb07ca02018-06-06 17:23:31 +02002212 {
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
2214 return( ret );
Gilles Peskinebb07ca02018-06-06 17:23:31 +02002215 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002216
Manuel Pégourié-Gonnard3b490a02020-08-25 11:18:11 +02002217 mbedtls_ssl_cf_memcpy_offset( mac_peer, ssl->in_msg,
2218 ssl->in_msglen,
2219 min_len, max_len,
2220 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002221 }
2222 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2224 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002228 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002229
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002230#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002231 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
Manuel Pégourié-Gonnardbf7a49e2020-08-25 11:07:25 +02002232 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, ssl->transform_in->maclen );
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002233#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002234
Manuel Pégourié-Gonnardbf7a49e2020-08-25 11:07:25 +02002235 if( mbedtls_ssl_safer_memcmp( mac_peer, mac_expect,
Hanno Becker992b6872017-11-09 18:57:39 +00002236 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238#if defined(MBEDTLS_SSL_DEBUG_ALL)
2239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002240#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002241 correct = 0;
2242 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002243 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002244 }
Hanno Beckerca31b472018-10-17 14:43:14 +01002245
2246 /*
2247 * Finally check the correct flag
2248 */
2249 if( correct == 0 )
2250 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002251#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002252
2253 /* Make extra sure authentication was performed, exactly once */
2254 if( auth_done != 1 )
2255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2257 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002258 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002259
2260 if( ssl->in_msglen == 0 )
2261 {
Angus Grattonb91cb6e2018-06-19 15:58:22 +10002262#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2263 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2264 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2265 {
2266 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2268 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2269 }
2270#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2271
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 ssl->nb_zero++;
2273
2274 /*
2275 * Three or more empty messages may be a DoS attack
2276 * (excessive CPU consumption).
2277 */
2278 if( ssl->nb_zero > 3 )
2279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002283 }
2284 }
2285 else
2286 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002289 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002290 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002291 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002292 }
2293 else
2294#endif
2295 {
2296 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2297 if( ++ssl->in_ctr[i - 1] != 0 )
2298 break;
2299
2300 /* The loop goes to its end iff the counter is wrapping */
2301 if( i == ssl_ep_len( ssl ) )
2302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2304 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002305 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002306 }
2307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002309
2310 return( 0 );
2311}
2312
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002313#undef MAC_NONE
2314#undef MAC_PLAINTEXT
2315#undef MAC_CIPHERTEXT
2316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002318/*
2319 * Compression/decompression functions
2320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002322{
2323 int ret;
2324 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002325 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002326 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002327 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002330
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002331 if( len_pre == 0 )
2332 return( 0 );
2333
Paul Bakker2770fbd2012-07-03 13:30:23 +00002334 memcpy( msg_pre, ssl->out_msg, len_pre );
2335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002337 ssl->out_msglen ) );
2338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002340 ssl->out_msg, ssl->out_msglen );
2341
Paul Bakker48916f92012-09-16 19:57:18 +00002342 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2343 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2344 ssl->transform_out->ctx_deflate.next_out = msg_post;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002345 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002346
Paul Bakker48916f92012-09-16 19:57:18 +00002347 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002348 if( ret != Z_OK )
2349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2351 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002352 }
2353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 ssl->out_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002355 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002358 ssl->out_msglen ) );
2359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002361 ssl->out_msg, ssl->out_msglen );
2362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002364
2365 return( 0 );
2366}
2367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002369{
2370 int ret;
2371 unsigned char *msg_post = ssl->in_msg;
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002372 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002373 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002374 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002377
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002378 if( len_pre == 0 )
2379 return( 0 );
2380
Paul Bakker2770fbd2012-07-03 13:30:23 +00002381 memcpy( msg_pre, ssl->in_msg, len_pre );
2382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002384 ssl->in_msglen ) );
2385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002387 ssl->in_msg, ssl->in_msglen );
2388
Paul Bakker48916f92012-09-16 19:57:18 +00002389 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2390 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2391 ssl->transform_in->ctx_inflate.next_out = msg_post;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002392 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002393 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002394
Paul Bakker48916f92012-09-16 19:57:18 +00002395 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002396 if( ret != Z_OK )
2397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2399 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002400 }
2401
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002402 ssl->in_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002403 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002406 ssl->in_msglen ) );
2407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002409 ssl->in_msg, ssl->in_msglen );
2410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002412
2413 return( 0 );
2414}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2418static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420#if defined(MBEDTLS_SSL_PROTO_DTLS)
2421static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002422{
2423 /* If renegotiation is not enforced, retransmit until we would reach max
2424 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002425 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002426 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002427 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002428 unsigned char doublings = 1;
2429
2430 while( ratio != 0 )
2431 {
2432 ++doublings;
2433 ratio >>= 1;
2434 }
2435
2436 if( ++ssl->renego_records_seen > doublings )
2437 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002439 return( 0 );
2440 }
2441 }
2442
2443 return( ssl_write_hello_request( ssl ) );
2444}
2445#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002447
Paul Bakker5121ce52009-01-03 21:22:43 +00002448/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002449 * Fill the input message buffer by appending data to it.
2450 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002451 *
2452 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2453 * available (from this read and/or a previous one). Otherwise, an error code
2454 * is returned (possibly EOF or WANT_READ).
2455 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002456 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2457 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2458 * since we always read a whole datagram at once.
2459 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002460 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002461 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002464{
Paul Bakker23986e52011-04-24 08:57:21 +00002465 int ret;
2466 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002470 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002473 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002475 }
2476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477 if( nb_want > MBEDTLS_SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2480 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002481 }
2482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002484 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002486 uint32_t timeout;
2487
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002488 /* Just to be sure */
2489 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2490 {
2491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2492 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2493 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2494 }
2495
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002496 /*
2497 * The point is, we need to always read a full datagram at once, so we
2498 * sometimes read more then requested, and handle the additional data.
2499 * It could be the rest of the current record (while fetching the
2500 * header) and/or some other records in the same datagram.
2501 */
2502
2503 /*
2504 * Move to the next record in the already read datagram if applicable
2505 */
2506 if( ssl->next_record_offset != 0 )
2507 {
2508 if( ssl->in_left < ssl->next_record_offset )
2509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2511 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002512 }
2513
2514 ssl->in_left -= ssl->next_record_offset;
2515
2516 if( ssl->in_left != 0 )
2517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002519 ssl->next_record_offset ) );
2520 memmove( ssl->in_hdr,
2521 ssl->in_hdr + ssl->next_record_offset,
2522 ssl->in_left );
2523 }
2524
2525 ssl->next_record_offset = 0;
2526 }
2527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002530
2531 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002532 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002533 */
2534 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002537 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002538 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002539
2540 /*
Antonin Décimo8fd91562019-01-23 15:24:37 +01002541 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002542 * are not at the beginning of a new record, the caller did something
2543 * wrong.
2544 */
2545 if( ssl->in_left != 0 )
2546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002549 }
2550
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002551 /*
2552 * Don't even try to read if time's out already.
2553 * This avoids by-passing the timer when repeatedly receiving messages
2554 * that will end up being dropped.
2555 */
2556 if( ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002557 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002558 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 len = MBEDTLS_SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002563 timeout = ssl->handshake->retransmit_timeout;
2564 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002565 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002568
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002569 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002570 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2571 timeout );
2572 else
2573 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002576
2577 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002579 }
2580
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002581 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002584 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002587 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002588 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002591 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002592 }
2593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002597 return( ret );
2598 }
2599
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002600 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002601 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002603 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002605 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002606 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002609 return( ret );
2610 }
2611
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002612 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002613 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002615 }
2616
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 if( ret < 0 )
2618 return( ret );
2619
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002620 ssl->in_left = ret;
2621 }
2622 else
2623#endif
2624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002626 ssl->in_left, nb_want ) );
2627
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002628 while( ssl->in_left < nb_want )
2629 {
2630 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002631
2632 if( ssl_check_timer( ssl ) != 0 )
2633 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2634 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002635 {
2636 if( ssl->f_recv_timeout != NULL )
2637 {
2638 ret = ssl->f_recv_timeout( ssl->p_bio,
2639 ssl->in_hdr + ssl->in_left, len,
2640 ssl->conf->read_timeout );
2641 }
2642 else
2643 {
2644 ret = ssl->f_recv( ssl->p_bio,
2645 ssl->in_hdr + ssl->in_left, len );
2646 }
2647 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002650 ssl->in_left, nb_want ) );
2651 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002652
2653 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002655
2656 if( ret < 0 )
2657 return( ret );
2658
makise-homura329fe7e2020-08-24 18:39:56 +03002659 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad1603b11af862018-03-19 07:18:13 -07002660 {
Jaeden Amerob5f53b12018-04-03 12:09:45 +01002661 MBEDTLS_SSL_DEBUG_MSG( 1,
2662 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160329ed80f2018-04-02 07:34:26 -07002663 ret, (unsigned long)len ) );
mohammad1603b11af862018-03-19 07:18:13 -07002664 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2665 }
2666
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002667 ssl->in_left += ret;
2668 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 }
2670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
2673 return( 0 );
2674}
2675
2676/*
2677 * Flush any data not yet written
2678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002680{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002681 int ret;
2682 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002686 if( ssl->f_send == NULL )
2687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002689 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002691 }
2692
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002693 /* Avoid incrementing counter if data is flushed */
2694 if( ssl->out_left == 0 )
2695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002697 return( 0 );
2698 }
2699
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 while( ssl->out_left > 0 )
2701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2703 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) +
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002706 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002707 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
2711 if( ret <= 0 )
2712 return( ret );
2713
makise-homura329fe7e2020-08-24 18:39:56 +03002714 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16036085c722018-02-22 04:29:04 -08002715 {
Jaeden Amerob5f53b12018-04-03 12:09:45 +01002716 MBEDTLS_SSL_DEBUG_MSG( 1,
2717 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160329ed80f2018-04-02 07:34:26 -07002718 ret, (unsigned long)ssl->out_left ) );
mohammad16036085c722018-02-22 04:29:04 -08002719 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2720 }
2721
Paul Bakker5121ce52009-01-03 21:22:43 +00002722 ssl->out_left -= ret;
2723 }
2724
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002725 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002726 if( ++ssl->out_ctr[i - 1] != 0 )
2727 break;
2728
2729 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002730 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2733 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002734 }
2735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
2738 return( 0 );
2739}
2740
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002741/*
2742 * Functions to handle the DTLS retransmission state machine
2743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002745/*
2746 * Append current handshake message to current outgoing flight
2747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002749{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 mbedtls_ssl_flight_item *msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002751
2752 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002753 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002754 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002757 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002758 }
2759
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002760 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002761 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002764 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002765 }
2766
2767 /* Copy current handshake message with headers */
2768 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2769 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002770 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002771 msg->next = NULL;
2772
2773 /* Append to the current flight */
2774 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002775 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002776 else
2777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002779 while( cur->next != NULL )
2780 cur = cur->next;
2781 cur->next = msg;
2782 }
2783
2784 return( 0 );
2785}
2786
2787/*
2788 * Free the current flight of handshake messages
2789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002791{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 mbedtls_ssl_flight_item *cur = flight;
2793 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002794
2795 while( cur != NULL )
2796 {
2797 next = cur->next;
2798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 mbedtls_free( cur->p );
2800 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002801
2802 cur = next;
2803 }
2804}
2805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2807static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002808#endif
2809
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002810/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002811 * Swap transform_out and out_ctr with the alternative ones
2812 */
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002813static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002814{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002816 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002817#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2818 int ret;
2819#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002820
2821 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002824 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002825 }
2826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002828
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002829 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002830 tmp_transform = ssl->transform_out;
2831 ssl->transform_out = ssl->handshake->alt_transform_out;
2832 ssl->handshake->alt_transform_out = tmp_transform;
2833
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002834 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002835 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2836 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2837 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002838
2839 /* Adjust to the newly activated transform */
2840 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002842 {
2843 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2844 ssl->transform_out->fixed_ivlen;
2845 }
2846 else
2847 ssl->out_msg = ssl->out_iv;
2848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2850 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2855 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002856 }
2857 }
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002858#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
2859
2860 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002861}
2862
2863/*
2864 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002865 *
2866 * Need to remember the current message in case flush_output returns
2867 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002868 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002869 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002871{
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002872 int ret;
2873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002879
2880 ssl->handshake->cur_msg = ssl->handshake->flight;
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002881 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
2882 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002885 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002886
2887 while( ssl->handshake->cur_msg != NULL )
2888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 mbedtls_ssl_flight_item *cur = ssl->handshake->cur_msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002890
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002891 /* Swap epochs before sending Finished: we can't do it after
2892 * sending ChangeCipherSpec, in case write returns WANT_READ.
2893 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2895 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002896 {
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002897 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
2898 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002899 }
2900
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002901 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002902 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002903 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002904
2905 ssl->handshake->cur_msg = cur->next;
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002912 return( ret );
2913 }
2914 }
2915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2917 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002918 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002921 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2922 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002925
2926 return( 0 );
2927}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002928
2929/*
2930 * To be called when the last message of an incoming flight is received.
2931 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002933{
2934 /* We won't need to resend that one any more */
2935 ssl_flight_free( ssl->handshake->flight );
2936 ssl->handshake->flight = NULL;
2937 ssl->handshake->cur_msg = NULL;
2938
2939 /* The next incoming flight will start with this msg_seq */
2940 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2941
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002942 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002943 ssl_set_timer( ssl, 0 );
2944
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é-Gonnard5d8ba532014-09-19 15:09:21 +02002947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002949 }
2950 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002952}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002953
2954/*
2955 * To be called when the last message of an outgoing flight is send.
2956 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002958{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002959 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002960 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2963 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002966 }
2967 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002969}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002971
Paul Bakker5121ce52009-01-03 21:22:43 +00002972/*
2973 * Record layer functions
2974 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002975
2976/*
2977 * Write current record.
2978 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002981{
Nicholas Wilsonf0021642016-04-13 11:51:05 +01002982 int ret, done = 0, out_msg_type;
Paul Bakker23986e52011-04-24 08:57:21 +00002983 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002988 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002989 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002991 {
2992 ; /* Skip special handshake treatment when resending */
2993 }
2994 else
2995#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 {
Nicholas Wilsonf0021642016-04-13 11:51:05 +01002998 out_msg_type = ssl->out_msg[0];
2999
3000 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003001 ssl->handshake == NULL )
3002 {
3003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3004 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3005 }
3006
Paul Bakker5121ce52009-01-03 21:22:43 +00003007 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
3008 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
3009 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
3010
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003011 /*
3012 * DTLS has additional fields in the Handshake layer,
3013 * between the length field and the actual payload:
3014 * uint16 message_seq;
3015 * uint24 fragment_offset;
3016 * uint24 fragment_length;
3017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003019 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003020 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003021 /* Make room for the additional DTLS fields */
Hanno Becker9648f8b2017-09-18 10:55:54 +01003022 if( MBEDTLS_SSL_MAX_CONTENT_LEN - ssl->out_msglen < 8 )
3023 {
3024 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3025 "size %u, maximum %u",
3026 (unsigned) ( ssl->in_hslen - 4 ),
3027 (unsigned) ( MBEDTLS_SSL_MAX_CONTENT_LEN - 12 ) ) );
3028 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3029 }
3030
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003031 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003032 ssl->out_msglen += 8;
3033 len += 8;
3034
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003035 /* Write message_seq and update it, except for HelloRequest */
Nicholas Wilsonf0021642016-04-13 11:51:05 +01003036 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003037 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003038 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3039 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3040 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003041 }
3042 else
3043 {
3044 ssl->out_msg[4] = 0;
3045 ssl->out_msg[5] = 0;
3046 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003047
3048 /* We don't fragment, so frag_offset = 0 and frag_len = len */
3049 memset( ssl->out_msg + 6, 0x00, 3 );
3050 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003051 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003053
Nicholas Wilsonf0021642016-04-13 11:51:05 +01003054 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003055 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003056 }
3057
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003058 /* Save handshake and CCS messages for resending */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003060 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003061 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING &&
3063 ( ssl->out_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ||
3064 ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003065 {
3066 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003069 return( ret );
3070 }
3071 }
3072#endif
3073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003075 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003077 {
3078 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003081 return( ret );
3082 }
3083
3084 len = ssl->out_msglen;
3085 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3089 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093 ret = mbedtls_ssl_hw_record_write( ssl );
3094 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3097 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003098 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003099
3100 if( ret == 0 )
3101 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003102 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003104 if( !done )
3105 {
3106 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003108 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003109
3110 ssl->out_len[0] = (unsigned char)( len >> 8 );
3111 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003112
Paul Bakker48916f92012-09-16 19:57:18 +00003113 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003114 {
3115 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003118 return( ret );
3119 }
3120
3121 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003122 ssl->out_len[0] = (unsigned char)( len >> 8 );
3123 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003124 }
3125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00003127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00003129 "version = [%d:%d], msglen = %d",
3130 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003131 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3134 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003135 }
3136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003140 return( ret );
3141 }
3142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003144
3145 return( 0 );
3146}
3147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003149/*
3150 * Mark bits in bitmask (used for DTLS HS reassembly)
3151 */
3152static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3153{
3154 unsigned int start_bits, end_bits;
3155
3156 start_bits = 8 - ( offset % 8 );
3157 if( start_bits != 8 )
3158 {
3159 size_t first_byte_idx = offset / 8;
3160
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003161 /* Special case */
3162 if( len <= start_bits )
3163 {
3164 for( ; len != 0; len-- )
3165 mask[first_byte_idx] |= 1 << ( start_bits - len );
3166
3167 /* Avoid potential issues with offset or len becoming invalid */
3168 return;
3169 }
3170
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003171 offset += start_bits; /* Now offset % 8 == 0 */
3172 len -= start_bits;
3173
3174 for( ; start_bits != 0; start_bits-- )
3175 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3176 }
3177
3178 end_bits = len % 8;
3179 if( end_bits != 0 )
3180 {
3181 size_t last_byte_idx = ( offset + len ) / 8;
3182
3183 len -= end_bits; /* Now len % 8 == 0 */
3184
3185 for( ; end_bits != 0; end_bits-- )
3186 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3187 }
3188
3189 memset( mask + offset / 8, 0xFF, len / 8 );
3190}
3191
3192/*
3193 * Check that bitmask is full
3194 */
3195static int ssl_bitmask_check( unsigned char *mask, size_t len )
3196{
3197 size_t i;
3198
3199 for( i = 0; i < len / 8; i++ )
3200 if( mask[i] != 0xFF )
3201 return( -1 );
3202
3203 for( i = 0; i < len % 8; i++ )
3204 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3205 return( -1 );
3206
3207 return( 0 );
3208}
3209
3210/*
3211 * Reassemble fragmented DTLS handshake messages.
3212 *
3213 * Use a temporary buffer for reassembly, divided in two parts:
3214 * - the first holds the reassembled message (including handshake header),
3215 * - the second holds a bitmask indicating which parts of the message
3216 * (excluding headers) have been received so far.
3217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003219{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003220 unsigned char *msg, *bitmask;
3221 size_t frag_len, frag_off;
3222 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3223
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003224 if( ssl->handshake == NULL )
3225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3227 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003228 }
3229
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003230 /*
3231 * For first fragment, check size and allocate buffer
3232 */
3233 if( ssl->handshake->hs_msg == NULL )
3234 {
3235 size_t alloc_len;
3236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003238 msg_len ) );
3239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 if( ssl->in_hslen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3243 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003244 }
3245
3246 /* The bitmask needs one bit per byte of message excluding header */
3247 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3248
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003249 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003250 if( ssl->handshake->hs_msg == NULL )
3251 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003253 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003254 }
3255
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003256 /* Prepare final header: copy msg_type, length and message_seq,
3257 * then add standardised fragment_offset and fragment_length */
3258 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3259 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3260 memcpy( ssl->handshake->hs_msg + 9,
3261 ssl->handshake->hs_msg + 1, 3 );
3262 }
3263 else
3264 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003265 /* Make sure msg_type and length are consistent */
3266 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3269 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003270 }
3271 }
3272
3273 msg = ssl->handshake->hs_msg + 12;
3274 bitmask = msg + msg_len;
3275
3276 /*
3277 * Check and copy current fragment
3278 */
3279 frag_off = ( ssl->in_msg[6] << 16 ) |
3280 ( ssl->in_msg[7] << 8 ) |
3281 ssl->in_msg[8];
3282 frag_len = ( ssl->in_msg[9] << 16 ) |
3283 ( ssl->in_msg[10] << 8 ) |
3284 ssl->in_msg[11];
3285
3286 if( frag_off + frag_len > msg_len )
3287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003289 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003291 }
3292
3293 if( frag_len + 12 > ssl->in_msglen )
3294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003296 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003298 }
3299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003301 frag_off, frag_len ) );
3302
3303 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3304 ssl_bitmask_set( bitmask, frag_off, frag_len );
3305
3306 /*
3307 * Do we have the complete message by now?
3308 * If yes, finalize it, else ask to read the next record.
3309 */
3310 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003313 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003314 }
3315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003317
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003318 if( frag_len + 12 < ssl->in_msglen )
3319 {
3320 /*
3321 * We'got more handshake messages in the same record.
3322 * This case is not handled now because no know implementation does
3323 * that and it's hard to test, so we prefer to fail cleanly for now.
3324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3326 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003327 }
3328
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003329 if( ssl->in_left > ssl->next_record_offset )
3330 {
3331 /*
3332 * We've got more data in the buffer after the current record,
3333 * that we don't want to overwrite. Move it before writing the
3334 * reassembled message, and adjust in_left and next_record_offset.
3335 */
3336 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3337 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3338 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3339
3340 /* First compute and check new lengths */
3341 ssl->next_record_offset = new_remain - ssl->in_hdr;
3342 ssl->in_left = ssl->next_record_offset + remain_len;
3343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344 if( ssl->in_left > MBEDTLS_SSL_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003345 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3348 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003349 }
3350
3351 memmove( new_remain, cur_remain, remain_len );
3352 }
3353
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003354 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3355
Hanno Beckerd82e0c02018-10-15 13:22:22 +01003356 mbedtls_zeroize( ssl->handshake->hs_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003358 ssl->handshake->hs_msg = NULL;
3359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003361 ssl->in_msg, ssl->in_hslen );
3362
3363 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003366
Simon Butcher99000142016-10-13 17:21:01 +01003367int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003368{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003372 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003374 }
3375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003377 ( ssl->in_msg[1] << 16 ) |
3378 ( ssl->in_msg[2] << 8 ) |
3379 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003382 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003383 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003386 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003387 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003388 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003389 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003390
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003391 /* ssl->handshake is NULL when receiving ClientHello for renego */
3392 if( ssl->handshake != NULL &&
3393 recv_msg_seq != ssl->handshake->in_msg_seq )
3394 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003395 /* Retransmit only on last message from previous flight, to avoid
3396 * too many retransmissions.
3397 * Besides, No sane server ever retransmits HelloVerifyRequest */
3398 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003402 "message_seq = %d, start_of_flight = %d",
3403 recv_msg_seq,
3404 ssl->handshake->in_flight_start_seq ) );
3405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003409 return( ret );
3410 }
3411 }
3412 else
3413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003415 "message_seq = %d, expected = %d",
3416 recv_msg_seq,
3417 ssl->handshake->in_msg_seq ) );
3418 }
3419
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003420 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003421 }
3422 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003423
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003424 /* Reassemble if current message is fragmented or reassembly is
3425 * already in progress */
3426 if( ssl->in_msglen < ssl->in_hslen ||
3427 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3428 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3429 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003432
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003433 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003436 return( ret );
3437 }
3438 }
3439 }
3440 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003442 /* With TLS we don't handle fragmentation (for now) */
3443 if( ssl->in_msglen < ssl->in_hslen )
3444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3446 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003447 }
3448
Simon Butcher99000142016-10-13 17:21:01 +01003449 return( 0 );
3450}
3451
3452void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3453{
3454
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003455 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3456 ssl->handshake != NULL )
3457 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003458 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003459 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003460
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003461 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003463 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003464 ssl->handshake != NULL )
3465 {
3466 ssl->handshake->in_msg_seq++;
3467 }
3468#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003469}
3470
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003471/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003472 * DTLS anti-replay: RFC 6347 4.1.2.6
3473 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003474 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3475 * Bit n is set iff record number in_window_top - n has been seen.
3476 *
3477 * Usually, in_window_top is the last record number seen and the lsb of
3478 * in_window is set. The only exception is the initial state (record number 0
3479 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3482static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003483{
3484 ssl->in_window_top = 0;
3485 ssl->in_window = 0;
3486}
3487
3488static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3489{
3490 return( ( (uint64_t) buf[0] << 40 ) |
3491 ( (uint64_t) buf[1] << 32 ) |
3492 ( (uint64_t) buf[2] << 24 ) |
3493 ( (uint64_t) buf[3] << 16 ) |
3494 ( (uint64_t) buf[4] << 8 ) |
3495 ( (uint64_t) buf[5] ) );
3496}
3497
3498/*
3499 * Return 0 if sequence number is acceptable, -1 otherwise
3500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003502{
3503 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3504 uint64_t bit;
3505
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003506 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003507 return( 0 );
3508
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003509 if( rec_seqnum > ssl->in_window_top )
3510 return( 0 );
3511
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003512 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003513
3514 if( bit >= 64 )
3515 return( -1 );
3516
3517 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3518 return( -1 );
3519
3520 return( 0 );
3521}
3522
3523/*
3524 * Update replay window on new validated record
3525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003527{
3528 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3529
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003530 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003531 return;
3532
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003533 if( rec_seqnum > ssl->in_window_top )
3534 {
3535 /* Update window_top and the contents of the window */
3536 uint64_t shift = rec_seqnum - ssl->in_window_top;
3537
3538 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003539 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003540 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003541 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003542 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003543 ssl->in_window |= 1;
3544 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003545
3546 ssl->in_window_top = rec_seqnum;
3547 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003548 else
3549 {
3550 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003551 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003552
3553 if( bit < 64 ) /* Always true, but be extra sure */
3554 ssl->in_window |= (uint64_t) 1 << bit;
3555 }
3556}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003558
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003559#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003560/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003561static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3562
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003563/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003564 * Without any SSL context, check if a datagram looks like a ClientHello with
3565 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003566 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003567 *
3568 * - if cookie is valid, return 0
3569 * - if ClientHello looks superficially valid but cookie is not,
3570 * fill obuf and set olen, then
3571 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3572 * - otherwise return a specific error code
3573 */
3574static int ssl_check_dtls_clihlo_cookie(
3575 mbedtls_ssl_cookie_write_t *f_cookie_write,
3576 mbedtls_ssl_cookie_check_t *f_cookie_check,
3577 void *p_cookie,
3578 const unsigned char *cli_id, size_t cli_id_len,
3579 const unsigned char *in, size_t in_len,
3580 unsigned char *obuf, size_t buf_len, size_t *olen )
3581{
3582 size_t sid_len, cookie_len;
3583 unsigned char *p;
3584
3585 if( f_cookie_write == NULL || f_cookie_check == NULL )
3586 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3587
3588 /*
3589 * Structure of ClientHello with record and handshake headers,
3590 * and expected values. We don't need to check a lot, more checks will be
3591 * done when actually parsing the ClientHello - skipping those checks
3592 * avoids code duplication and does not make cookie forging any easier.
3593 *
3594 * 0-0 ContentType type; copied, must be handshake
3595 * 1-2 ProtocolVersion version; copied
3596 * 3-4 uint16 epoch; copied, must be 0
3597 * 5-10 uint48 sequence_number; copied
3598 * 11-12 uint16 length; (ignored)
3599 *
3600 * 13-13 HandshakeType msg_type; (ignored)
3601 * 14-16 uint24 length; (ignored)
3602 * 17-18 uint16 message_seq; copied
3603 * 19-21 uint24 fragment_offset; copied, must be 0
3604 * 22-24 uint24 fragment_length; (ignored)
3605 *
3606 * 25-26 ProtocolVersion client_version; (ignored)
3607 * 27-58 Random random; (ignored)
3608 * 59-xx SessionID session_id; 1 byte len + sid_len content
3609 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3610 * ...
3611 *
3612 * Minimum length is 61 bytes.
3613 */
3614 if( in_len < 61 ||
3615 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3616 in[3] != 0 || in[4] != 0 ||
3617 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3618 {
3619 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3620 }
3621
3622 sid_len = in[59];
3623 if( sid_len > in_len - 61 )
3624 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3625
3626 cookie_len = in[60 + sid_len];
3627 if( cookie_len > in_len - 60 )
3628 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3629
3630 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3631 cli_id, cli_id_len ) == 0 )
3632 {
3633 /* Valid cookie */
3634 return( 0 );
3635 }
3636
3637 /*
3638 * If we get here, we've got an invalid cookie, let's prepare HVR.
3639 *
3640 * 0-0 ContentType type; copied
3641 * 1-2 ProtocolVersion version; copied
3642 * 3-4 uint16 epoch; copied
3643 * 5-10 uint48 sequence_number; copied
3644 * 11-12 uint16 length; olen - 13
3645 *
3646 * 13-13 HandshakeType msg_type; hello_verify_request
3647 * 14-16 uint24 length; olen - 25
3648 * 17-18 uint16 message_seq; copied
3649 * 19-21 uint24 fragment_offset; copied
3650 * 22-24 uint24 fragment_length; olen - 25
3651 *
3652 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3653 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3654 *
3655 * Minimum length is 28.
3656 */
3657 if( buf_len < 28 )
3658 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3659
3660 /* Copy most fields and adapt others */
3661 memcpy( obuf, in, 25 );
3662 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3663 obuf[25] = 0xfe;
3664 obuf[26] = 0xff;
3665
3666 /* Generate and write actual cookie */
3667 p = obuf + 28;
3668 if( f_cookie_write( p_cookie,
3669 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3670 {
3671 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3672 }
3673
3674 *olen = p - obuf;
3675
3676 /* Go back and fill length fields */
3677 obuf[27] = (unsigned char)( *olen - 28 );
3678
3679 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3680 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3681 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3682
3683 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3684 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3685
3686 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3687}
3688
3689/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003690 * Handle possible client reconnect with the same UDP quadruplet
3691 * (RFC 6347 Section 4.2.8).
3692 *
3693 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3694 * that looks like a ClientHello.
3695 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003696 * - if the input looks like a ClientHello without cookies,
3697 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003698 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003699 * - if the input looks like a ClientHello with a valid cookie,
3700 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003701 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003702 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003703 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003704 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003705 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3706 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003707 */
3708static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3709{
3710 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003711 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003712
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003713 /* Use out_msg as temporary buffer for writing out HelloVerifyRequest,
3714 * because the output buffer's already around. Don't use out_buf though,
3715 * as we don't want to overwrite out_ctr. */
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003716 ret = ssl_check_dtls_clihlo_cookie(
3717 ssl->conf->f_cookie_write,
3718 ssl->conf->f_cookie_check,
3719 ssl->conf->p_cookie,
3720 ssl->cli_id, ssl->cli_id_len,
3721 ssl->in_buf, ssl->in_left,
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003722 ssl->out_msg, MBEDTLS_SSL_MAX_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003723
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003724 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3725
3726 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003727 {
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003728 int send_ret;
3729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3730 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003731 ssl->out_msg, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003732 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003733 * If the error is permanent we'll catch it later,
3734 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003735 send_ret = ssl->f_send( ssl->p_bio, ssl->out_msg, len );
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003736 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3737 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003738
3739 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003740 }
3741
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003742 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003743 {
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003745 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3746 {
3747 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3748 return( ret );
3749 }
3750
3751 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003752 }
3753
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003754 return( ret );
3755}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003756#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003757
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003758/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003759 * ContentType type;
3760 * ProtocolVersion version;
3761 * uint16 epoch; // DTLS only
3762 * uint48 sequence_number; // DTLS only
3763 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003764 *
3765 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003766 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003767 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3768 *
3769 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003770 * 1. proceed with the record if this function returns 0
3771 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3772 * 3. return CLIENT_RECONNECT if this function return that value
3773 * 4. drop the whole datagram if this function returns anything else.
3774 * Point 2 is needed when the peer is resending, and we have already received
3775 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003778{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003779 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003781 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 +02003782
Paul Bakker5121ce52009-01-03 21:22:43 +00003783 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003784 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003785 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003788 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003789 ssl->in_msgtype,
3790 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003791
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003792 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003793 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3794 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3795 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3796 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003799
3800#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01003801 /* Silently ignore invalid DTLS records as recommended by RFC 6347
3802 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003803 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3804#endif /* MBEDTLS_SSL_PROTO_DTLS */
3805 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3806 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
3807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003808 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003809 }
3810
3811 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003812 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3815 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003816 }
3817
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003818 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3821 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003822 }
3823
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003824 /* Check length against the size of our buffer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003825 if( ssl->in_msglen > MBEDTLS_SSL_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003826 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3829 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003830 }
3831
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003832 /*
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003833 * DTLS-related tests.
3834 * Check epoch before checking length constraint because
3835 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3836 * message gets duplicated before the corresponding Finished message,
3837 * the second ChangeCipherSpec should be discarded because it belongs
3838 * to an old epoch, but not because its length is shorter than
3839 * the minimum record length for packets using the new record transform.
3840 * Note that these two kinds of failures are handled differently,
3841 * as an unexpected record is silently skipped but an invalid
3842 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003843 */
3844#if defined(MBEDTLS_SSL_PROTO_DTLS)
3845 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3846 {
3847 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3848
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003849 /* Check epoch (and sequence number) with DTLS */
3850 if( rec_epoch != ssl->in_epoch )
3851 {
3852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3853 "expected %d, received %d",
3854 ssl->in_epoch, rec_epoch ) );
3855
3856#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3857 /*
3858 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3859 * access the first byte of record content (handshake type), as we
3860 * have an active transform (possibly iv_len != 0), so use the
3861 * fact that the record header len is 13 instead.
3862 */
3863 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3864 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3865 rec_epoch == 0 &&
3866 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3867 ssl->in_left > 13 &&
3868 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3869 {
3870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3871 "from the same port" ) );
3872 return( ssl_handle_possible_reconnect( ssl ) );
3873 }
3874 else
3875#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3876 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3877 }
3878
3879#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3880 /* Replay detection only works for the current epoch */
3881 if( rec_epoch == ssl->in_epoch &&
3882 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3883 {
3884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3885 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3886 }
3887#endif
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003888
3889 /* Drop unexpected ChangeCipherSpec messages */
3890 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3891 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3892 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3893 {
3894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3895 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3896 }
3897
3898 /* Drop unexpected ApplicationData records,
3899 * except at the beginning of renegotiations */
3900 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
3901 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
3902#if defined(MBEDTLS_SSL_RENEGOTIATION)
3903 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
3904 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
3905#endif
3906 )
3907 {
3908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3909 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3910 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003911 }
3912#endif /* MBEDTLS_SSL_PROTO_DTLS */
3913
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003914
3915 /* Check length against bounds of the current transform and version */
3916 if( ssl->transform_in == NULL )
3917 {
3918 if( ssl->in_msglen < 1 ||
3919 ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
3920 {
3921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3922 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3923 }
3924 }
3925 else
3926 {
3927 if( ssl->in_msglen < ssl->transform_in->minlen )
3928 {
3929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3930 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3931 }
3932
3933#if defined(MBEDTLS_SSL_PROTO_SSL3)
3934 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3935 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_MAX_CONTENT_LEN )
3936 {
3937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3938 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3939 }
3940#endif
3941#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3942 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3943 /*
3944 * TLS encrypted messages can have up to 256 bytes of padding
3945 */
3946 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
3947 ssl->in_msglen > ssl->transform_in->minlen +
3948 MBEDTLS_SSL_MAX_CONTENT_LEN + 256 )
3949 {
3950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3951 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3952 }
3953#endif
3954 }
3955
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003956 return( 0 );
3957}
Paul Bakker5121ce52009-01-03 21:22:43 +00003958
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003959/*
3960 * If applicable, decrypt (and decompress) record content
3961 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003963{
3964 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003966 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
3967 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3970 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003974 ret = mbedtls_ssl_hw_record_read( ssl );
3975 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3978 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003979 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003980
3981 if( ret == 0 )
3982 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003983 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003985 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003986 {
3987 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003990 return( ret );
3991 }
3992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00003994 ssl->in_msg, ssl->in_msglen );
3995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996 if( ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3999 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004000 }
4001 }
4002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004004 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004006 {
4007 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004010 return( ret );
4011 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004012 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004013#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004016 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004019 }
4020#endif
4021
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004022 return( 0 );
4023}
4024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004026
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004027/*
4028 * Read a record.
4029 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004030 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4031 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4032 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004033 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004034int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004035{
4036 int ret;
4037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004039
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004040 if( ssl->keep_current_message == 0 )
4041 {
4042 do {
Simon Butcher99000142016-10-13 17:21:01 +01004043
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004044 if( ( ret = mbedtls_ssl_read_record_layer( ssl ) ) != 0 )
4045 {
4046 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4047 return( ret );
4048 }
4049
4050 ret = mbedtls_ssl_handle_message_type( ssl );
4051
4052 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret );
4053
4054 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004055 {
4056 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4057 return( ret );
4058 }
4059
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004060 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4061 {
4062 mbedtls_ssl_update_handshake_status( ssl );
4063 }
Simon Butcher99000142016-10-13 17:21:01 +01004064 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004065 else
Simon Butcher99000142016-10-13 17:21:01 +01004066 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4068 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004069 }
4070
4071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4072
4073 return( 0 );
4074}
4075
4076int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4077{
4078 int ret;
4079
Hanno Becker4a810fb2017-05-24 16:27:30 +01004080 /*
4081 * Step A
4082 *
4083 * Consume last content-layer message and potentially
4084 * update in_msglen which keeps track of the contents'
4085 * consumption state.
4086 *
4087 * (1) Handshake messages:
4088 * Remove last handshake message, move content
4089 * and adapt in_msglen.
4090 *
4091 * (2) Alert messages:
4092 * Consume whole record content, in_msglen = 0.
4093 *
4094 * NOTE: This needs to be fixed, since like for
4095 * handshake messages it is allowed to have
4096 * multiple alerts witin a single record.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004097 * Internal reference IOTSSL-1321.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004098 *
4099 * (3) Change cipher spec:
4100 * Consume whole record content, in_msglen = 0.
4101 *
4102 * (4) Application data:
4103 * Don't do anything - the record layer provides
4104 * the application data as a stream transport
4105 * and consumes through mbedtls_ssl_read only.
4106 *
4107 */
4108
4109 /* Case (1): Handshake messages */
4110 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004111 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004112 /* Hard assertion to be sure that no application data
4113 * is in flight, as corrupting ssl->in_msglen during
4114 * ssl->in_offt != NULL is fatal. */
4115 if( ssl->in_offt != NULL )
4116 {
4117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4118 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4119 }
4120
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004121 /*
4122 * Get next Handshake message in the current record
4123 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004124
Hanno Becker4a810fb2017-05-24 16:27:30 +01004125 /* Notes:
4126 * (1) in_hslen is *NOT* necessarily the size of the
4127 * current handshake content: If DTLS handshake
4128 * fragmentation is used, that's the fragment
4129 * size instead. Using the total handshake message
4130 * size here is FAULTY and should be changed at
4131 * some point. Internal reference IOTSSL-1414.
4132 * (2) While it doesn't seem to cause problems, one
4133 * has to be very careful not to assume that in_hslen
4134 * is always <= in_msglen in a sensible communication.
4135 * Again, it's wrong for DTLS handshake fragmentation.
4136 * The following check is therefore mandatory, and
4137 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004138 * Additionally, ssl->in_hslen might be arbitrarily out of
4139 * bounds after handling a DTLS message with an unexpected
4140 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004141 */
4142 if( ssl->in_hslen < ssl->in_msglen )
4143 {
4144 ssl->in_msglen -= ssl->in_hslen;
4145 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4146 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004147
Hanno Becker4a810fb2017-05-24 16:27:30 +01004148 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4149 ssl->in_msg, ssl->in_msglen );
4150 }
4151 else
4152 {
4153 ssl->in_msglen = 0;
4154 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004155
Hanno Becker4a810fb2017-05-24 16:27:30 +01004156 ssl->in_hslen = 0;
4157 }
4158 /* Case (4): Application data */
4159 else if( ssl->in_offt != NULL )
4160 {
4161 return( 0 );
4162 }
4163 /* Everything else (CCS & Alerts) */
4164 else
4165 {
4166 ssl->in_msglen = 0;
4167 }
4168
4169 /*
4170 * Step B
4171 *
4172 * Fetch and decode new record if current one is fully consumed.
4173 *
4174 */
4175
4176 if( ssl->in_msglen > 0 )
4177 {
4178 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004179 return( 0 );
4180 }
4181
Hanno Becker4a810fb2017-05-24 16:27:30 +01004182 /* Need to fetch a new record */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004183
Simon Butcher99000142016-10-13 17:21:01 +01004184#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004185read_record_header:
Simon Butcher99000142016-10-13 17:21:01 +01004186#endif
4187
Hanno Becker4a810fb2017-05-24 16:27:30 +01004188 /* Current record either fully processed or to be discarded. */
4189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004193 return( ret );
4194 }
4195
4196 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004199 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4200 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004201 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004202 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4203 {
4204 /* Skip unexpected record (but not whole datagram) */
4205 ssl->next_record_offset = ssl->in_msglen
4206 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004207
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4209 "(header)" ) );
4210 }
4211 else
4212 {
4213 /* Skip invalid record and the rest of the datagram */
4214 ssl->next_record_offset = 0;
4215 ssl->in_left = 0;
4216
4217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4218 "(header)" ) );
4219 }
4220
4221 /* Get next record */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004222 goto read_record_header;
4223 }
4224#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004225 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004226 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004227
4228 /*
4229 * Read and optionally decrypt the message contents
4230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004231 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4232 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004235 return( ret );
4236 }
4237
4238 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004240 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004241 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004242 else
4243#endif
4244 ssl->in_left = 0;
4245
4246 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004249 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004250 {
4251 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4253 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004254 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004255 /* Except when waiting for Finished as a bad mac here
4256 * probably means something went wrong in the handshake
4257 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4258 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4259 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4260 {
4261#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4262 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4263 {
4264 mbedtls_ssl_send_alert_message( ssl,
4265 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4266 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4267 }
4268#endif
4269 return( ret );
4270 }
4271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004272#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004273 if( ssl->conf->badmac_limit != 0 &&
4274 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4277 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004278 }
4279#endif
4280
Hanno Becker4a810fb2017-05-24 16:27:30 +01004281 /* As above, invalid records cause
4282 * dismissal of the whole datagram. */
4283
4284 ssl->next_record_offset = 0;
4285 ssl->in_left = 0;
4286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004288 goto read_record_header;
4289 }
4290
4291 return( ret );
4292 }
4293 else
4294#endif
4295 {
4296 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4298 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004300 mbedtls_ssl_send_alert_message( ssl,
4301 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4302 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004303 }
4304#endif
4305 return( ret );
4306 }
4307 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004308
4309 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004310 * When we sent the last flight of the handshake, we MUST respond to a
4311 * retransmit of the peer's previous flight with a retransmit. (In
4312 * practice, only the Finished message will make it, other messages
4313 * including CCS use the old transform so they're dropped as invalid.)
4314 *
4315 * If the record we received is not a handshake message, however, it
4316 * means the peer received our last flight so we can clean up
4317 * handshake info.
4318 *
4319 * This check needs to be done before prepare_handshake() due to an edge
4320 * case: if the client immediately requests renegotiation, this
4321 * finishes the current handshake first, avoiding the new ClientHello
4322 * being mistaken for an ancient message in the current handshake.
4323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004324#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004325 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004326 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004327 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4330 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004337 return( ret );
4338 }
4339
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01004340 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004341 }
4342 else
4343 {
4344 ssl_handshake_wrapup_free_hs_transform( ssl );
4345 }
4346 }
4347#endif
4348
Simon Butcher99000142016-10-13 17:21:01 +01004349 return( 0 );
4350}
4351
4352int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4353{
4354 int ret;
4355
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004356 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004357 * Handle particular types of records
4358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004359 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004360 {
Simon Butcher99000142016-10-13 17:21:01 +01004361 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4362 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004363 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004364 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004365 }
4366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004367 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004368 {
Angus Gratton8946b0d2018-06-20 15:43:50 +10004369 if( ssl->in_msglen != 2 )
4370 {
4371 /* Note: Standard allows for more than one 2 byte alert
4372 to be packed in a single message, but Mbed TLS doesn't
4373 currently support this. */
4374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4375 ssl->in_msglen ) );
4376 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4377 }
4378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004380 ssl->in_msg[0], ssl->in_msg[1] ) );
4381
4382 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004383 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004385 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004388 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004389 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004390 }
4391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004392 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4393 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4396 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004397 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004398
4399#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4400 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4401 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4402 {
4403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4404 /* Will be handled when trying to parse ServerHello */
4405 return( 0 );
4406 }
4407#endif
4408
4409#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4410 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4411 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4412 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4413 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4414 {
4415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4416 /* Will be handled in mbedtls_ssl_parse_certificate() */
4417 return( 0 );
4418 }
4419#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4420
4421 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004422 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004423 }
4424
Paul Bakker5121ce52009-01-03 21:22:43 +00004425 return( 0 );
4426}
4427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004428int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004429{
4430 int ret;
4431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4433 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4434 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004435 {
4436 return( ret );
4437 }
4438
4439 return( 0 );
4440}
4441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004443 unsigned char level,
4444 unsigned char message )
4445{
4446 int ret;
4447
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004448 if( ssl == NULL || ssl->conf == NULL )
4449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004452 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004455 ssl->out_msglen = 2;
4456 ssl->out_msg[0] = level;
4457 ssl->out_msg[1] = message;
4458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004462 return( ret );
4463 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004465
4466 return( 0 );
4467}
4468
Paul Bakker5121ce52009-01-03 21:22:43 +00004469/*
4470 * Handshake functions
4471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004472#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4473 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4474 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4475 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4476 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4477 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4478 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004479/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004480int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004481{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004486 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4487 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004488 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4489 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004492 ssl->state++;
4493 return( 0 );
4494 }
4495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4497 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004498}
4499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004500int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004501{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004502 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4507 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004508 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4509 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004512 ssl->state++;
4513 return( 0 );
4514 }
4515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4517 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004518}
Gilles Peskinef9828522017-05-03 12:28:43 +02004519
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004520#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004521/* Some certificate support -> implement write and parse */
4522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004524{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004525 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004526 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004527 const mbedtls_x509_crt *crt;
4528 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4533 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004534 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4535 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004538 ssl->state++;
4539 return( 0 );
4540 }
4541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004543 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004544 {
4545 if( ssl->client_auth == 0 )
4546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004548 ssl->state++;
4549 return( 0 );
4550 }
4551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004553 /*
4554 * If using SSLv3 and got no cert, send an Alert message
4555 * (otherwise an empty Certificate message will be sent).
4556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004557 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4558 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004559 {
4560 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4562 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4563 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004566 goto write_msg;
4567 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004569 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570#endif /* MBEDTLS_SSL_CLI_C */
4571#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004572 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4577 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004578 }
4579 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004580#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004582 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004583
4584 /*
4585 * 0 . 0 handshake type
4586 * 1 . 3 handshake length
4587 * 4 . 6 length of all certs
4588 * 7 . 9 length of cert. 1
4589 * 10 . n-1 peer certificate
4590 * n . n+2 length of cert. 2
4591 * n+3 . ... upper level cert, etc.
4592 */
4593 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004594 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004595
Paul Bakker29087132010-03-21 21:03:34 +00004596 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004597 {
4598 n = crt->raw.len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599 if( n > MBEDTLS_SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
4602 i + 3 + n, MBEDTLS_SSL_MAX_CONTENT_LEN ) );
4603 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004604 }
4605
4606 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4607 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4608 ssl->out_msg[i + 2] = (unsigned char)( n );
4609
4610 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4611 i += n; crt = crt->next;
4612 }
4613
4614 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4615 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4616 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4617
4618 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004619 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4620 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004621
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004622#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004623write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004624#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004625
4626 ssl->state++;
4627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004628 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004630 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004631 return( ret );
4632 }
4633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004635
Paul Bakkered27a042013-04-18 22:46:23 +02004636 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004637}
4638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004641 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004642 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004643 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004644 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004645 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4650 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004651 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4652 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004655 ssl->state++;
4656 return( 0 );
4657 }
4658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004660 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004661 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4662 {
4663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4664 ssl->state++;
4665 return( 0 );
4666 }
4667
4668#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4669 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4670 authmode = ssl->handshake->sni_authmode;
4671#endif
4672
4673 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4674 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004675 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004676 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004678 ssl->state++;
4679 return( 0 );
4680 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004681#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004683 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004684 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004685 /* mbedtls_ssl_read_record may have sent an alert already. We
4686 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004687 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004688 return( ret );
4689 }
4690
4691 ssl->state++;
4692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004693#if defined(MBEDTLS_SSL_SRV_C)
4694#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004695 /*
4696 * Check if the client sent an empty certificate
4697 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004698 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004699 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004700 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004701 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004702 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4703 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4704 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004707
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004708 /* The client was asked for a certificate but didn't send
4709 one. The client should know what's going on, so we
4710 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004711 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004712 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004713 return( 0 );
4714 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004715 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004716 }
4717 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004718#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004720#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4721 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004722 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004725 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4726 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4727 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4728 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004731
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004732 /* The client was asked for a certificate but didn't send
4733 one. The client should know what's going on, so we
4734 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004735 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004736 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004737 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004738 else
4739 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004740 }
4741 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4743 MBEDTLS_SSL_PROTO_TLS1_2 */
4744#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004746 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004749 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4750 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004752 }
4753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004754 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4755 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004758 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4759 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004761 }
4762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004764
Paul Bakker5121ce52009-01-03 21:22:43 +00004765 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004766 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004767 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004768 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004769
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004770 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004771 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004774 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4775 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004776 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004777 }
4778
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004779 /* In case we tried to reuse a session but it failed */
4780 if( ssl->session_negotiate->peer_cert != NULL )
4781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4783 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004784 }
4785
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004786 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004788 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004791 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4792 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004793 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004794 }
4795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004797
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004798 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004799
4800 while( i < ssl->in_hslen )
4801 {
Philippe Antoine33e5c322018-07-09 10:39:02 +02004802 if ( i + 3 > ssl->in_hslen ) {
4803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4804 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4805 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4806 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
4807 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004808 if( ssl->in_msg[i] != 0 )
4809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004811 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4812 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004814 }
4815
4816 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4817 | (unsigned int) ssl->in_msg[i + 2];
4818 i += 3;
4819
4820 if( n < 128 || i + n > ssl->in_hslen )
4821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004823 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4824 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004825 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004826 }
4827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004829 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004830 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004831 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004832 case 0: /*ok*/
4833 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
4834 /* Ignore certificate with an unknown algorithm: maybe a
4835 prior certificate was already trusted. */
4836 break;
4837
4838 case MBEDTLS_ERR_X509_ALLOC_FAILED:
4839 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
4840 goto crt_parse_der_failed;
4841
4842 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
4843 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4844 goto crt_parse_der_failed;
4845
4846 default:
4847 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4848 crt_parse_der_failed:
4849 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004851 return( ret );
4852 }
4853
4854 i += n;
4855 }
4856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004858
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004859 /*
4860 * On client, make sure the server cert doesn't change during renego to
4861 * avoid "triple handshake" attack: https://secure-resumption.com/
4862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004864 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004865 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004866 {
4867 if( ssl->session->peer_cert == NULL )
4868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004870 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4871 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004872 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004873 }
4874
4875 if( ssl->session->peer_cert->raw.len !=
4876 ssl->session_negotiate->peer_cert->raw.len ||
4877 memcmp( ssl->session->peer_cert->raw.p,
4878 ssl->session_negotiate->peer_cert->raw.p,
4879 ssl->session->peer_cert->raw.len ) != 0 )
4880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004882 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4883 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004884 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004885 }
4886 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004887#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004888
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004889 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004890 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004891 mbedtls_x509_crt *ca_chain;
4892 mbedtls_x509_crl *ca_crl;
4893
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004894#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004895 if( ssl->handshake->sni_ca_chain != NULL )
4896 {
4897 ca_chain = ssl->handshake->sni_ca_chain;
4898 ca_crl = ssl->handshake->sni_ca_crl;
4899 }
4900 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004901#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004902 {
4903 ca_chain = ssl->conf->ca_chain;
4904 ca_crl = ssl->conf->ca_crl;
4905 }
4906
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004907 /*
4908 * Main check: verify certificate
4909 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004910 ret = mbedtls_x509_crt_verify_with_profile(
4911 ssl->session_negotiate->peer_cert,
4912 ca_chain, ca_crl,
4913 ssl->conf->cert_profile,
4914 ssl->hostname,
4915 &ssl->session_negotiate->verify_result,
4916 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004917
4918 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004921 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004922
4923 /*
4924 * Secondary checks: always done, but change 'ret' only if it was 0
4925 */
4926
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004927#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004929 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004930
4931 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004933 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004934 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004935 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
4936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004938 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004940 }
4941 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004942#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004945 ciphersuite_info,
4946 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004947 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004950 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004951 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004952 }
4953
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004954 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
4955 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
4956 * with details encoded in the verification flags. All other kinds
4957 * of error codes, including those from the user provided f_vrfy
4958 * functions, are treated as fatal and lead to a failure of
4959 * ssl_parse_certificate even if verification was optional. */
4960 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
4961 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
4962 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
4963 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004964 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004965 }
4966
4967 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
4968 {
4969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
4970 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
4971 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004972
4973 if( ret != 0 )
4974 {
4975 /* The certificate may have been rejected for several reasons.
4976 Pick one and send the corresponding alert. Which alert to send
4977 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004978 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
4979 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
4980 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
4981 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4982 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
4983 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4984 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
4985 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4986 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
4987 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4988 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
4989 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4990 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
4991 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4992 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
4993 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
4994 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
4995 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
4996 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
4997 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02004998 else
4999 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005000 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5001 alert );
5002 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005003
Hanno Beckere6706e62017-05-15 16:05:15 +01005004#if defined(MBEDTLS_DEBUG_C)
5005 if( ssl->session_negotiate->verify_result != 0 )
5006 {
5007 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5008 ssl->session_negotiate->verify_result ) );
5009 }
5010 else
5011 {
5012 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5013 }
5014#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005015 }
5016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005018
5019 return( ret );
5020}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005021#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5022 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5023 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5024 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5025 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5026 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5027 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005029int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005030{
5031 int ret;
5032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005035 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005036 ssl->out_msglen = 1;
5037 ssl->out_msg[0] = 1;
5038
Paul Bakker5121ce52009-01-03 21:22:43 +00005039 ssl->state++;
5040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005041 if( ( ret = mbedtls_ssl_write_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_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005044 return( ret );
5045 }
5046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005048
5049 return( 0 );
5050}
5051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005052int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005053{
5054 int ret;
5055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005058 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005061 return( ret );
5062 }
5063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005064 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005067 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5068 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005069 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005070 }
5071
5072 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005075 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5076 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005078 }
5079
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005080 /*
5081 * Switch to our negotiated transform and session parameters for inbound
5082 * data.
5083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005085 ssl->transform_in = ssl->transform_negotiate;
5086 ssl->session_in = ssl->session_negotiate;
5087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005088#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005089 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005091#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005092 ssl_dtls_replay_reset( ssl );
5093#endif
5094
5095 /* Increment epoch */
5096 if( ++ssl->in_epoch == 0 )
5097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005099 /* This is highly unlikely to happen for legitimate reasons, so
5100 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005101 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005102 }
5103 }
5104 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005105#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005106 memset( ssl->in_ctr, 0, 8 );
5107
5108 /*
5109 * Set the in_msg pointer to the correct location based on IV length
5110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005111 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005112 {
5113 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
5114 ssl->transform_negotiate->fixed_ivlen;
5115 }
5116 else
5117 ssl->in_msg = ssl->in_iv;
5118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005119#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5120 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005122 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005124 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005125 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5126 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005127 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005128 }
5129 }
5130#endif
5131
Paul Bakker5121ce52009-01-03 21:22:43 +00005132 ssl->state++;
5133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005135
5136 return( 0 );
5137}
5138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005139void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5140 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005141{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005142 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005144#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5145 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5146 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005147 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005148 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005149#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005150#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5151#if defined(MBEDTLS_SHA512_C)
5152 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005153 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5154 else
5155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005156#if defined(MBEDTLS_SHA256_C)
5157 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005158 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005159 else
5160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005161#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005164 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005165 }
Paul Bakker380da532012-04-18 16:10:25 +00005166}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005168void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005169{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005170#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5171 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005172 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5173 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005174#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005175#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5176#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005177 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005178#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005179#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005180 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005181#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005182#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005183}
5184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005185static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005186 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005188#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5189 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005190 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5191 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005193#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5194#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005195 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005196#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005197#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005198 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005199#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005201}
5202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005203#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5204 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5205static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005206 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005207{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005208 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5209 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005210}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005211#endif
Paul Bakker380da532012-04-18 16:10:25 +00005212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005213#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5214#if defined(MBEDTLS_SHA256_C)
5215static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005216 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005217{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005218 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005219}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005220#endif
Paul Bakker380da532012-04-18 16:10:25 +00005221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005222#if defined(MBEDTLS_SHA512_C)
5223static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005224 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005225{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005226 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005227}
Paul Bakker769075d2012-11-24 11:26:46 +01005228#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005231#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005232static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005233 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005234{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005235 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005236 mbedtls_md5_context md5;
5237 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005238
Paul Bakker5121ce52009-01-03 21:22:43 +00005239 unsigned char padbuf[48];
5240 unsigned char md5sum[16];
5241 unsigned char sha1sum[20];
5242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005243 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005244 if( !session )
5245 session = ssl->session;
5246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005248
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005249 mbedtls_md5_init( &md5 );
5250 mbedtls_sha1_init( &sha1 );
5251
5252 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5253 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005254
5255 /*
5256 * SSLv3:
5257 * hash =
5258 * MD5( master + pad2 +
5259 * MD5( handshake + sender + master + pad1 ) )
5260 * + SHA1( master + pad2 +
5261 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005262 */
5263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005265 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5266 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005267#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005269#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005270 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5271 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005272#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005275 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005276
Paul Bakker1ef83d62012-04-11 12:09:53 +00005277 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005278
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005279 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5280 mbedtls_md5_update_ret( &md5, session->master, 48 );
5281 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5282 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005283
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005284 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5285 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5286 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5287 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005288
Paul Bakker1ef83d62012-04-11 12:09:53 +00005289 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005290
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005291 mbedtls_md5_starts_ret( &md5 );
5292 mbedtls_md5_update_ret( &md5, session->master, 48 );
5293 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5294 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5295 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005296
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005297 mbedtls_sha1_starts_ret( &sha1 );
5298 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5299 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5300 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5301 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005304
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005305 mbedtls_md5_free( &md5 );
5306 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
5309 mbedtls_zeroize( md5sum, sizeof( md5sum ) );
5310 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005313}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005316#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005317static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005318 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005319{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005320 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005321 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005322 mbedtls_md5_context md5;
5323 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005324 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005327 if( !session )
5328 session = ssl->session;
5329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005331
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005332 mbedtls_md5_init( &md5 );
5333 mbedtls_sha1_init( &sha1 );
5334
5335 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5336 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005337
Paul Bakker1ef83d62012-04-11 12:09:53 +00005338 /*
5339 * TLSv1:
5340 * hash = PRF( master, finished_label,
5341 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5342 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005345 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5346 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005347#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005350 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5351 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005352#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005354 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005355 ? "client finished"
5356 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005357
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005358 mbedtls_md5_finish_ret( &md5, padbuf );
5359 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005360
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005361 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005362 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005365
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005366 mbedtls_md5_free( &md5 );
5367 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005372}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005375#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5376#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005377static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005379{
5380 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005381 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005382 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005383 unsigned char padbuf[32];
5384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005386 if( !session )
5387 session = ssl->session;
5388
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005389 mbedtls_sha256_init( &sha256 );
5390
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005392
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005393 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005394
5395 /*
5396 * TLSv1.2:
5397 * hash = PRF( master, finished_label,
5398 * Hash( handshake ) )[0.11]
5399 */
5400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401#if !defined(MBEDTLS_SHA256_ALT)
5402 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005403 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005404#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005407 ? "client finished"
5408 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005409
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005410 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005411
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005412 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005413 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005416
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005417 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005422}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005423#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005425#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005426static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005428{
5429 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005430 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005431 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005432 unsigned char padbuf[48];
5433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005434 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005435 if( !session )
5436 session = ssl->session;
5437
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005438 mbedtls_sha512_init( &sha512 );
5439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005441
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005442 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005443
5444 /*
5445 * TLSv1.2:
5446 * hash = PRF( master, finished_label,
5447 * Hash( handshake ) )[0.11]
5448 */
5449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005450#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005451 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5452 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005453#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005456 ? "client finished"
5457 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005458
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005459 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005460
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005461 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005462 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005465
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005466 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005471}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472#endif /* MBEDTLS_SHA512_C */
5473#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005476{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005478
5479 /*
5480 * Free our handshake params
5481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482 mbedtls_ssl_handshake_free( ssl->handshake );
5483 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005484 ssl->handshake = NULL;
5485
5486 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005487 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005488 */
5489 if( ssl->transform )
5490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005491 mbedtls_ssl_transform_free( ssl->transform );
5492 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005493 }
5494 ssl->transform = ssl->transform_negotiate;
5495 ssl->transform_negotiate = NULL;
5496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005498}
5499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005500void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005501{
5502 int resume = ssl->handshake->resume;
5503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506#if defined(MBEDTLS_SSL_RENEGOTIATION)
5507 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005510 ssl->renego_records_seen = 0;
5511 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005512#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005513
5514 /*
5515 * Free the previous session and switch in the current one
5516 */
Paul Bakker0a597072012-09-25 21:55:46 +00005517 if( ssl->session )
5518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005519#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005520 /* RFC 7366 3.1: keep the EtM state */
5521 ssl->session_negotiate->encrypt_then_mac =
5522 ssl->session->encrypt_then_mac;
5523#endif
5524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005525 mbedtls_ssl_session_free( ssl->session );
5526 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005527 }
5528 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005529 ssl->session_negotiate = NULL;
5530
Paul Bakker0a597072012-09-25 21:55:46 +00005531 /*
5532 * Add cache entry
5533 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005534 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005535 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005536 resume == 0 )
5537 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005538 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005540 }
Paul Bakker0a597072012-09-25 21:55:46 +00005541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005543 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005544 ssl->handshake->flight != NULL )
5545 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005546 /* Cancel handshake timer */
5547 ssl_set_timer( ssl, 0 );
5548
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005549 /* Keep last flight around in case we need to resend it:
5550 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005551 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005552 }
5553 else
5554#endif
5555 ssl_handshake_wrapup_free_hs_transform( ssl );
5556
Paul Bakker48916f92012-09-16 19:57:18 +00005557 ssl->state++;
5558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005559 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005560}
5561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005562int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005563{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005564 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005567
Paul Bakker92be97b2013-01-02 17:30:03 +01005568 /*
5569 * Set the out_msg pointer to the correct location based on IV length
5570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005572 {
5573 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5574 ssl->transform_negotiate->fixed_ivlen;
5575 }
5576 else
5577 ssl->out_msg = ssl->out_iv;
5578
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005579 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005580
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005581 /*
5582 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5583 * may define some other value. Currently (early 2016), no defined
5584 * ciphersuite does this (and this is unlikely to change as activity has
5585 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005587 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005590 ssl->verify_data_len = hash_len;
5591 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005592#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005593
Paul Bakker5121ce52009-01-03 21:22:43 +00005594 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5596 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005597
5598 /*
5599 * In case of session resuming, invert the client and server
5600 * ChangeCipherSpec messages order.
5601 */
Paul Bakker0a597072012-09-25 21:55:46 +00005602 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005605 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005606 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005607#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005609 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005611#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005612 }
5613 else
5614 ssl->state++;
5615
Paul Bakker48916f92012-09-16 19:57:18 +00005616 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005617 * Switch to our negotiated transform and session parameters for outbound
5618 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005623 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005624 {
5625 unsigned char i;
5626
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005627 /* Remember current epoch settings for resending */
5628 ssl->handshake->alt_transform_out = ssl->transform_out;
5629 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5630
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005631 /* Set sequence_number to zero */
5632 memset( ssl->out_ctr + 2, 0, 6 );
5633
5634 /* Increment epoch */
5635 for( i = 2; i > 0; i-- )
5636 if( ++ssl->out_ctr[i - 1] != 0 )
5637 break;
5638
5639 /* The loop goes to its end iff the counter is wrapping */
5640 if( i == 0 )
5641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5643 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005644 }
5645 }
5646 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005647#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005648 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005649
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005650 ssl->transform_out = ssl->transform_negotiate;
5651 ssl->session_out = ssl->session_negotiate;
5652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005653#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5654 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005656 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5659 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005660 }
5661 }
5662#endif
5663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005665 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005666 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005667#endif
5668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005669 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005672 return( ret );
5673 }
5674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005676
5677 return( 0 );
5678}
5679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005680#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005681#define SSL_MAX_HASH_LEN 36
5682#else
5683#define SSL_MAX_HASH_LEN 12
5684#endif
5685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005686int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005687{
Paul Bakker23986e52011-04-24 08:57:21 +00005688 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005689 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005690 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005693
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005694 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005696 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005698 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005699 return( ret );
5700 }
5701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
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_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005708 }
5709
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005710 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005711#if defined(MBEDTLS_SSL_PROTO_SSL3)
5712 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005713 hash_len = 36;
5714 else
5715#endif
5716 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005718 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5719 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005722 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5723 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005724 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005725 }
5726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005728 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005731 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5732 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005734 }
5735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005736#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005737 ssl->verify_data_len = hash_len;
5738 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005739#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005740
Paul Bakker0a597072012-09-25 21:55:46 +00005741 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005744 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005745 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005746#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005747#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005748 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005750#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005751 }
5752 else
5753 ssl->state++;
5754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005756 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005757 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005758#endif
5759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005761
5762 return( 0 );
5763}
5764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005765static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005767 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5770 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5771 mbedtls_md5_init( &handshake->fin_md5 );
5772 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005773 mbedtls_md5_starts_ret( &handshake->fin_md5 );
5774 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005775#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005776#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5777#if defined(MBEDTLS_SHA256_C)
5778 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005779 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005780#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005781#if defined(MBEDTLS_SHA512_C)
5782 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005783 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005784#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005786
5787 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005788
5789#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5790 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5791 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5792#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005794#if defined(MBEDTLS_DHM_C)
5795 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005796#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005797#if defined(MBEDTLS_ECDH_C)
5798 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005799#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02005800#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005801 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02005802#if defined(MBEDTLS_SSL_CLI_C)
5803 handshake->ecjpake_cache = NULL;
5804 handshake->ecjpake_cache_len = 0;
5805#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005806#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005807
5808#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5809 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5810#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005811}
5812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005813static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005814{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005815 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5818 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820 mbedtls_md_init( &transform->md_ctx_enc );
5821 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005822}
5823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005826 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005827}
5828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005829static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005830{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005831 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005832 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005833 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005834 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005835 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005836 if( ssl->handshake )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 mbedtls_ssl_handshake_free( ssl->handshake );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005838
5839 /*
5840 * Either the pointers are now NULL or cleared properly and can be freed.
5841 * Now allocate missing structures.
5842 */
5843 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005844 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005845 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005846 }
Paul Bakker48916f92012-09-16 19:57:18 +00005847
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005848 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005849 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005850 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005851 }
Paul Bakker48916f92012-09-16 19:57:18 +00005852
Paul Bakker82788fb2014-10-20 13:59:19 +02005853 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005854 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005855 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005856 }
Paul Bakker48916f92012-09-16 19:57:18 +00005857
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005858 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005859 if( ssl->handshake == NULL ||
5860 ssl->transform_negotiate == NULL ||
5861 ssl->session_negotiate == NULL )
5862 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 mbedtls_free( ssl->handshake );
5866 mbedtls_free( ssl->transform_negotiate );
5867 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005868
5869 ssl->handshake = NULL;
5870 ssl->transform_negotiate = NULL;
5871 ssl->session_negotiate = NULL;
5872
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005873 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005874 }
5875
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005876 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005878 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005879 ssl_handshake_params_init( ssl->handshake );
5880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005882 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5883 {
5884 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005885
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005886 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5887 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5888 else
5889 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005890
5891 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005892 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005893#endif
5894
Paul Bakker48916f92012-09-16 19:57:18 +00005895 return( 0 );
5896}
5897
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005898#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005899/* Dummy cookie callbacks for defaults */
5900static int ssl_cookie_write_dummy( void *ctx,
5901 unsigned char **p, unsigned char *end,
5902 const unsigned char *cli_id, size_t cli_id_len )
5903{
5904 ((void) ctx);
5905 ((void) p);
5906 ((void) end);
5907 ((void) cli_id);
5908 ((void) cli_id_len);
5909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005910 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005911}
5912
5913static int ssl_cookie_check_dummy( void *ctx,
5914 const unsigned char *cookie, size_t cookie_len,
5915 const unsigned char *cli_id, size_t cli_id_len )
5916{
5917 ((void) ctx);
5918 ((void) cookie);
5919 ((void) cookie_len);
5920 ((void) cli_id);
5921 ((void) cli_id_len);
5922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005923 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005924}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005925#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005926
Paul Bakker5121ce52009-01-03 21:22:43 +00005927/*
5928 * Initialize an SSL context
5929 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005930void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
5931{
5932 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
5933}
5934
5935/*
5936 * Setup an SSL context
5937 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005938int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02005939 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00005940{
Paul Bakker48916f92012-09-16 19:57:18 +00005941 int ret;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005942 const size_t len = MBEDTLS_SSL_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005943
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005944 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00005945
5946 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005947 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00005948 */
k-stachowiakc2eddee2018-07-09 10:39:20 +02005949 ssl->in_buf = NULL;
5950 ssl->out_buf = NULL;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005951 if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL ||
5952 ( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005953 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", len ) );
k-stachowiak2c161142018-07-31 16:52:32 +02005955 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiakc2eddee2018-07-09 10:39:20 +02005956 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005957 }
5958
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005959#if defined(MBEDTLS_SSL_PROTO_DTLS)
5960 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5961 {
5962 ssl->out_hdr = ssl->out_buf;
5963 ssl->out_ctr = ssl->out_buf + 3;
5964 ssl->out_len = ssl->out_buf + 11;
5965 ssl->out_iv = ssl->out_buf + 13;
5966 ssl->out_msg = ssl->out_buf + 13;
5967
5968 ssl->in_hdr = ssl->in_buf;
5969 ssl->in_ctr = ssl->in_buf + 3;
5970 ssl->in_len = ssl->in_buf + 11;
5971 ssl->in_iv = ssl->in_buf + 13;
5972 ssl->in_msg = ssl->in_buf + 13;
5973 }
5974 else
5975#endif
5976 {
5977 ssl->out_ctr = ssl->out_buf;
5978 ssl->out_hdr = ssl->out_buf + 8;
5979 ssl->out_len = ssl->out_buf + 11;
5980 ssl->out_iv = ssl->out_buf + 13;
5981 ssl->out_msg = ssl->out_buf + 13;
5982
5983 ssl->in_ctr = ssl->in_buf;
5984 ssl->in_hdr = ssl->in_buf + 8;
5985 ssl->in_len = ssl->in_buf + 11;
5986 ssl->in_iv = ssl->in_buf + 13;
5987 ssl->in_msg = ssl->in_buf + 13;
5988 }
5989
Paul Bakker48916f92012-09-16 19:57:18 +00005990 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiakc2eddee2018-07-09 10:39:20 +02005991 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005992
5993 return( 0 );
k-stachowiakc2eddee2018-07-09 10:39:20 +02005994
5995error:
5996 mbedtls_free( ssl->in_buf );
5997 mbedtls_free( ssl->out_buf );
5998
5999 ssl->conf = NULL;
6000
6001 ssl->in_buf = NULL;
6002 ssl->out_buf = NULL;
6003
6004 ssl->in_hdr = NULL;
6005 ssl->in_ctr = NULL;
6006 ssl->in_len = NULL;
6007 ssl->in_iv = NULL;
6008 ssl->in_msg = NULL;
6009
6010 ssl->out_hdr = NULL;
6011 ssl->out_ctr = NULL;
6012 ssl->out_len = NULL;
6013 ssl->out_iv = NULL;
6014 ssl->out_msg = NULL;
6015
k-stachowiak2c161142018-07-31 16:52:32 +02006016 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006017}
6018
6019/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006020 * Reset an initialized and used SSL context for re-use while retaining
6021 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006022 *
6023 * If partial is non-zero, keep data in the input buffer and client ID.
6024 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006025 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006026static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006027{
Paul Bakker48916f92012-09-16 19:57:18 +00006028 int ret;
6029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006031
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006032 /* Cancel any possibly running timer */
6033 ssl_set_timer( ssl, 0 );
6034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035#if defined(MBEDTLS_SSL_RENEGOTIATION)
6036 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006037 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006038
6039 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006040 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6041 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006044
Paul Bakker7eb013f2011-10-06 12:37:39 +00006045 ssl->in_offt = NULL;
6046
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006047 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006048 ssl->in_msgtype = 0;
6049 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006050 if( partial == 0 )
6051 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006053 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006054 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006055#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006057 ssl_dtls_replay_reset( ssl );
6058#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006059
6060 ssl->in_hslen = 0;
6061 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006062
6063 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006064
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006065 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006066 ssl->out_msgtype = 0;
6067 ssl->out_msglen = 0;
6068 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6070 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006071 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006072#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006073
Paul Bakker48916f92012-09-16 19:57:18 +00006074 ssl->transform_in = NULL;
6075 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006076
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006077 ssl->session_in = NULL;
6078 ssl->session_out = NULL;
6079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080 memset( ssl->out_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006081
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006082 if( partial == 0 )
6083 memset( ssl->in_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00006084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6086 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6089 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006091 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6092 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006093 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006094 }
6095#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006096
Paul Bakker48916f92012-09-16 19:57:18 +00006097 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006099 mbedtls_ssl_transform_free( ssl->transform );
6100 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006101 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006102 }
Paul Bakker48916f92012-09-16 19:57:18 +00006103
Paul Bakkerc0463502013-02-14 11:19:38 +01006104 if( ssl->session )
6105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006106 mbedtls_ssl_session_free( ssl->session );
6107 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006108 ssl->session = NULL;
6109 }
6110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006112 ssl->alpn_chosen = NULL;
6113#endif
6114
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006115#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006116 if( partial == 0 )
6117 {
6118 mbedtls_free( ssl->cli_id );
6119 ssl->cli_id = NULL;
6120 ssl->cli_id_len = 0;
6121 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006122#endif
6123
Paul Bakker48916f92012-09-16 19:57:18 +00006124 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6125 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006126
6127 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006128}
6129
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006130/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006131 * Reset an initialized and used SSL context for re-use while retaining
6132 * all application-set variables, function pointers and data.
6133 */
6134int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6135{
6136 return( ssl_session_reset_int( ssl, 0 ) );
6137}
6138
6139/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006140 * SSL set accessors
6141 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006142void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006143{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006144 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006145}
6146
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006147void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006148{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006149 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006150}
6151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006152#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006153void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006154{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006155 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006156}
6157#endif
6158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006160void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006161{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006162 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006163}
6164#endif
6165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006166#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006167void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006168{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006169 conf->hs_timeout_min = min;
6170 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006171}
6172#endif
6173
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006174void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006175{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006176 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006177}
6178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006180void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006181 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006182 void *p_vrfy )
6183{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006184 conf->f_vrfy = f_vrfy;
6185 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006186}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006188
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006189void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006190 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006191 void *p_rng )
6192{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006193 conf->f_rng = f_rng;
6194 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006195}
6196
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006197void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006198 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006199 void *p_dbg )
6200{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006201 conf->f_dbg = f_dbg;
6202 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006203}
6204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006206 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006207 mbedtls_ssl_send_t *f_send,
6208 mbedtls_ssl_recv_t *f_recv,
6209 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006210{
6211 ssl->p_bio = p_bio;
6212 ssl->f_send = f_send;
6213 ssl->f_recv = f_recv;
6214 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006215}
6216
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006217void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006218{
6219 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006220}
6221
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006222void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6223 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006224 mbedtls_ssl_set_timer_t *f_set_timer,
6225 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006226{
6227 ssl->p_timer = p_timer;
6228 ssl->f_set_timer = f_set_timer;
6229 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006230
6231 /* Make sure we start with no timer running */
6232 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006233}
6234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006236void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006237 void *p_cache,
6238 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6239 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006240{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006241 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006242 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006243 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006244}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006247#if defined(MBEDTLS_SSL_CLI_C)
6248int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006249{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006250 int ret;
6251
6252 if( ssl == NULL ||
6253 session == NULL ||
6254 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006255 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006258 }
6259
6260 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6261 return( ret );
6262
Paul Bakker0a597072012-09-25 21:55:46 +00006263 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006264
6265 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006266}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006267#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006268
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006269void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006270 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006271{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006272 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6273 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6274 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6275 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006276}
6277
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006278void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006279 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006280 int major, int minor )
6281{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006283 return;
6284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006286 return;
6287
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006288 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006289}
6290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006292void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006293 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006294{
6295 conf->cert_profile = profile;
6296}
6297
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006298/* Append a new keycert entry to a (possibly empty) list */
6299static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6300 mbedtls_x509_crt *cert,
6301 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006302{
niisatoa35dbf12018-06-25 19:05:48 +09006303 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006304
niisatoa35dbf12018-06-25 19:05:48 +09006305 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6306 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006307 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006308
niisatoa35dbf12018-06-25 19:05:48 +09006309 new_cert->cert = cert;
6310 new_cert->key = key;
6311 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006312
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006313 /* Update head is the list was null, else add to the end */
6314 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006315 {
niisatoa35dbf12018-06-25 19:05:48 +09006316 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006317 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006318 else
6319 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006320 mbedtls_ssl_key_cert *cur = *head;
6321 while( cur->next != NULL )
6322 cur = cur->next;
niisatoa35dbf12018-06-25 19:05:48 +09006323 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006324 }
6325
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006326 return( 0 );
6327}
6328
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006329int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006330 mbedtls_x509_crt *own_cert,
6331 mbedtls_pk_context *pk_key )
6332{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006333 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006334}
6335
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006336void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006337 mbedtls_x509_crt *ca_chain,
6338 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006339{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006340 conf->ca_chain = ca_chain;
6341 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006343#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006344
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006345#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6346int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6347 mbedtls_x509_crt *own_cert,
6348 mbedtls_pk_context *pk_key )
6349{
6350 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6351 own_cert, pk_key ) );
6352}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006353
6354void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6355 mbedtls_x509_crt *ca_chain,
6356 mbedtls_x509_crl *ca_crl )
6357{
6358 ssl->handshake->sni_ca_chain = ca_chain;
6359 ssl->handshake->sni_ca_crl = ca_crl;
6360}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006361
6362void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6363 int authmode )
6364{
6365 ssl->handshake->sni_authmode = authmode;
6366}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006367#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6368
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006369#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006370/*
6371 * Set EC J-PAKE password for current handshake
6372 */
6373int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6374 const unsigned char *pw,
6375 size_t pw_len )
6376{
6377 mbedtls_ecjpake_role role;
6378
Janos Follath8eb64132016-06-03 15:40:57 +01006379 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006380 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6381
6382 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6383 role = MBEDTLS_ECJPAKE_SERVER;
6384 else
6385 role = MBEDTLS_ECJPAKE_CLIENT;
6386
6387 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6388 role,
6389 MBEDTLS_MD_SHA256,
6390 MBEDTLS_ECP_DP_SECP256R1,
6391 pw, pw_len ) );
6392}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006393#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006395#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006396int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006397 const unsigned char *psk, size_t psk_len,
6398 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006399{
Paul Bakker6db455e2013-09-18 17:29:31 +02006400 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006401 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006403 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6404 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006405
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006406 /* Identity len will be encoded on two bytes */
6407 if( ( psk_identity_len >> 16 ) != 0 ||
6408 psk_identity_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
6409 {
6410 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6411 }
6412
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006413 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006414 {
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006415 mbedtls_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006416
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006417 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006418 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006419 conf->psk_len = 0;
6420 }
6421 if( conf->psk_identity != NULL )
6422 {
6423 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006424 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006425 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006426 }
6427
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006428 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6429 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006430 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006431 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006432 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006433 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006434 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006435 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006436 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006437
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006438 conf->psk_len = psk_len;
6439 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006440
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006441 memcpy( conf->psk, psk, conf->psk_len );
6442 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006443
6444 return( 0 );
6445}
6446
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006447int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6448 const unsigned char *psk, size_t psk_len )
6449{
6450 if( psk == NULL || ssl->handshake == NULL )
6451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6452
6453 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6454 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6455
6456 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006457 {
6458 mbedtls_zeroize( ssl->handshake->psk, ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006459 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006460 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006461 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006462
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006463 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006464 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006465
6466 ssl->handshake->psk_len = psk_len;
6467 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6468
6469 return( 0 );
6470}
6471
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006472void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006474 size_t),
6475 void *p_psk )
6476{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006477 conf->f_psk = f_psk;
6478 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006479}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006481
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006482#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006483
6484#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006485int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006486{
6487 int ret;
6488
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006489 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6490 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6491 {
6492 mbedtls_mpi_free( &conf->dhm_P );
6493 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006494 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006495 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006496
6497 return( 0 );
6498}
Hanno Becker470a8c42017-10-04 15:28:46 +01006499#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006500
Hanno Beckera90658f2017-10-04 15:29:08 +01006501int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6502 const unsigned char *dhm_P, size_t P_len,
6503 const unsigned char *dhm_G, size_t G_len )
6504{
6505 int ret;
6506
6507 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6508 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6509 {
6510 mbedtls_mpi_free( &conf->dhm_P );
6511 mbedtls_mpi_free( &conf->dhm_G );
6512 return( ret );
6513 }
6514
6515 return( 0 );
6516}
Paul Bakker5121ce52009-01-03 21:22:43 +00006517
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006518int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006519{
6520 int ret;
6521
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006522 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6523 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6524 {
6525 mbedtls_mpi_free( &conf->dhm_P );
6526 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006527 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006528 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006529
6530 return( 0 );
6531}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006532#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006533
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006534#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6535/*
6536 * Set the minimum length for Diffie-Hellman parameters
6537 */
6538void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6539 unsigned int bitlen )
6540{
6541 conf->dhm_min_bitlen = bitlen;
6542}
6543#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6544
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006545#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006546/*
6547 * Set allowed/preferred hashes for handshake signatures
6548 */
6549void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6550 const int *hashes )
6551{
6552 conf->sig_hashes = hashes;
6553}
Hanno Becker947194e2017-04-07 13:25:49 +01006554#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006555
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006556#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006557/*
6558 * Set the allowed elliptic curves
6559 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006560void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006561 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006562{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006563 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006564}
Hanno Becker947194e2017-04-07 13:25:49 +01006565#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006566
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006567#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006569{
Hanno Becker947194e2017-04-07 13:25:49 +01006570 /* Initialize to suppress unnecessary compiler warning */
6571 size_t hostname_len = 0;
6572
6573 /* Check if new hostname is valid before
6574 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006575 if( hostname != NULL )
6576 {
6577 hostname_len = strlen( hostname );
6578
6579 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6581 }
6582
6583 /* Now it's clear that we will overwrite the old hostname,
6584 * so we can free it safely */
6585
6586 if( ssl->hostname != NULL )
6587 {
6588 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
6589 mbedtls_free( ssl->hostname );
6590 }
6591
6592 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006593
Paul Bakker5121ce52009-01-03 21:22:43 +00006594 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006595 {
6596 ssl->hostname = NULL;
6597 }
6598 else
6599 {
6600 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006601 if( ssl->hostname == NULL )
6602 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006603
Hanno Becker947194e2017-04-07 13:25:49 +01006604 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006605
Hanno Becker947194e2017-04-07 13:25:49 +01006606 ssl->hostname[hostname_len] = '\0';
6607 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006608
6609 return( 0 );
6610}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006611#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006612
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006613#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006614void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006616 const unsigned char *, size_t),
6617 void *p_sni )
6618{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006619 conf->f_sni = f_sni;
6620 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006621}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006622#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006624#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006625int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006626{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006627 size_t cur_len, tot_len;
6628 const char **p;
6629
6630 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006631 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6632 * MUST NOT be truncated."
6633 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006634 */
6635 tot_len = 0;
6636 for( p = protos; *p != NULL; p++ )
6637 {
6638 cur_len = strlen( *p );
6639 tot_len += cur_len;
6640
Ronald Crona32236c2020-04-23 16:41:44 +02006641 if( ( cur_len == 0 ) ||
6642 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
6643 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006645 }
6646
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006647 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006648
6649 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006650}
6651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006653{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006654 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006655}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006657
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006658void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006659{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006660 conf->max_major_ver = major;
6661 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006662}
6663
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006664void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006665{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006666 conf->min_major_ver = major;
6667 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006668}
6669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006671void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006672{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006673 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006674}
6675#endif
6676
Janos Follath088ce432017-04-10 12:42:31 +01006677#if defined(MBEDTLS_SSL_SRV_C)
6678void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6679 char cert_req_ca_list )
6680{
6681 conf->cert_req_ca_list = cert_req_ca_list;
6682}
6683#endif
6684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006686void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006687{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006688 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006689}
6690#endif
6691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006693void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006694{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006695 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006696}
6697#endif
6698
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006699#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006700void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006701{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006702 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006703}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006704#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006707int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006708{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006709 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
6710 mfl_code_to_length[mfl_code] > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006713 }
6714
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006715 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006716
6717 return( 0 );
6718}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006722void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006723{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006724 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006729void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006730{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006731 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006732}
6733#endif
6734
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006735void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006736{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006737 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006738}
6739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006741void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006742{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006743 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006744}
6745
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006746void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006747{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006748 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006749}
6750
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006751void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006752 const unsigned char period[8] )
6753{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006754 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006755}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006756#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006759#if defined(MBEDTLS_SSL_CLI_C)
6760void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006761{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006762 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006763}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006764#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006765
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006766#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006767void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6768 mbedtls_ssl_ticket_write_t *f_ticket_write,
6769 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6770 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006771{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006772 conf->f_ticket_write = f_ticket_write;
6773 conf->f_ticket_parse = f_ticket_parse;
6774 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006775}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006776#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006778
Robert Cragie4feb7ae2015-10-02 13:33:37 +01006779#if defined(MBEDTLS_SSL_EXPORT_KEYS)
6780void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
6781 mbedtls_ssl_export_keys_t *f_export_keys,
6782 void *p_export_keys )
6783{
6784 conf->f_export_keys = f_export_keys;
6785 conf->p_export_keys = p_export_keys;
6786}
6787#endif
6788
Paul Bakker5121ce52009-01-03 21:22:43 +00006789/*
6790 * SSL get accessors
6791 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006793{
6794 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6795}
6796
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006797uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006798{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006799 if( ssl->session != NULL )
6800 return( ssl->session->verify_result );
6801
6802 if( ssl->session_negotiate != NULL )
6803 return( ssl->session_negotiate->verify_result );
6804
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006805 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006806}
6807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006809{
Paul Bakker926c8e42013-03-06 10:23:34 +01006810 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006811 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006814}
6815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006816const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006817{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006818#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006819 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006820 {
6821 switch( ssl->minor_ver )
6822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006824 return( "DTLSv1.0" );
6825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006827 return( "DTLSv1.2" );
6828
6829 default:
6830 return( "unknown (DTLS)" );
6831 }
6832 }
6833#endif
6834
Paul Bakker43ca69c2011-01-15 17:35:19 +00006835 switch( ssl->minor_ver )
6836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006838 return( "SSLv3.0" );
6839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006841 return( "TLSv1.0" );
6842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006844 return( "TLSv1.1" );
6845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00006847 return( "TLSv1.2" );
6848
Paul Bakker43ca69c2011-01-15 17:35:19 +00006849 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006850 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00006851 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00006852}
6853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006855{
Hanno Becker12f7ede2018-08-17 15:28:19 +01006856 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006858 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006859
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006860 if( transform == NULL )
6861 return( (int) mbedtls_ssl_hdr_len( ssl ) );
6862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863#if defined(MBEDTLS_ZLIB_SUPPORT)
6864 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
6865 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006866#endif
6867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870 case MBEDTLS_MODE_GCM:
6871 case MBEDTLS_MODE_CCM:
6872 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006873 transform_expansion = transform->minlen;
6874 break;
6875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006876 case MBEDTLS_MODE_CBC:
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006877
6878 block_size = mbedtls_cipher_get_block_size(
6879 &transform->cipher_ctx_enc );
6880
Hanno Becker12f7ede2018-08-17 15:28:19 +01006881 /* Expansion due to the addition of the MAC. */
6882 transform_expansion += transform->maclen;
6883
6884 /* Expansion due to the addition of CBC padding;
6885 * Theoretically up to 256 bytes, but we never use
6886 * more than the block size of the underlying cipher. */
6887 transform_expansion += block_size;
6888
6889 /* For TLS 1.1 or higher, an explicit IV is added
6890 * after the record header. */
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006891#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
6892 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker12f7ede2018-08-17 15:28:19 +01006893 transform_expansion += block_size;
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006894#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker12f7ede2018-08-17 15:28:19 +01006895
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006896 break;
6897
6898 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006900 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006901 }
6902
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006903 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006904}
6905
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02006906#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
6907size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
6908{
6909 size_t max_len;
6910
6911 /*
6912 * Assume mfl_code is correct since it was checked when set
6913 */
6914 max_len = mfl_code_to_length[ssl->conf->mfl_code];
6915
6916 /*
6917 * Check if a smaller max length was negotiated
6918 */
6919 if( ssl->session_out != NULL &&
6920 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6921 {
6922 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6923 }
6924
6925 return max_len;
6926}
6927#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
6928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006929#if defined(MBEDTLS_X509_CRT_PARSE_C)
6930const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00006931{
6932 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006933 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006934
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006935 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006936}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00006938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939#if defined(MBEDTLS_SSL_CLI_C)
6940int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006941{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006942 if( ssl == NULL ||
6943 dst == NULL ||
6944 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006945 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006948 }
6949
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006950 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006951}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006953
Paul Bakker5121ce52009-01-03 21:22:43 +00006954/*
Paul Bakker1961b702013-01-25 14:49:24 +01006955 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00006956 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006957int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006958{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006960
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006961 if( ssl == NULL || ssl->conf == NULL )
6962 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006965 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006967#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006969 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006971#endif
6972
Paul Bakker1961b702013-01-25 14:49:24 +01006973 return( ret );
6974}
6975
6976/*
6977 * Perform the SSL handshake
6978 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01006980{
6981 int ret = 0;
6982
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006983 if( ssl == NULL || ssl->conf == NULL )
6984 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01006987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01006989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01006991
6992 if( ret != 0 )
6993 break;
6994 }
6995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006997
6998 return( ret );
6999}
7000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001#if defined(MBEDTLS_SSL_RENEGOTIATION)
7002#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007003/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007004 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007007{
7008 int ret;
7009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007011
7012 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7014 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007019 return( ret );
7020 }
7021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007023
7024 return( 0 );
7025}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007027
7028/*
7029 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030 * - any side: calling mbedtls_ssl_renegotiate(),
7031 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7032 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007033 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007034 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007036 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007038{
7039 int ret;
7040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007042
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007043 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7044 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007045
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007046 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7047 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007048#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007049 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007051 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007052 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007053 ssl->handshake->out_msg_seq = 1;
7054 else
7055 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007056 }
7057#endif
7058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7060 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007065 return( ret );
7066 }
7067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007069
7070 return( 0 );
7071}
7072
7073/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007074 * Renegotiate current connection on client,
7075 * or request renegotiation on server
7076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007078{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007080
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007081 if( ssl == NULL || ssl->conf == NULL )
7082 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007085 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007086 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007092
7093 /* Did we already try/start sending HelloRequest? */
7094 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007096
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007097 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007098 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007101#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007102 /*
7103 * On client, either start the renegotiation process or,
7104 * if already in progress, continue the handshake
7105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7109 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007110
7111 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007114 return( ret );
7115 }
7116 }
7117 else
7118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007122 return( ret );
7123 }
7124 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007126
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007127 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007128}
7129
7130/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007131 * Check record counters and renegotiate if they're above the limit.
7132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007134{
Andres AG2196c7f2016-12-15 17:01:16 +00007135 size_t ep_len = ssl_ep_len( ssl );
7136 int in_ctr_cmp;
7137 int out_ctr_cmp;
7138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7140 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007141 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007142 {
7143 return( 0 );
7144 }
7145
Andres AG2196c7f2016-12-15 17:01:16 +00007146 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7147 ssl->conf->renego_period + ep_len, 8 - ep_len );
7148 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
7149 ssl->conf->renego_period + ep_len, 8 - ep_len );
7150
7151 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007152 {
7153 return( 0 );
7154 }
7155
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007160
7161/*
7162 * Receive application data decrypted from the SSL layer
7163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007165{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007166 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007167 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007168
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007169 if( ssl == NULL || ssl->conf == NULL )
7170 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007175 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007177 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007178 return( ret );
7179
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007180 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007181 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007183 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007184 return( ret );
7185 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007186 }
7187#endif
7188
Hanno Becker4a810fb2017-05-24 16:27:30 +01007189 /*
7190 * Check if renegotiation is necessary and/or handshake is
7191 * in process. If yes, perform/continue, and fall through
7192 * if an unexpected packet is received while the client
7193 * is waiting for the ServerHello.
7194 *
7195 * (There is no equivalent to the last condition on
7196 * the server-side as it is not treated as within
7197 * a handshake while waiting for the ClientHello
7198 * after a renegotiation request.)
7199 */
7200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007202 ret = ssl_check_ctr_renegotiate( ssl );
7203 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7204 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007207 return( ret );
7208 }
7209#endif
7210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007214 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7215 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007218 return( ret );
7219 }
7220 }
7221
7222 if( ssl->in_offt == NULL )
7223 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007224 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007225 if( ssl->f_get_timer != NULL &&
7226 ssl->f_get_timer( ssl->p_timer ) == -1 )
7227 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007228 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007229 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007230
Hanno Becker4a810fb2017-05-24 16:27:30 +01007231 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007232 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007233 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7234 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007235
Hanno Becker4a810fb2017-05-24 16:27:30 +01007236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7237 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007238 }
7239
7240 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007242 {
7243 /*
7244 * OpenSSL sends empty messages to randomize the IV
7245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007249 return( 0 );
7250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007252 return( ret );
7253 }
7254 }
7255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007259
Hanno Becker4a810fb2017-05-24 16:27:30 +01007260 /*
7261 * - For client-side, expect SERVER_HELLO_REQUEST.
7262 * - For server-side, expect CLIENT_HELLO.
7263 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7264 */
7265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007267 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007269 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007272
7273 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007275 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007276 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007277#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007279 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007280#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007281
Hanno Becker4a810fb2017-05-24 16:27:30 +01007282#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007283 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007287
7288 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007290 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007291 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007292#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007294 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007295#endif /* MBEDTLS_SSL_SRV_C */
7296
Hanno Becker21df7f92017-10-17 11:03:26 +01007297#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007298 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007299 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7300 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7301 ssl->conf->allow_legacy_renegotiation ==
7302 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7303 {
7304 /*
7305 * Accept renegotiation request
7306 */
Paul Bakker48916f92012-09-16 19:57:18 +00007307
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007308 /* DTLS clients need to know renego is server-initiated */
7309#if defined(MBEDTLS_SSL_PROTO_DTLS)
7310 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7311 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7312 {
7313 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7314 }
7315#endif
7316 ret = ssl_start_renegotiation( ssl );
7317 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7318 ret != 0 )
7319 {
7320 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7321 return( ret );
7322 }
7323 }
7324 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007325#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007326 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007327 /*
7328 * Refuse renegotiation
7329 */
7330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333#if defined(MBEDTLS_SSL_PROTO_SSL3)
7334 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007335 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007336 /* SSLv3 does not have a "no_renegotiation" warning, so
7337 we send a fatal alert and abort the connection. */
7338 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7339 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7340 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007341 }
7342 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7344#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7345 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7346 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7349 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7350 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007351 {
7352 return( ret );
7353 }
Paul Bakker48916f92012-09-16 19:57:18 +00007354 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007355 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7357 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7360 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007361 }
Paul Bakker48916f92012-09-16 19:57:18 +00007362 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007363
Hanno Becker4a810fb2017-05-24 16:27:30 +01007364 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00007365 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007366#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007368 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007369 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007370 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007371 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007374 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007376 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007377 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007378 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7382 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007385 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007386 }
7387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7391 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007392 }
7393
7394 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007395
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007396 /* We're going to return something now, cancel timer,
7397 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007399 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007400
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007401#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007402 /* If we requested renego but received AppData, resend HelloRequest.
7403 * Do it now, after setting in_offt, to avoid taking this branch
7404 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007406 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007408 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007409 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007412 return( ret );
7413 }
7414 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007416#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007417 }
7418
7419 n = ( len < ssl->in_msglen )
7420 ? len : ssl->in_msglen;
7421
7422 memcpy( buf, ssl->in_offt, n );
7423 ssl->in_msglen -= n;
7424
gabor-mezei-armef738752020-07-15 10:55:00 +02007425 /* Zeroising the plaintext buffer to erase unused application data
7426 from the memory. */
7427 mbedtls_zeroize( ssl->in_offt, n );
7428
Paul Bakker5121ce52009-01-03 21:22:43 +00007429 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007430 {
7431 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007432 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007433 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007434 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007435 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007436 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007437 /* more data available */
7438 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007439 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007442
Paul Bakker23986e52011-04-24 08:57:21 +00007443 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007444}
7445
7446/*
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007447 * Send application data to be encrypted by the SSL layer, taking care of max
7448 * fragment length and buffer size.
7449 *
7450 * According to RFC 5246 Section 6.2.1:
7451 *
7452 * Zero-length fragments of Application data MAY be sent as they are
7453 * potentially useful as a traffic analysis countermeasure.
7454 *
7455 * Therefore, it is possible that the input message length is 0 and the
7456 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007457 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007458static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007459 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007460{
Paul Bakker23986e52011-04-24 08:57:21 +00007461 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007463 size_t max_len = mbedtls_ssl_get_max_frag_len( ssl );
Florin0b7b83f2017-07-22 09:01:44 +02007464#else
7465 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
7466#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007467 if( len > max_len )
7468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007470 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007473 "maximum fragment length: %d > %d",
7474 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007476 }
7477 else
7478#endif
7479 len = max_len;
7480 }
Paul Bakker887bd502011-06-08 13:10:54 +00007481
Paul Bakker5121ce52009-01-03 21:22:43 +00007482 if( ssl->out_left != 0 )
7483 {
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007484 /*
7485 * The user has previously tried to send the data and
7486 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7487 * written. In this case, we expect the high-level write function
7488 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007490 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007492 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007493 return( ret );
7494 }
7495 }
Paul Bakker887bd502011-06-08 13:10:54 +00007496 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007497 {
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007498 /*
7499 * The user is trying to send a message the first time, so we need to
7500 * copy the data into the internal buffers and setup the data structure
7501 * to keep track of partial writes
7502 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007503 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007505 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007510 return( ret );
7511 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007512 }
7513
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007514 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007515}
7516
7517/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007518 * Write application data, doing 1/n-1 splitting if necessary.
7519 *
7520 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007521 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01007522 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007525static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007526 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007527{
7528 int ret;
7529
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007530 if( ssl->conf->cbc_record_splitting ==
7531 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007532 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7534 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7535 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007536 {
7537 return( ssl_write_real( ssl, buf, len ) );
7538 }
7539
7540 if( ssl->split_done == 0 )
7541 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007542 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007543 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007544 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007545 }
7546
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007547 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7548 return( ret );
7549 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007550
7551 return( ret + 1 );
7552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007554
7555/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007556 * Write application data (public-facing wrapper)
7557 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007558int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007559{
7560 int ret;
7561
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007563
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007564 if( ssl == NULL || ssl->conf == NULL )
7565 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7566
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007567#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007568 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7569 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007570 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007571 return( ret );
7572 }
7573#endif
7574
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007575 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007576 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007577 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007578 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007580 return( ret );
7581 }
7582 }
7583
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007584#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007585 ret = ssl_write_split( ssl, buf, len );
7586#else
7587 ret = ssl_write_real( ssl, buf, len );
7588#endif
7589
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007591
7592 return( ret );
7593}
7594
7595/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007596 * Notify the peer that the connection is being closed
7597 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007599{
7600 int ret;
7601
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007602 if( ssl == NULL || ssl->conf == NULL )
7603 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007606
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007607 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7613 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7614 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007617 return( ret );
7618 }
7619 }
7620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007622
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007623 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007624}
7625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00007627{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007628 if( transform == NULL )
7629 return;
7630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00007632 deflateEnd( &transform->ctx_deflate );
7633 inflateEnd( &transform->ctx_inflate );
7634#endif
7635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636 mbedtls_cipher_free( &transform->cipher_ctx_enc );
7637 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02007638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639 mbedtls_md_free( &transform->md_ctx_enc );
7640 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02007641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642 mbedtls_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007643}
7644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645#if defined(MBEDTLS_X509_CRT_PARSE_C)
7646static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007647{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007649
7650 while( cur != NULL )
7651 {
7652 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007654 cur = next;
7655 }
7656}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007657#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake )
Paul Bakker48916f92012-09-16 19:57:18 +00007660{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007661 if( handshake == NULL )
7662 return;
7663
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007664#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7665 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7666 mbedtls_md5_free( &handshake->fin_md5 );
7667 mbedtls_sha1_free( &handshake->fin_sha1 );
7668#endif
7669#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7670#if defined(MBEDTLS_SHA256_C)
7671 mbedtls_sha256_free( &handshake->fin_sha256 );
7672#endif
7673#if defined(MBEDTLS_SHA512_C)
7674 mbedtls_sha512_free( &handshake->fin_sha512 );
7675#endif
7676#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007678#if defined(MBEDTLS_DHM_C)
7679 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007680#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681#if defined(MBEDTLS_ECDH_C)
7682 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007683#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007684#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007685 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007686#if defined(MBEDTLS_SSL_CLI_C)
7687 mbedtls_free( handshake->ecjpake_cache );
7688 handshake->ecjpake_cache = NULL;
7689 handshake->ecjpake_cache_len = 0;
7690#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007691#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02007692
Janos Follath4ae5c292016-02-10 11:27:43 +00007693#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
7694 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02007695 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007697#endif
7698
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007699#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7700 if( handshake->psk != NULL )
7701 {
7702 mbedtls_zeroize( handshake->psk, handshake->psk_len );
7703 mbedtls_free( handshake->psk );
7704 }
7705#endif
7706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7708 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007709 /*
7710 * Free only the linked list wrapper, not the keys themselves
7711 * since the belong to the SNI callback
7712 */
7713 if( handshake->sni_key_cert != NULL )
7714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007715 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007716
7717 while( cur != NULL )
7718 {
7719 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007721 cur = next;
7722 }
7723 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726#if defined(MBEDTLS_SSL_PROTO_DTLS)
7727 mbedtls_free( handshake->verify_cookie );
7728 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007729 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007730#endif
7731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007732 mbedtls_zeroize( handshake, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007733}
7734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007736{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007737 if( session == NULL )
7738 return;
7739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007741 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743 mbedtls_x509_crt_free( session->peer_cert );
7744 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007745 }
Paul Bakkered27a042013-04-18 22:46:23 +02007746#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007747
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007748#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007749 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007750#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 mbedtls_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007753}
7754
Paul Bakker5121ce52009-01-03 21:22:43 +00007755/*
7756 * Free an SSL context
7757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007759{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007760 if( ssl == NULL )
7761 return;
7762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007764
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007765 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767 mbedtls_zeroize( ssl->out_buf, MBEDTLS_SSL_BUFFER_LEN );
7768 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007769 }
7770
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007771 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773 mbedtls_zeroize( ssl->in_buf, MBEDTLS_SSL_BUFFER_LEN );
7774 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007775 }
7776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02007778 if( ssl->compress_buf != NULL )
7779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007780 mbedtls_zeroize( ssl->compress_buf, MBEDTLS_SSL_BUFFER_LEN );
7781 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02007782 }
7783#endif
7784
Paul Bakker48916f92012-09-16 19:57:18 +00007785 if( ssl->transform )
7786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787 mbedtls_ssl_transform_free( ssl->transform );
7788 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007789 }
7790
7791 if( ssl->handshake )
7792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793 mbedtls_ssl_handshake_free( ssl->handshake );
7794 mbedtls_ssl_transform_free( ssl->transform_negotiate );
7795 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007797 mbedtls_free( ssl->handshake );
7798 mbedtls_free( ssl->transform_negotiate );
7799 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007800 }
7801
Paul Bakkerc0463502013-02-14 11:19:38 +01007802 if( ssl->session )
7803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007804 mbedtls_ssl_session_free( ssl->session );
7805 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007806 }
7807
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02007808#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02007809 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007810 {
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007811 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00007813 }
Paul Bakker0be444a2013-08-27 21:55:01 +02007814#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7817 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
7820 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00007821 }
7822#endif
7823
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007824#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007825 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007826#endif
7827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00007829
Paul Bakker86f04f42013-02-14 11:20:09 +01007830 /* Actually clear after last debug message */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831 mbedtls_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007832}
7833
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007834/*
7835 * Initialze mbedtls_ssl_config
7836 */
7837void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
7838{
7839 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
7840}
7841
Simon Butcherc97b6972015-12-27 23:48:17 +00007842#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007843static int ssl_preset_default_hashes[] = {
7844#if defined(MBEDTLS_SHA512_C)
7845 MBEDTLS_MD_SHA512,
7846 MBEDTLS_MD_SHA384,
7847#endif
7848#if defined(MBEDTLS_SHA256_C)
7849 MBEDTLS_MD_SHA256,
7850 MBEDTLS_MD_SHA224,
7851#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02007852#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007853 MBEDTLS_MD_SHA1,
7854#endif
7855 MBEDTLS_MD_NONE
7856};
Simon Butcherc97b6972015-12-27 23:48:17 +00007857#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007858
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007859static int ssl_preset_suiteb_ciphersuites[] = {
7860 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
7861 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
7862 0
7863};
7864
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007865#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007866static int ssl_preset_suiteb_hashes[] = {
7867 MBEDTLS_MD_SHA256,
7868 MBEDTLS_MD_SHA384,
7869 MBEDTLS_MD_NONE
7870};
7871#endif
7872
7873#if defined(MBEDTLS_ECP_C)
7874static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007875#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007876 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007877#endif
7878#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007879 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007880#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007881 MBEDTLS_ECP_DP_NONE
7882};
7883#endif
7884
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007885/*
Tillmann Karras588ad502015-09-25 04:27:22 +02007886 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007887 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007888int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007889 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007890{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007891#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007892 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007893#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007894
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02007895 /* Use the functions here so that they are covered in tests,
7896 * but otherwise access member directly for efficiency */
7897 mbedtls_ssl_conf_endpoint( conf, endpoint );
7898 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007899
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007900 /*
7901 * Things that are common to all presets
7902 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007903#if defined(MBEDTLS_SSL_CLI_C)
7904 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7905 {
7906 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
7907#if defined(MBEDTLS_SSL_SESSION_TICKETS)
7908 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
7909#endif
7910 }
7911#endif
7912
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007913#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007914 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007915#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007916
7917#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7918 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
7919#endif
7920
7921#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7922 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
7923#endif
7924
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007925#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7926 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7927#endif
7928
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007929#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007930 conf->f_cookie_write = ssl_cookie_write_dummy;
7931 conf->f_cookie_check = ssl_cookie_check_dummy;
7932#endif
7933
7934#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7935 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7936#endif
7937
Janos Follath088ce432017-04-10 12:42:31 +01007938#if defined(MBEDTLS_SSL_SRV_C)
7939 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
7940#endif
7941
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007942#if defined(MBEDTLS_SSL_PROTO_DTLS)
7943 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7944 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7945#endif
7946
7947#if defined(MBEDTLS_SSL_RENEGOTIATION)
7948 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00007949 memset( conf->renego_period, 0x00, 2 );
7950 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007951#endif
7952
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007953#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
7954 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7955 {
Hanno Becker00d0a682017-10-04 13:14:29 +01007956 const unsigned char dhm_p[] =
7957 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
7958 const unsigned char dhm_g[] =
7959 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
7960
Hanno Beckera90658f2017-10-04 15:29:08 +01007961 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
7962 dhm_p, sizeof( dhm_p ),
7963 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007964 {
7965 return( ret );
7966 }
7967 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007968#endif
7969
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007970 /*
7971 * Preset-specific defaults
7972 */
7973 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007974 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007975 /*
7976 * NSA Suite B
7977 */
7978 case MBEDTLS_SSL_PRESET_SUITEB:
7979 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7980 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7981 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7982 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7983
7984 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7985 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7986 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7987 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7988 ssl_preset_suiteb_ciphersuites;
7989
7990#if defined(MBEDTLS_X509_CRT_PARSE_C)
7991 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007992#endif
7993
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007994#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007995 conf->sig_hashes = ssl_preset_suiteb_hashes;
7996#endif
7997
7998#if defined(MBEDTLS_ECP_C)
7999 conf->curve_list = ssl_preset_suiteb_curves;
8000#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008001 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008002
8003 /*
8004 * Default
8005 */
8006 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008007 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8008 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8009 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8010 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8011 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8012 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8013 MBEDTLS_SSL_MIN_MINOR_VERSION :
8014 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008015 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8016 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8017
8018#if defined(MBEDTLS_SSL_PROTO_DTLS)
8019 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8020 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8021#endif
8022
8023 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8024 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8025 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8026 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8027 mbedtls_ssl_list_ciphersuites();
8028
8029#if defined(MBEDTLS_X509_CRT_PARSE_C)
8030 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8031#endif
8032
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008033#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008034 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008035#endif
8036
8037#if defined(MBEDTLS_ECP_C)
8038 conf->curve_list = mbedtls_ecp_grp_id_list();
8039#endif
8040
8041#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8042 conf->dhm_min_bitlen = 1024;
8043#endif
8044 }
8045
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008046 return( 0 );
8047}
8048
8049/*
8050 * Free mbedtls_ssl_config
8051 */
8052void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8053{
8054#if defined(MBEDTLS_DHM_C)
8055 mbedtls_mpi_free( &conf->dhm_P );
8056 mbedtls_mpi_free( &conf->dhm_G );
8057#endif
8058
8059#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8060 if( conf->psk != NULL )
8061 {
8062 mbedtls_zeroize( conf->psk, conf->psk_len );
8063 mbedtls_zeroize( conf->psk_identity, conf->psk_identity_len );
8064 mbedtls_free( conf->psk );
8065 mbedtls_free( conf->psk_identity );
8066 conf->psk_len = 0;
8067 conf->psk_identity_len = 0;
8068 }
8069#endif
8070
8071#if defined(MBEDTLS_X509_CRT_PARSE_C)
8072 ssl_key_cert_free( conf->key_cert );
8073#endif
8074
8075 mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
8076}
8077
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008078#if defined(MBEDTLS_PK_C) && \
8079 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008080/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008083unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008084{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085#if defined(MBEDTLS_RSA_C)
8086 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8087 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008088#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008089#if defined(MBEDTLS_ECDSA_C)
8090 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8091 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008092#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008093 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008094}
8095
Hanno Becker7e5437a2017-04-28 17:15:26 +01008096unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8097{
8098 switch( type ) {
8099 case MBEDTLS_PK_RSA:
8100 return( MBEDTLS_SSL_SIG_RSA );
8101 case MBEDTLS_PK_ECDSA:
8102 case MBEDTLS_PK_ECKEY:
8103 return( MBEDTLS_SSL_SIG_ECDSA );
8104 default:
8105 return( MBEDTLS_SSL_SIG_ANON );
8106 }
8107}
8108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008109mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008110{
8111 switch( sig )
8112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008113#if defined(MBEDTLS_RSA_C)
8114 case MBEDTLS_SSL_SIG_RSA:
8115 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008116#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008117#if defined(MBEDTLS_ECDSA_C)
8118 case MBEDTLS_SSL_SIG_ECDSA:
8119 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008120#endif
8121 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008122 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008123 }
8124}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008125#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008126
Hanno Becker7e5437a2017-04-28 17:15:26 +01008127#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8128 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8129
8130/* Find an entry in a signature-hash set matching a given hash algorithm. */
8131mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8132 mbedtls_pk_type_t sig_alg )
8133{
8134 switch( sig_alg )
8135 {
8136 case MBEDTLS_PK_RSA:
8137 return( set->rsa );
8138 case MBEDTLS_PK_ECDSA:
8139 return( set->ecdsa );
8140 default:
8141 return( MBEDTLS_MD_NONE );
8142 }
8143}
8144
8145/* Add a signature-hash-pair to a signature-hash set */
8146void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8147 mbedtls_pk_type_t sig_alg,
8148 mbedtls_md_type_t md_alg )
8149{
8150 switch( sig_alg )
8151 {
8152 case MBEDTLS_PK_RSA:
8153 if( set->rsa == MBEDTLS_MD_NONE )
8154 set->rsa = md_alg;
8155 break;
8156
8157 case MBEDTLS_PK_ECDSA:
8158 if( set->ecdsa == MBEDTLS_MD_NONE )
8159 set->ecdsa = md_alg;
8160 break;
8161
8162 default:
8163 break;
8164 }
8165}
8166
8167/* Allow exactly one hash algorithm for each signature. */
8168void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8169 mbedtls_md_type_t md_alg )
8170{
8171 set->rsa = md_alg;
8172 set->ecdsa = md_alg;
8173}
8174
8175#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8176 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8177
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008178/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008179 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008182{
8183 switch( hash )
8184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185#if defined(MBEDTLS_MD5_C)
8186 case MBEDTLS_SSL_HASH_MD5:
8187 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008188#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008189#if defined(MBEDTLS_SHA1_C)
8190 case MBEDTLS_SSL_HASH_SHA1:
8191 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008193#if defined(MBEDTLS_SHA256_C)
8194 case MBEDTLS_SSL_HASH_SHA224:
8195 return( MBEDTLS_MD_SHA224 );
8196 case MBEDTLS_SSL_HASH_SHA256:
8197 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008198#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199#if defined(MBEDTLS_SHA512_C)
8200 case MBEDTLS_SSL_HASH_SHA384:
8201 return( MBEDTLS_MD_SHA384 );
8202 case MBEDTLS_SSL_HASH_SHA512:
8203 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008204#endif
8205 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008206 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008207 }
8208}
8209
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008210/*
8211 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8212 */
8213unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8214{
8215 switch( md )
8216 {
8217#if defined(MBEDTLS_MD5_C)
8218 case MBEDTLS_MD_MD5:
8219 return( MBEDTLS_SSL_HASH_MD5 );
8220#endif
8221#if defined(MBEDTLS_SHA1_C)
8222 case MBEDTLS_MD_SHA1:
8223 return( MBEDTLS_SSL_HASH_SHA1 );
8224#endif
8225#if defined(MBEDTLS_SHA256_C)
8226 case MBEDTLS_MD_SHA224:
8227 return( MBEDTLS_SSL_HASH_SHA224 );
8228 case MBEDTLS_MD_SHA256:
8229 return( MBEDTLS_SSL_HASH_SHA256 );
8230#endif
8231#if defined(MBEDTLS_SHA512_C)
8232 case MBEDTLS_MD_SHA384:
8233 return( MBEDTLS_SSL_HASH_SHA384 );
8234 case MBEDTLS_MD_SHA512:
8235 return( MBEDTLS_SSL_HASH_SHA512 );
8236#endif
8237 default:
8238 return( MBEDTLS_SSL_HASH_NONE );
8239 }
8240}
8241
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008242#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008243/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008244 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008245 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008246 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008247int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008248{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008250
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008251 if( ssl->conf->curve_list == NULL )
8252 return( -1 );
8253
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008254 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008255 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008256 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008257
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008258 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008259}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008260#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008261
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008262#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008263/*
8264 * Check if a hash proposed by the peer is in our list.
8265 * Return 0 if we're willing to use it, -1 otherwise.
8266 */
8267int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8268 mbedtls_md_type_t md )
8269{
8270 const int *cur;
8271
8272 if( ssl->conf->sig_hashes == NULL )
8273 return( -1 );
8274
8275 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8276 if( *cur == (int) md )
8277 return( 0 );
8278
8279 return( -1 );
8280}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008281#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008283#if defined(MBEDTLS_X509_CRT_PARSE_C)
8284int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8285 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008286 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008287 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008288{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008289 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008291 int usage = 0;
8292#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008294 const char *ext_oid;
8295 size_t ext_len;
8296#endif
8297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008298#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8299 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008300 ((void) cert);
8301 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008302 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008303#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8306 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008307 {
8308 /* Server part of the key exchange */
8309 switch( ciphersuite->key_exchange )
8310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311 case MBEDTLS_KEY_EXCHANGE_RSA:
8312 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008313 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008314 break;
8315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8317 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8318 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8319 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008320 break;
8321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8323 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008324 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008325 break;
8326
8327 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328 case MBEDTLS_KEY_EXCHANGE_NONE:
8329 case MBEDTLS_KEY_EXCHANGE_PSK:
8330 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8331 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008332 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008333 usage = 0;
8334 }
8335 }
8336 else
8337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008338 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8339 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008340 }
8341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008343 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008344 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008345 ret = -1;
8346 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008347#else
8348 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008349#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8352 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8355 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008356 }
8357 else
8358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8360 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008361 }
8362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008363 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008364 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008365 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008366 ret = -1;
8367 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008368#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008369
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008370 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008371}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008373
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008374/*
8375 * Convert version numbers to/from wire format
8376 * and, for DTLS, to/from TLS equivalent.
8377 *
8378 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008379 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008380 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8381 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008384 unsigned char ver[2] )
8385{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008386#if defined(MBEDTLS_SSL_PROTO_DTLS)
8387 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008389 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008390 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8391
8392 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8393 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8394 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008395 else
8396#else
8397 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008398#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008399 {
8400 ver[0] = (unsigned char) major;
8401 ver[1] = (unsigned char) minor;
8402 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008403}
8404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008406 const unsigned char ver[2] )
8407{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408#if defined(MBEDTLS_SSL_PROTO_DTLS)
8409 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008410 {
8411 *major = 255 - ver[0] + 2;
8412 *minor = 255 - ver[1] + 1;
8413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008415 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8416 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008417 else
8418#else
8419 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008420#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008421 {
8422 *major = ver[0];
8423 *minor = ver[1];
8424 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008425}
8426
Simon Butcher99000142016-10-13 17:21:01 +01008427int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8428{
8429#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8430 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8431 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8432
8433 switch( md )
8434 {
8435#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8436#if defined(MBEDTLS_MD5_C)
8437 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008438 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008439#endif
8440#if defined(MBEDTLS_SHA1_C)
8441 case MBEDTLS_SSL_HASH_SHA1:
8442 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8443 break;
8444#endif
8445#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8446#if defined(MBEDTLS_SHA512_C)
8447 case MBEDTLS_SSL_HASH_SHA384:
8448 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8449 break;
8450#endif
8451#if defined(MBEDTLS_SHA256_C)
8452 case MBEDTLS_SSL_HASH_SHA256:
8453 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8454 break;
8455#endif
8456 default:
8457 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8458 }
8459
8460 return 0;
8461#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8462 (void) ssl;
8463 (void) md;
8464
8465 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8466#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8467}
8468
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008469#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8470 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8471int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8472 unsigned char *output,
8473 unsigned char *data, size_t data_len )
8474{
8475 int ret = 0;
8476 mbedtls_md5_context mbedtls_md5;
8477 mbedtls_sha1_context mbedtls_sha1;
8478
8479 mbedtls_md5_init( &mbedtls_md5 );
8480 mbedtls_sha1_init( &mbedtls_sha1 );
8481
8482 /*
8483 * digitally-signed struct {
8484 * opaque md5_hash[16];
8485 * opaque sha_hash[20];
8486 * };
8487 *
8488 * md5_hash
8489 * MD5(ClientHello.random + ServerHello.random
8490 * + ServerParams);
8491 * sha_hash
8492 * SHA(ClientHello.random + ServerHello.random
8493 * + ServerParams);
8494 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008495 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008496 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008498 goto exit;
8499 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008500 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008501 ssl->handshake->randbytes, 64 ) ) != 0 )
8502 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008504 goto exit;
8505 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008506 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008507 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008508 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_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_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008512 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008513 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008514 goto exit;
8515 }
8516
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008517 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008518 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008519 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008520 goto exit;
8521 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008522 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008523 ssl->handshake->randbytes, 64 ) ) != 0 )
8524 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008525 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008526 goto exit;
8527 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008528 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008529 data_len ) ) != 0 )
8530 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008531 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008532 goto exit;
8533 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008534 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008535 output + 16 ) ) != 0 )
8536 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008537 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008538 goto exit;
8539 }
8540
8541exit:
8542 mbedtls_md5_free( &mbedtls_md5 );
8543 mbedtls_sha1_free( &mbedtls_sha1 );
8544
8545 if( ret != 0 )
8546 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8547 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8548
8549 return( ret );
8550
8551}
8552#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
8553 MBEDTLS_SSL_PROTO_TLS1_1 */
8554
8555#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8556 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8557int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8558 unsigned char *output,
8559 unsigned char *data, size_t data_len,
8560 mbedtls_md_type_t md_alg )
8561{
8562 int ret = 0;
8563 mbedtls_md_context_t ctx;
8564 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8565
8566 mbedtls_md_init( &ctx );
8567
8568 /*
8569 * digitally-signed struct {
8570 * opaque client_random[32];
8571 * opaque server_random[32];
8572 * ServerDHParams params;
8573 * };
8574 */
8575 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8576 {
8577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8578 goto exit;
8579 }
8580 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8581 {
8582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8583 goto exit;
8584 }
8585 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8586 {
8587 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8588 goto exit;
8589 }
8590 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8591 {
8592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8593 goto exit;
8594 }
8595 if( ( ret = mbedtls_md_finish( &ctx, output ) ) != 0 )
8596 {
8597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8598 goto exit;
8599 }
8600
8601exit:
8602 mbedtls_md_free( &ctx );
8603
8604 if( ret != 0 )
8605 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8606 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8607
8608 return( ret );
8609}
8610#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
8611 MBEDTLS_SSL_PROTO_TLS1_2 */
8612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613#endif /* MBEDTLS_SSL_TLS_C */