blob: 725e9e7ef2eca6af35843838ec87e4dabe9e75c2 [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)
Rodrigo Dias Correa375366a2020-11-10 01:38:00 -0300502static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503static 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 );
Rodrigo Dias Correa375366a2020-11-10 01:38:00 -0300509static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *, unsigned char * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510static 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 );
Rodrigo Dias Correa375366a2020-11-10 01:38:00 -0300515static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516static 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)
Rodrigo Dias Correa375366a2020-11-10 01:38:00 -03001014void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char *hash )
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)
Rodrigo Dias Correa375366a2020-11-10 01:38:00 -03001063void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char *hash )
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)
Rodrigo Dias Correa375366a2020-11-10 01:38:00 -03001091void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char *hash )
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)
Rodrigo Dias Correa375366a2020-11-10 01:38:00 -03001112void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char *hash )
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é-Gonnard0098e7d2014-10-28 13:08:59 +01001306/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001307 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001310{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001312 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001316 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1319 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001320 }
1321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001325 ssl->out_msg, ssl->out_msglen );
1326
Hanno Beckerd33f1ca2017-09-18 10:55:31 +01001327 if( ssl->out_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
1328 {
Hanno Becker184f6752017-10-04 13:47:33 +01001329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1330 (unsigned) ssl->out_msglen,
Hanno Beckerd33f1ca2017-09-18 10:55:31 +01001331 MBEDTLS_SSL_MAX_CONTENT_LEN ) );
1332 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1333 }
1334
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001336 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001338#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 if( mode == MBEDTLS_MODE_STREAM ||
1340 ( mode == MBEDTLS_MODE_CBC
1341#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1342 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001343#endif
1344 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346#if defined(MBEDTLS_SSL_PROTO_SSL3)
1347 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001348 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001349 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001350
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001351 ssl_mac( &ssl->transform_out->md_ctx_enc,
1352 ssl->transform_out->mac_enc,
1353 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001354 ssl->out_ctr, ssl->out_msgtype,
1355 mac );
1356
1357 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001358 }
1359 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001360#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1362 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1363 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001364 {
Hanno Becker992b6872017-11-09 18:57:39 +00001365 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1368 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1369 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1370 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001371 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001372 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001374
1375 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001376 }
1377 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001378#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1381 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001382 }
1383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001385 ssl->out_msg + ssl->out_msglen,
1386 ssl->transform_out->maclen );
1387
1388 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001389 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001390 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001391#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001393 /*
1394 * Encrypt
1395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1397 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001398 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001399 int ret;
1400 size_t olen = 0;
1401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 "including %d bytes of padding",
1404 ssl->out_msglen, 0 ) );
1405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001407 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001408 ssl->transform_out->ivlen,
1409 ssl->out_msg, ssl->out_msglen,
1410 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001413 return( ret );
1414 }
1415
1416 if( ssl->out_msglen != olen )
1417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1419 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001420 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 }
Paul Bakker68884e32013-01-07 18:20:04 +01001422 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1424#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1425 if( mode == MBEDTLS_MODE_GCM ||
1426 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001427 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001428 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001429 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001430 unsigned char *enc_msg;
1431 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001432 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001434
Paul Bakkerca4ab492012-04-18 14:23:57 +00001435 memcpy( add_data, ssl->out_ctr, 8 );
1436 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001438 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001439 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1440 add_data[12] = ssl->out_msglen & 0xFF;
1441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakkerca4ab492012-04-18 14:23:57 +00001443 add_data, 13 );
1444
Paul Bakker68884e32013-01-07 18:20:04 +01001445 /*
1446 * Generate IV
1447 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001448 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1449 {
1450 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001453 }
1454
1455 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1456 ssl->out_ctr, 8 );
1457 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001460 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001461
Paul Bakker68884e32013-01-07 18:20:04 +01001462 /*
1463 * Fix pointer positions and message length with added IV
1464 */
1465 enc_msg = ssl->out_msg;
1466 enc_msglen = ssl->out_msglen;
1467 ssl->out_msglen += ssl->transform_out->ivlen -
1468 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker68884e32013-01-07 18:20:04 +01001471 "including %d bytes of padding",
1472 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001473
Paul Bakker68884e32013-01-07 18:20:04 +01001474 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001475 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 if( ( ret = mbedtls_cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001478 ssl->transform_out->iv_enc,
1479 ssl->transform_out->ivlen,
1480 add_data, 13,
1481 enc_msg, enc_msglen,
1482 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001483 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001486 return( ret );
1487 }
1488
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001489 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1492 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001493 }
1494
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001495 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001496 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001499 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001500 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001502#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001505 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001506 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001507 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001508
Paul Bakker48916f92012-09-16 19:57:18 +00001509 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1510 ssl->transform_out->ivlen;
1511 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001512 padlen = 0;
1513
1514 for( i = 0; i <= padlen; i++ )
1515 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1516
1517 ssl->out_msglen += padlen + 1;
1518
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001519 enc_msglen = ssl->out_msglen;
1520 enc_msg = ssl->out_msg;
1521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001523 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001524 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1525 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001528 {
1529 /*
1530 * Generate IV
1531 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001532 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001533 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001534 if( ret != 0 )
1535 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001536
Paul Bakker92be97b2013-01-02 17:30:03 +01001537 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001538 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001539
1540 /*
1541 * Fix pointer positions and message length with added IV
1542 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001543 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001544 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001545 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001546 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001550 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001551 ssl->out_msglen, ssl->transform_out->ivlen,
1552 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001555 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001556 ssl->transform_out->ivlen,
1557 enc_msg, enc_msglen,
1558 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001561 return( ret );
1562 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001563
Paul Bakkercca5b812013-08-31 17:40:26 +02001564 if( enc_msglen != olen )
1565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1567 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001568 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1571 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001572 {
1573 /*
1574 * Save IV in SSL3 and TLS1
1575 */
1576 memcpy( ssl->transform_out->iv_enc,
1577 ssl->transform_out->cipher_ctx_enc.iv,
1578 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001579 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001580#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001583 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001584 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001585 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1586
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001587 /*
1588 * MAC(MAC_write_key, seq_num +
1589 * TLSCipherText.type +
1590 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001591 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001592 * IV + // except for TLS 1.0
1593 * ENC(content + padding + padding_length));
1594 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001595 unsigned char pseudo_hdr[13];
1596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001598
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001599 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1600 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001601 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1602 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1607 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001608 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001609 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001611
Hanno Becker3d8c9072018-01-05 16:24:22 +00001612 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1613 ssl->transform_out->maclen );
1614
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001615 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001616 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001617 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001619 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001620 else
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001621#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001625 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001626
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001627 /* Make extra sure authentication was performed, exactly once */
1628 if( auth_done != 1 )
1629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1631 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001632 }
1633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001635
1636 return( 0 );
1637}
1638
Manuel Pégourié-Gonnard41df0f22020-07-28 11:35:39 +02001639#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001640/*
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001641 * Constant-flow conditional memcpy:
1642 * - if c1 == c2, equivalent to memcpy(dst, src, len),
1643 * - otherwise, a no-op,
1644 * but with execution flow independent of the values of c1 and c2.
1645 *
1646 * Use only bit operations to avoid branches that could be used by some
1647 * compilers on some platforms to translate comparison operators.
1648 */
Manuel Pégourié-Gonnard2da9a542020-07-28 11:56:05 +02001649static void mbedtls_ssl_cf_memcpy_if_eq( unsigned char *dst,
1650 const unsigned char *src,
1651 size_t len,
1652 size_t c1, size_t c2 )
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001653{
1654 /* diff = 0 if c1 == c2, non-zero otherwise */
1655 const size_t diff = c1 ^ c2;
1656
1657 /* MSVC has a warning about unary minus on unsigned integer types,
1658 * but this is well-defined and precisely what we want to do here. */
1659#if defined(_MSC_VER)
1660#pragma warning( push )
1661#pragma warning( disable : 4146 )
1662#endif
1663
Manuel Pégourié-Gonnard2f484bd2020-07-28 11:57:25 +02001664 /* diff_msb's most significant bit is equal to c1 != c2 */
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001665 const size_t diff_msb = ( diff | -diff );
1666
1667 /* diff1 = c1 != c2 */
1668 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
1669
1670 /* mask = c1 != c2 ? 0xff : 0x00 */
Manuel Pégourié-Gonnard2f484bd2020-07-28 11:57:25 +02001671 const unsigned char mask = (unsigned char) -diff1;
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001672
1673#if defined(_MSC_VER)
1674#pragma warning( pop )
1675#endif
1676
1677 /* dst[i] = c1 != c2 ? dst[i] : src[i] */
Manuel Pégourié-Gonnarde05e5762020-07-29 10:04:36 +02001678 size_t i;
1679 for( i = 0; i < len; i++ )
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001680 dst[i] = ( dst[i] & mask ) | ( src[i] & ~mask );
1681}
1682
1683/*
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001684 * Compute HMAC of variable-length data with constant flow.
Manuel Pégourié-Gonnard7cf5ebc2020-07-29 12:54:04 +02001685 *
1686 * Only works with MD-5, SHA-1, SHA-256 and SHA-384.
1687 * (Otherwise, computation of block_size needs to be adapted.)
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001688 */
1689int mbedtls_ssl_cf_hmac(
1690 mbedtls_md_context_t *ctx,
1691 const unsigned char *add_data, size_t add_data_len,
1692 const unsigned char *data, size_t data_len_secret,
1693 size_t min_data_len, size_t max_data_len,
1694 unsigned char *output )
1695{
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001696 /*
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001697 * This function breaks the HMAC abstraction and uses the md_clone()
1698 * extension to the MD API in order to get constant-flow behaviour.
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001699 *
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001700 * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
Manuel Pégourié-Gonnardec956b12020-07-28 11:42:31 +02001701 * concatenation, and okey/ikey are the XOR of the key with some fixed bit
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001702 * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001703 *
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001704 * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
1705 * minlen, then cloning the context, and for each byte up to maxlen
1706 * finishing up the hash computation, keeping only the correct result.
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001707 *
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001708 * Then we only need to compute HASH(okey + inner_hash) and we're done.
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001709 */
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001710 const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info );
Manuel Pégourié-Gonnardec956b12020-07-28 11:42:31 +02001711 /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5,
1712 * all of which have the same block size except SHA-384. */
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001713 const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
Manuel Pégourié-Gonnardc9ef5a22020-07-28 11:45:02 +02001714 const unsigned char * const ikey = ctx->hmac_ctx;
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001715 const unsigned char * const okey = ikey + block_size;
1716 const size_t hash_size = mbedtls_md_get_size( ctx->md_info );
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001717
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001718 unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
1719 mbedtls_md_context_t aux;
1720 size_t offset;
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001721 int ret;
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001722
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001723 mbedtls_md_init( &aux );
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001724
1725#define MD_CHK( func_call ) \
1726 do { \
1727 ret = (func_call); \
1728 if( ret != 0 ) \
1729 goto cleanup; \
1730 } while( 0 )
1731
1732 MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) );
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001733
1734 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
1735 * so we can start directly with the message */
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001736 MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) );
1737 MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) );
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001738
1739 /* For each possible length, compute the hash up to that point */
1740 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001741 {
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001742 MD_CHK( mbedtls_md_clone( &aux, ctx ) );
1743 MD_CHK( mbedtls_md_finish( &aux, aux_out ) );
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001744 /* Keep only the correct inner_hash in the output buffer */
1745 mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size,
1746 offset, data_len_secret );
1747
1748 if( offset < max_data_len )
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001749 MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) );
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001750 }
1751
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001752 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001753 MD_CHK( mbedtls_md_starts( ctx ) );
1754 MD_CHK( mbedtls_md_update( ctx, okey, block_size ) );
1755 MD_CHK( mbedtls_md_update( ctx, output, hash_size ) );
1756 MD_CHK( mbedtls_md_finish( ctx, output ) );
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001757
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001758 /* Done, get ready for next time */
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001759 MD_CHK( mbedtls_md_hmac_reset( ctx ) );
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001760
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001761#undef MD_CHK
1762
1763cleanup:
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001764 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001765 return( ret );
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001766}
Manuel Pégourié-Gonnard3b490a02020-08-25 11:18:11 +02001767
1768/*
1769 * Constant-flow memcpy from variable position in buffer.
1770 * - functionally equivalent to memcpy(dst, src + offset_secret, len)
Manuel Pégourié-Gonnard520e78b2020-08-25 11:43:10 +02001771 * - but with execution flow independent from the value of offset_secret.
Manuel Pégourié-Gonnard3b490a02020-08-25 11:18:11 +02001772 */
1773void mbedtls_ssl_cf_memcpy_offset( unsigned char *dst,
1774 const unsigned char *src_base,
1775 size_t offset_secret,
1776 size_t offset_min, size_t offset_max,
1777 size_t len )
1778{
Manuel Pégourié-Gonnard520e78b2020-08-25 11:43:10 +02001779 size_t offset;
1780
1781 for( offset = offset_min; offset <= offset_max; offset++ )
1782 {
1783 mbedtls_ssl_cf_memcpy_if_eq( dst, src_base + offset, len,
1784 offset, offset_secret );
1785 }
Manuel Pégourié-Gonnard3b490a02020-08-25 11:18:11 +02001786}
Manuel Pégourié-Gonnard41df0f22020-07-28 11:35:39 +02001787#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001790{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001791 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001793 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001794#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001795 size_t padlen = 0, correct = 1;
1796#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001799
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001800 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1803 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001804 }
1805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001807
Paul Bakker48916f92012-09-16 19:57:18 +00001808 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001811 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001813 }
1814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1816 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001817 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001818 int ret;
1819 size_t olen = 0;
1820
Paul Bakker68884e32013-01-07 18:20:04 +01001821 padlen = 0;
1822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001824 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001825 ssl->transform_in->ivlen,
1826 ssl->in_msg, ssl->in_msglen,
1827 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001830 return( ret );
1831 }
1832
1833 if( ssl->in_msglen != olen )
1834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1836 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001837 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001838 }
Paul Bakker68884e32013-01-07 18:20:04 +01001839 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1841#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1842 if( mode == MBEDTLS_MODE_GCM ||
1843 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001844 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001845 int ret;
1846 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001847 unsigned char *dec_msg;
1848 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001849 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001850 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001852 size_t explicit_iv_len = ssl->transform_in->ivlen -
1853 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001854
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001855 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001858 "+ taglen (%d)", ssl->in_msglen,
1859 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001861 }
1862 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1863
Paul Bakker68884e32013-01-07 18:20:04 +01001864 dec_msg = ssl->in_msg;
1865 dec_msg_result = ssl->in_msg;
1866 ssl->in_msglen = dec_msglen;
1867
1868 memcpy( add_data, ssl->in_ctr, 8 );
1869 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001871 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001872 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1873 add_data[12] = ssl->in_msglen & 0xFF;
1874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakker68884e32013-01-07 18:20:04 +01001876 add_data, 13 );
1877
1878 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1879 ssl->in_iv,
1880 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
Paul Bakker68884e32013-01-07 18:20:04 +01001883 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001885
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001886 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001887 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001890 ssl->transform_in->iv_dec,
1891 ssl->transform_in->ivlen,
1892 add_data, 13,
1893 dec_msg, dec_msglen,
1894 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001895 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1900 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001901
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001902 return( ret );
1903 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001904 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001905
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001906 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1909 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001910 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001911 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001912 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001914#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 {
Paul Bakker45829992013-01-03 14:52:21 +01001917 /*
1918 * Decrypt and check the padding
1919 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001920 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001921 unsigned char *dec_msg;
1922 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001923 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001924 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001925 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001926
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 /*
Paul Bakker45829992013-01-03 14:52:21 +01001928 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1931 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001932 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001933#endif
Paul Bakker45829992013-01-03 14:52:21 +01001934
1935 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1936 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001939 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1940 ssl->transform_in->ivlen,
1941 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001943 }
1944
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001945 dec_msglen = ssl->in_msglen;
1946 dec_msg = ssl->in_msg;
1947 dec_msg_result = ssl->in_msg;
1948
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001949 /*
1950 * Authenticate before decrypt if enabled
1951 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1953 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001954 {
Hanno Becker992b6872017-11-09 18:57:39 +00001955 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001956 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001959
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001960 dec_msglen -= ssl->transform_in->maclen;
1961 ssl->in_msglen -= ssl->transform_in->maclen;
1962
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001963 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1964 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1965 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1966 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1971 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001972 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001973 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001977 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001978 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001979 ssl->transform_in->maclen );
1980
Hanno Becker992b6872017-11-09 18:57:39 +00001981 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1982 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001987 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001988 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001989 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001991
1992 /*
1993 * Check length sanity
1994 */
1995 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001998 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002000 }
2001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002003 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002004 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002007 {
Paul Bakker48916f92012-09-16 19:57:18 +00002008 dec_msglen -= ssl->transform_in->ivlen;
2009 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002010
Paul Bakker48916f92012-09-16 19:57:18 +00002011 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002012 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002013 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002017 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002018 ssl->transform_in->ivlen,
2019 dec_msg, dec_msglen,
2020 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002023 return( ret );
2024 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002025
Paul Bakkercca5b812013-08-31 17:40:26 +02002026 if( dec_msglen != olen )
2027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2029 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002030 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2033 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002034 {
2035 /*
2036 * Save IV in SSL3 and TLS1
2037 */
2038 memcpy( ssl->transform_in->iv_dec,
2039 ssl->transform_in->cipher_ctx_dec.iv,
2040 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
2044 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002045
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002046 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002047 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049#if defined(MBEDTLS_SSL_DEBUG_ALL)
2050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002051 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002052#endif
Paul Bakker45829992013-01-03 14:52:21 +01002053 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002054 correct = 0;
2055 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057#if defined(MBEDTLS_SSL_PROTO_SSL3)
2058 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 {
Paul Bakker48916f92012-09-16 19:57:18 +00002060 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062#if defined(MBEDTLS_SSL_DEBUG_ALL)
2063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002064 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002065 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002066#endif
Paul Bakker45829992013-01-03 14:52:21 +01002067 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 }
2069 }
2070 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2072#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2073 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2074 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 {
2076 /*
Paul Bakker45829992013-01-03 14:52:21 +01002077 * TLSv1+: always check the padding up to the first failure
2078 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002080 size_t pad_count = 0, real_count = 1;
Angus Gratton1ba8e912018-06-19 15:57:50 +10002081 size_t padding_idx = ssl->in_msglen - padlen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002082
Paul Bakker956c9e02013-12-19 14:42:28 +01002083 /*
2084 * Padding is guaranteed to be incorrect if:
Angus Gratton1ba8e912018-06-19 15:57:50 +10002085 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002086 *
Angus Gratton1ba8e912018-06-19 15:57:50 +10002087 * 2. padding_idx > MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002088 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002089 *
2090 * In both cases we reset padding_idx to a safe value (0) to
2091 * prevent out-of-buffer reads.
2092 */
Angus Gratton1ba8e912018-06-19 15:57:50 +10002093 correct &= ( padlen <= ssl->in_msglen );
2094 correct &= ( padding_idx <= MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002095 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002096
2097 padding_idx *= correct;
2098
Angus Gratton1ba8e912018-06-19 15:57:50 +10002099 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002100 {
Angus Gratton1ba8e912018-06-19 15:57:50 +10002101 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002102 pad_count += real_count *
2103 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2104 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002105
2106 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002109 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002111#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002112 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002114 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2116 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2119 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002120 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002121
2122 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002124 else
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02002125#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2128 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002129 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002130
Manuel Pégourié-Gonnard7c344322018-07-10 11:15:36 +02002131#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002133 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard7c344322018-07-10 11:15:36 +02002134#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002135
2136 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002137 * Authenticate if not done yet.
2138 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002140#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002141 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002142 {
Hanno Becker992b6872017-11-09 18:57:39 +00002143 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnardbf7a49e2020-08-25 11:07:25 +02002144 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002145
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002146 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002147
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002148 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2149 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151#if defined(MBEDTLS_SSL_PROTO_SSL3)
2152 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002153 {
2154 ssl_mac( &ssl->transform_in->md_ctx_dec,
2155 ssl->transform_in->mac_dec,
2156 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002157 ssl->in_ctr, ssl->in_msgtype,
2158 mac_expect );
Manuel Pégourié-Gonnardbf7a49e2020-08-25 11:07:25 +02002159 memcpy( mac_peer, ssl->in_msg + ssl->in_msglen,
2160 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002161 }
2162 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2164#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2165 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2166 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002167 {
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002168 int ret;
2169 unsigned char add_data[13];
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002170
2171 /*
2172 * The next two sizes are the minimum and maximum values of
2173 * in_msglen over all padlen values.
2174 *
2175 * They're independent of padlen, since we previously did
2176 * in_msglen -= padlen.
2177 *
2178 * Note that max_len + maclen is never more than the buffer
2179 * length, as we previously did in_msglen -= maclen too.
2180 */
2181 const size_t max_len = ssl->in_msglen + padlen;
2182 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2183
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002184 memcpy( add_data + 0, ssl->in_ctr, 8 );
2185 memcpy( add_data + 8, ssl->in_hdr, 3 );
2186 memcpy( add_data + 11, ssl->in_len, 2 );
2187
2188 ret = mbedtls_ssl_cf_hmac( &ssl->transform_in->md_ctx_dec,
2189 add_data, sizeof( add_data ),
2190 ssl->in_msg, ssl->in_msglen,
2191 min_len, max_len,
2192 mac_expect );
2193 if( ret != 0 )
Gilles Peskinebb07ca02018-06-06 17:23:31 +02002194 {
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002195 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
2196 return( ret );
Gilles Peskinebb07ca02018-06-06 17:23:31 +02002197 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002198
Manuel Pégourié-Gonnard3b490a02020-08-25 11:18:11 +02002199 mbedtls_ssl_cf_memcpy_offset( mac_peer, ssl->in_msg,
2200 ssl->in_msglen,
2201 min_len, max_len,
2202 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002203 }
2204 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2206 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2209 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002210 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002211
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002212#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002213 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
Manuel Pégourié-Gonnardbf7a49e2020-08-25 11:07:25 +02002214 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, ssl->transform_in->maclen );
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002215#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002216
Manuel Pégourié-Gonnardbf7a49e2020-08-25 11:07:25 +02002217 if( mbedtls_ssl_safer_memcmp( mac_peer, mac_expect,
Hanno Becker992b6872017-11-09 18:57:39 +00002218 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220#if defined(MBEDTLS_SSL_DEBUG_ALL)
2221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002222#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002223 correct = 0;
2224 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002225 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002226 }
Hanno Beckerca31b472018-10-17 14:43:14 +01002227
2228 /*
2229 * Finally check the correct flag
2230 */
2231 if( correct == 0 )
2232 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002233#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002234
2235 /* Make extra sure authentication was performed, exactly once */
2236 if( auth_done != 1 )
2237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2239 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002240 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002241
2242 if( ssl->in_msglen == 0 )
2243 {
Angus Grattonb91cb6e2018-06-19 15:58:22 +10002244#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2245 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2246 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2247 {
2248 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2250 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2251 }
2252#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2253
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 ssl->nb_zero++;
2255
2256 /*
2257 * Three or more empty messages may be a DoS attack
2258 * (excessive CPU consumption).
2259 */
2260 if( ssl->nb_zero > 3 )
2261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002263 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 }
2266 }
2267 else
2268 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002271 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002272 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002273 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002274 }
2275 else
2276#endif
2277 {
2278 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2279 if( ++ssl->in_ctr[i - 1] != 0 )
2280 break;
2281
2282 /* The loop goes to its end iff the counter is wrapping */
2283 if( i == ssl_ep_len( ssl ) )
2284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2286 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002287 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002288 }
2289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002291
2292 return( 0 );
2293}
2294
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002295#undef MAC_NONE
2296#undef MAC_PLAINTEXT
2297#undef MAC_CIPHERTEXT
2298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002300/*
2301 * Compression/decompression functions
2302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002304{
2305 int ret;
2306 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002307 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002308 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002309 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002312
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002313 if( len_pre == 0 )
2314 return( 0 );
2315
Paul Bakker2770fbd2012-07-03 13:30:23 +00002316 memcpy( msg_pre, ssl->out_msg, len_pre );
2317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002319 ssl->out_msglen ) );
2320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002322 ssl->out_msg, ssl->out_msglen );
2323
Paul Bakker48916f92012-09-16 19:57:18 +00002324 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2325 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2326 ssl->transform_out->ctx_deflate.next_out = msg_post;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002327 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002328
Paul Bakker48916f92012-09-16 19:57:18 +00002329 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002330 if( ret != Z_OK )
2331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2333 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002334 }
2335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 ssl->out_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002337 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002340 ssl->out_msglen ) );
2341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002343 ssl->out_msg, ssl->out_msglen );
2344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002346
2347 return( 0 );
2348}
2349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002351{
2352 int ret;
2353 unsigned char *msg_post = ssl->in_msg;
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002354 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002355 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002356 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002359
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002360 if( len_pre == 0 )
2361 return( 0 );
2362
Paul Bakker2770fbd2012-07-03 13:30:23 +00002363 memcpy( msg_pre, ssl->in_msg, len_pre );
2364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002366 ssl->in_msglen ) );
2367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002369 ssl->in_msg, ssl->in_msglen );
2370
Paul Bakker48916f92012-09-16 19:57:18 +00002371 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2372 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2373 ssl->transform_in->ctx_inflate.next_out = msg_post;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002374 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002375 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002376
Paul Bakker48916f92012-09-16 19:57:18 +00002377 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002378 if( ret != Z_OK )
2379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2381 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002382 }
2383
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002384 ssl->in_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002385 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002388 ssl->in_msglen ) );
2389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002391 ssl->in_msg, ssl->in_msglen );
2392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002394
2395 return( 0 );
2396}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2400static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402#if defined(MBEDTLS_SSL_PROTO_DTLS)
2403static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002404{
2405 /* If renegotiation is not enforced, retransmit until we would reach max
2406 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002407 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002408 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002409 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002410 unsigned char doublings = 1;
2411
2412 while( ratio != 0 )
2413 {
2414 ++doublings;
2415 ratio >>= 1;
2416 }
2417
2418 if( ++ssl->renego_records_seen > doublings )
2419 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002421 return( 0 );
2422 }
2423 }
2424
2425 return( ssl_write_hello_request( ssl ) );
2426}
2427#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002429
Paul Bakker5121ce52009-01-03 21:22:43 +00002430/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002431 * Fill the input message buffer by appending data to it.
2432 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002433 *
2434 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2435 * available (from this read and/or a previous one). Otherwise, an error code
2436 * is returned (possibly EOF or WANT_READ).
2437 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002438 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2439 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2440 * since we always read a whole datagram at once.
2441 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002442 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002443 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002446{
Paul Bakker23986e52011-04-24 08:57:21 +00002447 int ret;
2448 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002451
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002452 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002455 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002457 }
2458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 if( nb_want > MBEDTLS_SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2462 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002463 }
2464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002466 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002468 uint32_t timeout;
2469
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002470 /* Just to be sure */
2471 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2472 {
2473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2474 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2476 }
2477
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002478 /*
2479 * The point is, we need to always read a full datagram at once, so we
2480 * sometimes read more then requested, and handle the additional data.
2481 * It could be the rest of the current record (while fetching the
2482 * header) and/or some other records in the same datagram.
2483 */
2484
2485 /*
2486 * Move to the next record in the already read datagram if applicable
2487 */
2488 if( ssl->next_record_offset != 0 )
2489 {
2490 if( ssl->in_left < ssl->next_record_offset )
2491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2493 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002494 }
2495
2496 ssl->in_left -= ssl->next_record_offset;
2497
2498 if( ssl->in_left != 0 )
2499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002501 ssl->next_record_offset ) );
2502 memmove( ssl->in_hdr,
2503 ssl->in_hdr + ssl->next_record_offset,
2504 ssl->in_left );
2505 }
2506
2507 ssl->next_record_offset = 0;
2508 }
2509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002512
2513 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002514 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002515 */
2516 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002519 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002520 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002521
2522 /*
Antonin Décimo8fd91562019-01-23 15:24:37 +01002523 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002524 * are not at the beginning of a new record, the caller did something
2525 * wrong.
2526 */
2527 if( ssl->in_left != 0 )
2528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2530 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002531 }
2532
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002533 /*
2534 * Don't even try to read if time's out already.
2535 * This avoids by-passing the timer when repeatedly receiving messages
2536 * that will end up being dropped.
2537 */
2538 if( ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002539 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002540 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 len = MBEDTLS_SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002545 timeout = ssl->handshake->retransmit_timeout;
2546 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002547 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002550
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002551 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002552 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2553 timeout );
2554 else
2555 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002558
2559 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002561 }
2562
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002563 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002566 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002569 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002570 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002573 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002574 }
2575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002579 return( ret );
2580 }
2581
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002582 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002583 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002585 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002587 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002588 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002591 return( ret );
2592 }
2593
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002594 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002595 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002597 }
2598
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 if( ret < 0 )
2600 return( ret );
2601
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002602 ssl->in_left = ret;
2603 }
2604 else
2605#endif
2606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002608 ssl->in_left, nb_want ) );
2609
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002610 while( ssl->in_left < nb_want )
2611 {
2612 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002613
2614 if( ssl_check_timer( ssl ) != 0 )
2615 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2616 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002617 {
2618 if( ssl->f_recv_timeout != NULL )
2619 {
2620 ret = ssl->f_recv_timeout( ssl->p_bio,
2621 ssl->in_hdr + ssl->in_left, len,
2622 ssl->conf->read_timeout );
2623 }
2624 else
2625 {
2626 ret = ssl->f_recv( ssl->p_bio,
2627 ssl->in_hdr + ssl->in_left, len );
2628 }
2629 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002632 ssl->in_left, nb_want ) );
2633 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002634
2635 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002637
2638 if( ret < 0 )
2639 return( ret );
2640
makise-homura329fe7e2020-08-24 18:39:56 +03002641 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad1603b11af862018-03-19 07:18:13 -07002642 {
Jaeden Amerob5f53b12018-04-03 12:09:45 +01002643 MBEDTLS_SSL_DEBUG_MSG( 1,
2644 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160329ed80f2018-04-02 07:34:26 -07002645 ret, (unsigned long)len ) );
mohammad1603b11af862018-03-19 07:18:13 -07002646 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2647 }
2648
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002649 ssl->in_left += ret;
2650 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002651 }
2652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
2655 return( 0 );
2656}
2657
2658/*
2659 * Flush any data not yet written
2660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002662{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002663 int ret;
2664 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002667
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002668 if( ssl->f_send == NULL )
2669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002671 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002673 }
2674
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002675 /* Avoid incrementing counter if data is flushed */
2676 if( ssl->out_left == 0 )
2677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002679 return( 0 );
2680 }
2681
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 while( ssl->out_left > 0 )
2683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2685 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) +
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002688 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002689 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
2693 if( ret <= 0 )
2694 return( ret );
2695
makise-homura329fe7e2020-08-24 18:39:56 +03002696 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16036085c722018-02-22 04:29:04 -08002697 {
Jaeden Amerob5f53b12018-04-03 12:09:45 +01002698 MBEDTLS_SSL_DEBUG_MSG( 1,
2699 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160329ed80f2018-04-02 07:34:26 -07002700 ret, (unsigned long)ssl->out_left ) );
mohammad16036085c722018-02-22 04:29:04 -08002701 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2702 }
2703
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 ssl->out_left -= ret;
2705 }
2706
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002707 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002708 if( ++ssl->out_ctr[i - 1] != 0 )
2709 break;
2710
2711 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002712 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2715 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002716 }
2717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
2720 return( 0 );
2721}
2722
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002723/*
2724 * Functions to handle the DTLS retransmission state machine
2725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002727/*
2728 * Append current handshake message to current outgoing flight
2729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002731{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 mbedtls_ssl_flight_item *msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002733
2734 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002735 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002736 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002739 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002740 }
2741
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002742 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002743 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002746 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002747 }
2748
2749 /* Copy current handshake message with headers */
2750 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2751 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002752 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002753 msg->next = NULL;
2754
2755 /* Append to the current flight */
2756 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002757 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002758 else
2759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002761 while( cur->next != NULL )
2762 cur = cur->next;
2763 cur->next = msg;
2764 }
2765
2766 return( 0 );
2767}
2768
2769/*
2770 * Free the current flight of handshake messages
2771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002773{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 mbedtls_ssl_flight_item *cur = flight;
2775 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002776
2777 while( cur != NULL )
2778 {
2779 next = cur->next;
2780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 mbedtls_free( cur->p );
2782 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002783
2784 cur = next;
2785 }
2786}
2787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2789static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002790#endif
2791
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002792/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002793 * Swap transform_out and out_ctr with the alternative ones
2794 */
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002795static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002796{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002798 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002799#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2800 int ret;
2801#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002802
2803 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002806 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002807 }
2808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002810
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002811 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002812 tmp_transform = ssl->transform_out;
2813 ssl->transform_out = ssl->handshake->alt_transform_out;
2814 ssl->handshake->alt_transform_out = tmp_transform;
2815
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002816 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002817 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2818 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2819 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002820
2821 /* Adjust to the newly activated transform */
2822 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002824 {
2825 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2826 ssl->transform_out->fixed_ivlen;
2827 }
2828 else
2829 ssl->out_msg = ssl->out_iv;
2830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2832 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2837 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002838 }
2839 }
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002840#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
2841
2842 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002843}
2844
2845/*
2846 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002847 *
2848 * Need to remember the current message in case flush_output returns
2849 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002850 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002851 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002853{
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002854 int ret;
2855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002861
2862 ssl->handshake->cur_msg = ssl->handshake->flight;
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002863 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
2864 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002867 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002868
2869 while( ssl->handshake->cur_msg != NULL )
2870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 mbedtls_ssl_flight_item *cur = ssl->handshake->cur_msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002872
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002873 /* Swap epochs before sending Finished: we can't do it after
2874 * sending ChangeCipherSpec, in case write returns WANT_READ.
2875 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2877 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002878 {
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002879 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
2880 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002881 }
2882
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002883 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002884 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002885 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002886
2887 ssl->handshake->cur_msg = cur->next;
2888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 MBEDTLS_SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002894 return( ret );
2895 }
2896 }
2897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2899 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002900 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002903 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2904 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002907
2908 return( 0 );
2909}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002910
2911/*
2912 * To be called when the last message of an incoming flight is received.
2913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002915{
2916 /* We won't need to resend that one any more */
2917 ssl_flight_free( ssl->handshake->flight );
2918 ssl->handshake->flight = NULL;
2919 ssl->handshake->cur_msg = NULL;
2920
2921 /* The next incoming flight will start with this msg_seq */
2922 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2923
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002924 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002925 ssl_set_timer( ssl, 0 );
2926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2928 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002931 }
2932 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002934}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002935
2936/*
2937 * To be called when the last message of an outgoing flight is send.
2938 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002940{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002941 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002942 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2945 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002948 }
2949 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002951}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002953
Paul Bakker5121ce52009-01-03 21:22:43 +00002954/*
2955 * Record layer functions
2956 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002957
2958/*
2959 * Write current record.
2960 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2961 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002963{
Nicholas Wilsonf0021642016-04-13 11:51:05 +01002964 int ret, done = 0, out_msg_type;
Paul Bakker23986e52011-04-24 08:57:21 +00002965 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002970 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002971 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002973 {
2974 ; /* Skip special handshake treatment when resending */
2975 }
2976 else
2977#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002979 {
Nicholas Wilsonf0021642016-04-13 11:51:05 +01002980 out_msg_type = ssl->out_msg[0];
2981
2982 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002983 ssl->handshake == NULL )
2984 {
2985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2986 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2987 }
2988
Paul Bakker5121ce52009-01-03 21:22:43 +00002989 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2990 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2991 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2992
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002993 /*
2994 * DTLS has additional fields in the Handshake layer,
2995 * between the length field and the actual payload:
2996 * uint16 message_seq;
2997 * uint24 fragment_offset;
2998 * uint24 fragment_length;
2999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003001 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003002 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003003 /* Make room for the additional DTLS fields */
Hanno Becker9648f8b2017-09-18 10:55:54 +01003004 if( MBEDTLS_SSL_MAX_CONTENT_LEN - ssl->out_msglen < 8 )
3005 {
3006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3007 "size %u, maximum %u",
3008 (unsigned) ( ssl->in_hslen - 4 ),
3009 (unsigned) ( MBEDTLS_SSL_MAX_CONTENT_LEN - 12 ) ) );
3010 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3011 }
3012
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003013 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003014 ssl->out_msglen += 8;
3015 len += 8;
3016
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003017 /* Write message_seq and update it, except for HelloRequest */
Nicholas Wilsonf0021642016-04-13 11:51:05 +01003018 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003019 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003020 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3021 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3022 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003023 }
3024 else
3025 {
3026 ssl->out_msg[4] = 0;
3027 ssl->out_msg[5] = 0;
3028 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003029
3030 /* We don't fragment, so frag_offset = 0 and frag_len = len */
3031 memset( ssl->out_msg + 6, 0x00, 3 );
3032 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003033 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003035
Nicholas Wilsonf0021642016-04-13 11:51:05 +01003036 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003037 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003038 }
3039
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003040 /* Save handshake and CCS messages for resending */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003042 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003043 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044 ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING &&
3045 ( ssl->out_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ||
3046 ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003047 {
3048 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003051 return( ret );
3052 }
3053 }
3054#endif
3055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003057 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003058 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003059 {
3060 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003063 return( ret );
3064 }
3065
3066 len = ssl->out_msglen;
3067 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3071 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075 ret = mbedtls_ssl_hw_record_write( ssl );
3076 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3079 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003080 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003081
3082 if( ret == 0 )
3083 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003084 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003086 if( !done )
3087 {
3088 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003090 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003091
3092 ssl->out_len[0] = (unsigned char)( len >> 8 );
3093 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003094
Paul Bakker48916f92012-09-16 19:57:18 +00003095 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003096 {
3097 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003100 return( ret );
3101 }
3102
3103 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003104 ssl->out_len[0] = (unsigned char)( len >> 8 );
3105 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003106 }
3107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00003109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00003111 "version = [%d:%d], msglen = %d",
3112 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003113 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3116 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003117 }
3118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003122 return( ret );
3123 }
3124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003126
3127 return( 0 );
3128}
3129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003131/*
3132 * Mark bits in bitmask (used for DTLS HS reassembly)
3133 */
3134static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3135{
3136 unsigned int start_bits, end_bits;
3137
3138 start_bits = 8 - ( offset % 8 );
3139 if( start_bits != 8 )
3140 {
3141 size_t first_byte_idx = offset / 8;
3142
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003143 /* Special case */
3144 if( len <= start_bits )
3145 {
3146 for( ; len != 0; len-- )
3147 mask[first_byte_idx] |= 1 << ( start_bits - len );
3148
3149 /* Avoid potential issues with offset or len becoming invalid */
3150 return;
3151 }
3152
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003153 offset += start_bits; /* Now offset % 8 == 0 */
3154 len -= start_bits;
3155
3156 for( ; start_bits != 0; start_bits-- )
3157 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3158 }
3159
3160 end_bits = len % 8;
3161 if( end_bits != 0 )
3162 {
3163 size_t last_byte_idx = ( offset + len ) / 8;
3164
3165 len -= end_bits; /* Now len % 8 == 0 */
3166
3167 for( ; end_bits != 0; end_bits-- )
3168 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3169 }
3170
3171 memset( mask + offset / 8, 0xFF, len / 8 );
3172}
3173
3174/*
3175 * Check that bitmask is full
3176 */
3177static int ssl_bitmask_check( unsigned char *mask, size_t len )
3178{
3179 size_t i;
3180
3181 for( i = 0; i < len / 8; i++ )
3182 if( mask[i] != 0xFF )
3183 return( -1 );
3184
3185 for( i = 0; i < len % 8; i++ )
3186 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3187 return( -1 );
3188
3189 return( 0 );
3190}
3191
3192/*
3193 * Reassemble fragmented DTLS handshake messages.
3194 *
3195 * Use a temporary buffer for reassembly, divided in two parts:
3196 * - the first holds the reassembled message (including handshake header),
3197 * - the second holds a bitmask indicating which parts of the message
3198 * (excluding headers) have been received so far.
3199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003201{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003202 unsigned char *msg, *bitmask;
3203 size_t frag_len, frag_off;
3204 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3205
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003206 if( ssl->handshake == NULL )
3207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3209 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003210 }
3211
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003212 /*
3213 * For first fragment, check size and allocate buffer
3214 */
3215 if( ssl->handshake->hs_msg == NULL )
3216 {
3217 size_t alloc_len;
3218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003220 msg_len ) );
3221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 if( ssl->in_hslen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3225 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003226 }
3227
3228 /* The bitmask needs one bit per byte of message excluding header */
3229 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3230
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003231 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003232 if( ssl->handshake->hs_msg == NULL )
3233 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003235 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003236 }
3237
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003238 /* Prepare final header: copy msg_type, length and message_seq,
3239 * then add standardised fragment_offset and fragment_length */
3240 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3241 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3242 memcpy( ssl->handshake->hs_msg + 9,
3243 ssl->handshake->hs_msg + 1, 3 );
3244 }
3245 else
3246 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003247 /* Make sure msg_type and length are consistent */
3248 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3251 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003252 }
3253 }
3254
3255 msg = ssl->handshake->hs_msg + 12;
3256 bitmask = msg + msg_len;
3257
3258 /*
3259 * Check and copy current fragment
3260 */
3261 frag_off = ( ssl->in_msg[6] << 16 ) |
3262 ( ssl->in_msg[7] << 8 ) |
3263 ssl->in_msg[8];
3264 frag_len = ( ssl->in_msg[9] << 16 ) |
3265 ( ssl->in_msg[10] << 8 ) |
3266 ssl->in_msg[11];
3267
3268 if( frag_off + frag_len > msg_len )
3269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003271 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003273 }
3274
3275 if( frag_len + 12 > ssl->in_msglen )
3276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003278 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003279 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003280 }
3281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003283 frag_off, frag_len ) );
3284
3285 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3286 ssl_bitmask_set( bitmask, frag_off, frag_len );
3287
3288 /*
3289 * Do we have the complete message by now?
3290 * If yes, finalize it, else ask to read the next record.
3291 */
3292 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003295 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003296 }
3297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003299
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003300 if( frag_len + 12 < ssl->in_msglen )
3301 {
3302 /*
3303 * We'got more handshake messages in the same record.
3304 * This case is not handled now because no know implementation does
3305 * that and it's hard to test, so we prefer to fail cleanly for now.
3306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3308 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003309 }
3310
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003311 if( ssl->in_left > ssl->next_record_offset )
3312 {
3313 /*
3314 * We've got more data in the buffer after the current record,
3315 * that we don't want to overwrite. Move it before writing the
3316 * reassembled message, and adjust in_left and next_record_offset.
3317 */
3318 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3319 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3320 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3321
3322 /* First compute and check new lengths */
3323 ssl->next_record_offset = new_remain - ssl->in_hdr;
3324 ssl->in_left = ssl->next_record_offset + remain_len;
3325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 if( ssl->in_left > MBEDTLS_SSL_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003327 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3330 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003331 }
3332
3333 memmove( new_remain, cur_remain, remain_len );
3334 }
3335
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003336 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3337
Hanno Beckerd82e0c02018-10-15 13:22:22 +01003338 mbedtls_zeroize( ssl->handshake->hs_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003340 ssl->handshake->hs_msg = NULL;
3341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003343 ssl->in_msg, ssl->in_hslen );
3344
3345 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003346}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003348
Simon Butcher99000142016-10-13 17:21:01 +01003349int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003350{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003354 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003355 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003356 }
3357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003359 ( ssl->in_msg[1] << 16 ) |
3360 ( ssl->in_msg[2] << 8 ) |
3361 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003364 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003365 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003368 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003369 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003370 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003371 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003372
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003373 /* ssl->handshake is NULL when receiving ClientHello for renego */
3374 if( ssl->handshake != NULL &&
3375 recv_msg_seq != ssl->handshake->in_msg_seq )
3376 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003377 /* Retransmit only on last message from previous flight, to avoid
3378 * too many retransmissions.
3379 * Besides, No sane server ever retransmits HelloVerifyRequest */
3380 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003384 "message_seq = %d, start_of_flight = %d",
3385 recv_msg_seq,
3386 ssl->handshake->in_flight_start_seq ) );
3387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003391 return( ret );
3392 }
3393 }
3394 else
3395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003397 "message_seq = %d, expected = %d",
3398 recv_msg_seq,
3399 ssl->handshake->in_msg_seq ) );
3400 }
3401
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003402 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003403 }
3404 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003405
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003406 /* Reassemble if current message is fragmented or reassembly is
3407 * already in progress */
3408 if( ssl->in_msglen < ssl->in_hslen ||
3409 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3410 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3411 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003414
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003415 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003418 return( ret );
3419 }
3420 }
3421 }
3422 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003424 /* With TLS we don't handle fragmentation (for now) */
3425 if( ssl->in_msglen < ssl->in_hslen )
3426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3428 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003429 }
3430
Simon Butcher99000142016-10-13 17:21:01 +01003431 return( 0 );
3432}
3433
3434void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3435{
3436
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003437 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3438 ssl->handshake != NULL )
3439 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003440 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003441 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003442
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003443 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003445 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003446 ssl->handshake != NULL )
3447 {
3448 ssl->handshake->in_msg_seq++;
3449 }
3450#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003451}
3452
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003453/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003454 * DTLS anti-replay: RFC 6347 4.1.2.6
3455 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003456 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3457 * Bit n is set iff record number in_window_top - n has been seen.
3458 *
3459 * Usually, in_window_top is the last record number seen and the lsb of
3460 * in_window is set. The only exception is the initial state (record number 0
3461 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3464static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003465{
3466 ssl->in_window_top = 0;
3467 ssl->in_window = 0;
3468}
3469
3470static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3471{
3472 return( ( (uint64_t) buf[0] << 40 ) |
3473 ( (uint64_t) buf[1] << 32 ) |
3474 ( (uint64_t) buf[2] << 24 ) |
3475 ( (uint64_t) buf[3] << 16 ) |
3476 ( (uint64_t) buf[4] << 8 ) |
3477 ( (uint64_t) buf[5] ) );
3478}
3479
3480/*
3481 * Return 0 if sequence number is acceptable, -1 otherwise
3482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003484{
3485 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3486 uint64_t bit;
3487
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003488 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003489 return( 0 );
3490
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003491 if( rec_seqnum > ssl->in_window_top )
3492 return( 0 );
3493
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003494 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003495
3496 if( bit >= 64 )
3497 return( -1 );
3498
3499 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3500 return( -1 );
3501
3502 return( 0 );
3503}
3504
3505/*
3506 * Update replay window on new validated record
3507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003509{
3510 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3511
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003512 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003513 return;
3514
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003515 if( rec_seqnum > ssl->in_window_top )
3516 {
3517 /* Update window_top and the contents of the window */
3518 uint64_t shift = rec_seqnum - ssl->in_window_top;
3519
3520 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003521 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003522 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003523 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003524 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003525 ssl->in_window |= 1;
3526 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003527
3528 ssl->in_window_top = rec_seqnum;
3529 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003530 else
3531 {
3532 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003533 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003534
3535 if( bit < 64 ) /* Always true, but be extra sure */
3536 ssl->in_window |= (uint64_t) 1 << bit;
3537 }
3538}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003540
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003541#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003542/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003543static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3544
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003545/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003546 * Without any SSL context, check if a datagram looks like a ClientHello with
3547 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003548 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003549 *
3550 * - if cookie is valid, return 0
3551 * - if ClientHello looks superficially valid but cookie is not,
3552 * fill obuf and set olen, then
3553 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3554 * - otherwise return a specific error code
3555 */
3556static int ssl_check_dtls_clihlo_cookie(
3557 mbedtls_ssl_cookie_write_t *f_cookie_write,
3558 mbedtls_ssl_cookie_check_t *f_cookie_check,
3559 void *p_cookie,
3560 const unsigned char *cli_id, size_t cli_id_len,
3561 const unsigned char *in, size_t in_len,
3562 unsigned char *obuf, size_t buf_len, size_t *olen )
3563{
3564 size_t sid_len, cookie_len;
3565 unsigned char *p;
3566
3567 if( f_cookie_write == NULL || f_cookie_check == NULL )
3568 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3569
3570 /*
3571 * Structure of ClientHello with record and handshake headers,
3572 * and expected values. We don't need to check a lot, more checks will be
3573 * done when actually parsing the ClientHello - skipping those checks
3574 * avoids code duplication and does not make cookie forging any easier.
3575 *
3576 * 0-0 ContentType type; copied, must be handshake
3577 * 1-2 ProtocolVersion version; copied
3578 * 3-4 uint16 epoch; copied, must be 0
3579 * 5-10 uint48 sequence_number; copied
3580 * 11-12 uint16 length; (ignored)
3581 *
3582 * 13-13 HandshakeType msg_type; (ignored)
3583 * 14-16 uint24 length; (ignored)
3584 * 17-18 uint16 message_seq; copied
3585 * 19-21 uint24 fragment_offset; copied, must be 0
3586 * 22-24 uint24 fragment_length; (ignored)
3587 *
3588 * 25-26 ProtocolVersion client_version; (ignored)
3589 * 27-58 Random random; (ignored)
3590 * 59-xx SessionID session_id; 1 byte len + sid_len content
3591 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3592 * ...
3593 *
3594 * Minimum length is 61 bytes.
3595 */
3596 if( in_len < 61 ||
3597 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3598 in[3] != 0 || in[4] != 0 ||
3599 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3600 {
3601 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3602 }
3603
3604 sid_len = in[59];
3605 if( sid_len > in_len - 61 )
3606 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3607
3608 cookie_len = in[60 + sid_len];
3609 if( cookie_len > in_len - 60 )
3610 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3611
3612 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3613 cli_id, cli_id_len ) == 0 )
3614 {
3615 /* Valid cookie */
3616 return( 0 );
3617 }
3618
3619 /*
3620 * If we get here, we've got an invalid cookie, let's prepare HVR.
3621 *
3622 * 0-0 ContentType type; copied
3623 * 1-2 ProtocolVersion version; copied
3624 * 3-4 uint16 epoch; copied
3625 * 5-10 uint48 sequence_number; copied
3626 * 11-12 uint16 length; olen - 13
3627 *
3628 * 13-13 HandshakeType msg_type; hello_verify_request
3629 * 14-16 uint24 length; olen - 25
3630 * 17-18 uint16 message_seq; copied
3631 * 19-21 uint24 fragment_offset; copied
3632 * 22-24 uint24 fragment_length; olen - 25
3633 *
3634 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3635 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3636 *
3637 * Minimum length is 28.
3638 */
3639 if( buf_len < 28 )
3640 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3641
3642 /* Copy most fields and adapt others */
3643 memcpy( obuf, in, 25 );
3644 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3645 obuf[25] = 0xfe;
3646 obuf[26] = 0xff;
3647
3648 /* Generate and write actual cookie */
3649 p = obuf + 28;
3650 if( f_cookie_write( p_cookie,
3651 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3652 {
3653 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3654 }
3655
3656 *olen = p - obuf;
3657
3658 /* Go back and fill length fields */
3659 obuf[27] = (unsigned char)( *olen - 28 );
3660
3661 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3662 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3663 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3664
3665 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3666 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3667
3668 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3669}
3670
3671/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003672 * Handle possible client reconnect with the same UDP quadruplet
3673 * (RFC 6347 Section 4.2.8).
3674 *
3675 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3676 * that looks like a ClientHello.
3677 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003678 * - if the input looks like a ClientHello without cookies,
3679 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003680 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003681 * - if the input looks like a ClientHello with a valid cookie,
3682 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003683 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003684 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003685 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003686 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003687 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3688 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003689 */
3690static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3691{
3692 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003693 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003694
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003695 /* Use out_msg as temporary buffer for writing out HelloVerifyRequest,
3696 * because the output buffer's already around. Don't use out_buf though,
3697 * as we don't want to overwrite out_ctr. */
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003698 ret = ssl_check_dtls_clihlo_cookie(
3699 ssl->conf->f_cookie_write,
3700 ssl->conf->f_cookie_check,
3701 ssl->conf->p_cookie,
3702 ssl->cli_id, ssl->cli_id_len,
3703 ssl->in_buf, ssl->in_left,
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003704 ssl->out_msg, MBEDTLS_SSL_MAX_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003705
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003706 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3707
3708 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003709 {
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003710 int send_ret;
3711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3712 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003713 ssl->out_msg, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003714 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003715 * If the error is permanent we'll catch it later,
3716 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003717 send_ret = ssl->f_send( ssl->p_bio, ssl->out_msg, len );
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003718 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3719 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003720
3721 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003722 }
3723
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003724 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003725 {
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003727 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3728 {
3729 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3730 return( ret );
3731 }
3732
3733 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003734 }
3735
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003736 return( ret );
3737}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003738#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003739
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003740/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003741 * ContentType type;
3742 * ProtocolVersion version;
3743 * uint16 epoch; // DTLS only
3744 * uint48 sequence_number; // DTLS only
3745 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003746 *
3747 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003748 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003749 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3750 *
3751 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003752 * 1. proceed with the record if this function returns 0
3753 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3754 * 3. return CLIENT_RECONNECT if this function return that value
3755 * 4. drop the whole datagram if this function returns anything else.
3756 * Point 2 is needed when the peer is resending, and we have already received
3757 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003759static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003760{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003761 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763 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 +02003764
Paul Bakker5121ce52009-01-03 21:22:43 +00003765 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003766 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003767 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003770 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003771 ssl->in_msgtype,
3772 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003773
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003774 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003775 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3776 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3777 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3778 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003781
3782#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01003783 /* Silently ignore invalid DTLS records as recommended by RFC 6347
3784 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003785 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3786#endif /* MBEDTLS_SSL_PROTO_DTLS */
3787 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3788 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
3789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003791 }
3792
3793 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003794 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3797 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003798 }
3799
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003800 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3803 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003804 }
3805
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003806 /* Check length against the size of our buffer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003807 if( ssl->in_msglen > MBEDTLS_SSL_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003808 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3811 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003812 }
3813
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003814 /*
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003815 * DTLS-related tests.
3816 * Check epoch before checking length constraint because
3817 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3818 * message gets duplicated before the corresponding Finished message,
3819 * the second ChangeCipherSpec should be discarded because it belongs
3820 * to an old epoch, but not because its length is shorter than
3821 * the minimum record length for packets using the new record transform.
3822 * Note that these two kinds of failures are handled differently,
3823 * as an unexpected record is silently skipped but an invalid
3824 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003825 */
3826#if defined(MBEDTLS_SSL_PROTO_DTLS)
3827 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3828 {
3829 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3830
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003831 /* Check epoch (and sequence number) with DTLS */
3832 if( rec_epoch != ssl->in_epoch )
3833 {
3834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3835 "expected %d, received %d",
3836 ssl->in_epoch, rec_epoch ) );
3837
3838#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3839 /*
3840 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3841 * access the first byte of record content (handshake type), as we
3842 * have an active transform (possibly iv_len != 0), so use the
3843 * fact that the record header len is 13 instead.
3844 */
3845 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3846 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3847 rec_epoch == 0 &&
3848 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3849 ssl->in_left > 13 &&
3850 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3851 {
3852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3853 "from the same port" ) );
3854 return( ssl_handle_possible_reconnect( ssl ) );
3855 }
3856 else
3857#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3858 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3859 }
3860
3861#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3862 /* Replay detection only works for the current epoch */
3863 if( rec_epoch == ssl->in_epoch &&
3864 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3865 {
3866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3867 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3868 }
3869#endif
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003870
3871 /* Drop unexpected ChangeCipherSpec messages */
3872 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3873 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3874 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3875 {
3876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3877 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3878 }
3879
3880 /* Drop unexpected ApplicationData records,
3881 * except at the beginning of renegotiations */
3882 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
3883 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
3884#if defined(MBEDTLS_SSL_RENEGOTIATION)
3885 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
3886 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
3887#endif
3888 )
3889 {
3890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3891 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3892 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003893 }
3894#endif /* MBEDTLS_SSL_PROTO_DTLS */
3895
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003896
3897 /* Check length against bounds of the current transform and version */
3898 if( ssl->transform_in == NULL )
3899 {
3900 if( ssl->in_msglen < 1 ||
3901 ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
3902 {
3903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3904 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3905 }
3906 }
3907 else
3908 {
3909 if( ssl->in_msglen < ssl->transform_in->minlen )
3910 {
3911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3912 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3913 }
3914
3915#if defined(MBEDTLS_SSL_PROTO_SSL3)
3916 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3917 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_MAX_CONTENT_LEN )
3918 {
3919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3920 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3921 }
3922#endif
3923#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3924 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3925 /*
3926 * TLS encrypted messages can have up to 256 bytes of padding
3927 */
3928 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
3929 ssl->in_msglen > ssl->transform_in->minlen +
3930 MBEDTLS_SSL_MAX_CONTENT_LEN + 256 )
3931 {
3932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3933 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3934 }
3935#endif
3936 }
3937
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003938 return( 0 );
3939}
Paul Bakker5121ce52009-01-03 21:22:43 +00003940
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003941/*
3942 * If applicable, decrypt (and decompress) record content
3943 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003944static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003945{
3946 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003948 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
3949 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003951#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3952 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956 ret = mbedtls_ssl_hw_record_read( ssl );
3957 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3960 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003961 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003962
3963 if( ret == 0 )
3964 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003965 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003966#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003967 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003968 {
3969 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003972 return( ret );
3973 }
3974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003975 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00003976 ssl->in_msg, ssl->in_msglen );
3977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 if( ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3981 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003982 }
3983 }
3984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003986 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003987 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003988 {
3989 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003992 return( ret );
3993 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00003994 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003998 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004000 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004001 }
4002#endif
4003
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004004 return( 0 );
4005}
4006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004007static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004008
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004009/*
4010 * Read a record.
4011 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004012 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4013 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4014 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004017{
4018 int ret;
4019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004021
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004022 if( ssl->keep_current_message == 0 )
4023 {
4024 do {
Simon Butcher99000142016-10-13 17:21:01 +01004025
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004026 if( ( ret = mbedtls_ssl_read_record_layer( ssl ) ) != 0 )
4027 {
4028 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4029 return( ret );
4030 }
4031
4032 ret = mbedtls_ssl_handle_message_type( ssl );
4033
4034 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret );
4035
4036 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004037 {
4038 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4039 return( ret );
4040 }
4041
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004042 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4043 {
4044 mbedtls_ssl_update_handshake_status( ssl );
4045 }
Simon Butcher99000142016-10-13 17:21:01 +01004046 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004047 else
Simon Butcher99000142016-10-13 17:21:01 +01004048 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4050 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004051 }
4052
4053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4054
4055 return( 0 );
4056}
4057
4058int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4059{
4060 int ret;
4061
Hanno Becker4a810fb2017-05-24 16:27:30 +01004062 /*
4063 * Step A
4064 *
4065 * Consume last content-layer message and potentially
4066 * update in_msglen which keeps track of the contents'
4067 * consumption state.
4068 *
4069 * (1) Handshake messages:
4070 * Remove last handshake message, move content
4071 * and adapt in_msglen.
4072 *
4073 * (2) Alert messages:
4074 * Consume whole record content, in_msglen = 0.
4075 *
4076 * NOTE: This needs to be fixed, since like for
4077 * handshake messages it is allowed to have
4078 * multiple alerts witin a single record.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004079 * Internal reference IOTSSL-1321.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004080 *
4081 * (3) Change cipher spec:
4082 * Consume whole record content, in_msglen = 0.
4083 *
4084 * (4) Application data:
4085 * Don't do anything - the record layer provides
4086 * the application data as a stream transport
4087 * and consumes through mbedtls_ssl_read only.
4088 *
4089 */
4090
4091 /* Case (1): Handshake messages */
4092 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004093 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004094 /* Hard assertion to be sure that no application data
4095 * is in flight, as corrupting ssl->in_msglen during
4096 * ssl->in_offt != NULL is fatal. */
4097 if( ssl->in_offt != NULL )
4098 {
4099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4100 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4101 }
4102
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004103 /*
4104 * Get next Handshake message in the current record
4105 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004106
Hanno Becker4a810fb2017-05-24 16:27:30 +01004107 /* Notes:
4108 * (1) in_hslen is *NOT* necessarily the size of the
4109 * current handshake content: If DTLS handshake
4110 * fragmentation is used, that's the fragment
4111 * size instead. Using the total handshake message
4112 * size here is FAULTY and should be changed at
4113 * some point. Internal reference IOTSSL-1414.
4114 * (2) While it doesn't seem to cause problems, one
4115 * has to be very careful not to assume that in_hslen
4116 * is always <= in_msglen in a sensible communication.
4117 * Again, it's wrong for DTLS handshake fragmentation.
4118 * The following check is therefore mandatory, and
4119 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004120 * Additionally, ssl->in_hslen might be arbitrarily out of
4121 * bounds after handling a DTLS message with an unexpected
4122 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004123 */
4124 if( ssl->in_hslen < ssl->in_msglen )
4125 {
4126 ssl->in_msglen -= ssl->in_hslen;
4127 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4128 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004129
Hanno Becker4a810fb2017-05-24 16:27:30 +01004130 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4131 ssl->in_msg, ssl->in_msglen );
4132 }
4133 else
4134 {
4135 ssl->in_msglen = 0;
4136 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004137
Hanno Becker4a810fb2017-05-24 16:27:30 +01004138 ssl->in_hslen = 0;
4139 }
4140 /* Case (4): Application data */
4141 else if( ssl->in_offt != NULL )
4142 {
4143 return( 0 );
4144 }
4145 /* Everything else (CCS & Alerts) */
4146 else
4147 {
4148 ssl->in_msglen = 0;
4149 }
4150
4151 /*
4152 * Step B
4153 *
4154 * Fetch and decode new record if current one is fully consumed.
4155 *
4156 */
4157
4158 if( ssl->in_msglen > 0 )
4159 {
4160 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004161 return( 0 );
4162 }
4163
Hanno Becker4a810fb2017-05-24 16:27:30 +01004164 /* Need to fetch a new record */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004165
Simon Butcher99000142016-10-13 17:21:01 +01004166#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004167read_record_header:
Simon Butcher99000142016-10-13 17:21:01 +01004168#endif
4169
Hanno Becker4a810fb2017-05-24 16:27:30 +01004170 /* Current record either fully processed or to be discarded. */
4171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004172 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004174 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004175 return( ret );
4176 }
4177
4178 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004181 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4182 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004183 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004184 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4185 {
4186 /* Skip unexpected record (but not whole datagram) */
4187 ssl->next_record_offset = ssl->in_msglen
4188 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004189
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4191 "(header)" ) );
4192 }
4193 else
4194 {
4195 /* Skip invalid record and the rest of the datagram */
4196 ssl->next_record_offset = 0;
4197 ssl->in_left = 0;
4198
4199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4200 "(header)" ) );
4201 }
4202
4203 /* Get next record */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004204 goto read_record_header;
4205 }
4206#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004207 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004208 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004209
4210 /*
4211 * Read and optionally decrypt the message contents
4212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4214 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004217 return( ret );
4218 }
4219
4220 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004221#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004222 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004224 else
4225#endif
4226 ssl->in_left = 0;
4227
4228 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004230#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004231 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004232 {
4233 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4235 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004236 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004237 /* Except when waiting for Finished as a bad mac here
4238 * probably means something went wrong in the handshake
4239 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4240 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4241 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4242 {
4243#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4244 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4245 {
4246 mbedtls_ssl_send_alert_message( ssl,
4247 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4248 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4249 }
4250#endif
4251 return( ret );
4252 }
4253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004255 if( ssl->conf->badmac_limit != 0 &&
4256 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4259 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004260 }
4261#endif
4262
Hanno Becker4a810fb2017-05-24 16:27:30 +01004263 /* As above, invalid records cause
4264 * dismissal of the whole datagram. */
4265
4266 ssl->next_record_offset = 0;
4267 ssl->in_left = 0;
4268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004270 goto read_record_header;
4271 }
4272
4273 return( ret );
4274 }
4275 else
4276#endif
4277 {
4278 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004279#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4280 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282 mbedtls_ssl_send_alert_message( ssl,
4283 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4284 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004285 }
4286#endif
4287 return( ret );
4288 }
4289 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004290
4291 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004292 * When we sent the last flight of the handshake, we MUST respond to a
4293 * retransmit of the peer's previous flight with a retransmit. (In
4294 * practice, only the Finished message will make it, other messages
4295 * including CCS use the old transform so they're dropped as invalid.)
4296 *
4297 * If the record we received is not a handshake message, however, it
4298 * means the peer received our last flight so we can clean up
4299 * handshake info.
4300 *
4301 * This check needs to be done before prepare_handshake() due to an edge
4302 * case: if the client immediately requests renegotiation, this
4303 * finishes the current handshake first, avoiding the new ClientHello
4304 * being mistaken for an ancient message in the current handshake.
4305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004306#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004307 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004308 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4312 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004316 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004319 return( ret );
4320 }
4321
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01004322 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004323 }
4324 else
4325 {
4326 ssl_handshake_wrapup_free_hs_transform( ssl );
4327 }
4328 }
4329#endif
4330
Simon Butcher99000142016-10-13 17:21:01 +01004331 return( 0 );
4332}
4333
4334int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4335{
4336 int ret;
4337
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004338 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004339 * Handle particular types of records
4340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004342 {
Simon Butcher99000142016-10-13 17:21:01 +01004343 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4344 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004345 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004346 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004347 }
4348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004349 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004350 {
Angus Gratton8946b0d2018-06-20 15:43:50 +10004351 if( ssl->in_msglen != 2 )
4352 {
4353 /* Note: Standard allows for more than one 2 byte alert
4354 to be packed in a single message, but Mbed TLS doesn't
4355 currently support this. */
4356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4357 ssl->in_msglen ) );
4358 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4359 }
4360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004362 ssl->in_msg[0], ssl->in_msg[1] ) );
4363
4364 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004365 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004367 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004370 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004372 }
4373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004374 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4375 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4378 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004379 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004380
4381#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4382 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4383 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4384 {
4385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4386 /* Will be handled when trying to parse ServerHello */
4387 return( 0 );
4388 }
4389#endif
4390
4391#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4392 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4393 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4394 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4395 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4396 {
4397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4398 /* Will be handled in mbedtls_ssl_parse_certificate() */
4399 return( 0 );
4400 }
4401#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4402
4403 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004404 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004405 }
4406
Paul Bakker5121ce52009-01-03 21:22:43 +00004407 return( 0 );
4408}
4409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004410int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004411{
4412 int ret;
4413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004414 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4415 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4416 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004417 {
4418 return( ret );
4419 }
4420
4421 return( 0 );
4422}
4423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004424int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004425 unsigned char level,
4426 unsigned char message )
4427{
4428 int ret;
4429
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004430 if( ssl == NULL || ssl->conf == NULL )
4431 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004434 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004436 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004437 ssl->out_msglen = 2;
4438 ssl->out_msg[0] = level;
4439 ssl->out_msg[1] = message;
4440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004441 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004444 return( ret );
4445 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004447
4448 return( 0 );
4449}
4450
Paul Bakker5121ce52009-01-03 21:22:43 +00004451/*
4452 * Handshake functions
4453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4455 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4456 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4457 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4458 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4459 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4460 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004461/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004463{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004464 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004468 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4469 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004470 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4471 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004474 ssl->state++;
4475 return( 0 );
4476 }
4477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4479 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004480}
4481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004483{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004488 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4489 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004490 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4491 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004494 ssl->state++;
4495 return( 0 );
4496 }
4497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4499 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004500}
Gilles Peskinef9828522017-05-03 12:28:43 +02004501
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004502#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004503/* Some certificate support -> implement write and parse */
4504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004505int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004506{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004508 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004509 const mbedtls_x509_crt *crt;
4510 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004514 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4515 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004516 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4517 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004520 ssl->state++;
4521 return( 0 );
4522 }
4523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004524#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004525 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004526 {
4527 if( ssl->client_auth == 0 )
4528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004530 ssl->state++;
4531 return( 0 );
4532 }
4533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004534#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004535 /*
4536 * If using SSLv3 and got no cert, send an Alert message
4537 * (otherwise an empty Certificate message will be sent).
4538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4540 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004541 {
4542 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4544 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4545 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004548 goto write_msg;
4549 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004551 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552#endif /* MBEDTLS_SSL_CLI_C */
4553#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004554 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004556 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4559 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004560 }
4561 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004562#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004565
4566 /*
4567 * 0 . 0 handshake type
4568 * 1 . 3 handshake length
4569 * 4 . 6 length of all certs
4570 * 7 . 9 length of cert. 1
4571 * 10 . n-1 peer certificate
4572 * n . n+2 length of cert. 2
4573 * n+3 . ... upper level cert, etc.
4574 */
4575 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004577
Paul Bakker29087132010-03-21 21:03:34 +00004578 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004579 {
4580 n = crt->raw.len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004581 if( n > MBEDTLS_SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
4584 i + 3 + n, MBEDTLS_SSL_MAX_CONTENT_LEN ) );
4585 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004586 }
4587
4588 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4589 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4590 ssl->out_msg[i + 2] = (unsigned char)( n );
4591
4592 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4593 i += n; crt = crt->next;
4594 }
4595
4596 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4597 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4598 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4599
4600 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004601 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4602 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004603
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004604#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004605write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004606#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004607
4608 ssl->state++;
4609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004610 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004612 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004613 return( ret );
4614 }
4615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004617
Paul Bakkered27a042013-04-18 22:46:23 +02004618 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004619}
4620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004622{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004624 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004625 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004626 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004627 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4632 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004633 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4634 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004637 ssl->state++;
4638 return( 0 );
4639 }
4640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004641#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004642 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004643 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4644 {
4645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4646 ssl->state++;
4647 return( 0 );
4648 }
4649
4650#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4651 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4652 authmode = ssl->handshake->sni_authmode;
4653#endif
4654
4655 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4656 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004657 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004658 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004660 ssl->state++;
4661 return( 0 );
4662 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004663#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004665 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004666 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004667 /* mbedtls_ssl_read_record may have sent an alert already. We
4668 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004670 return( ret );
4671 }
4672
4673 ssl->state++;
4674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004675#if defined(MBEDTLS_SSL_SRV_C)
4676#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004677 /*
4678 * Check if the client sent an empty certificate
4679 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004680 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004681 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004682 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004683 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004684 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4685 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4686 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004689
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004690 /* The client was asked for a certificate but didn't send
4691 one. The client should know what's going on, so we
4692 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004693 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004694 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004695 return( 0 );
4696 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004697 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004698 }
4699 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004700#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004702#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4703 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004704 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004705 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004707 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4708 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4709 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4710 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004713
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004714 /* The client was asked for a certificate but didn't send
4715 one. The client should know what's going on, so we
4716 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004717 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004718 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004719 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004720 else
4721 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004722 }
4723 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004724#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4725 MBEDTLS_SSL_PROTO_TLS1_2 */
4726#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004731 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4732 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004733 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004734 }
4735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4737 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004740 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4741 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004743 }
4744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004745 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004746
Paul Bakker5121ce52009-01-03 21:22:43 +00004747 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004749 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004750 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004751
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004752 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004753 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004756 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4757 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004758 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004759 }
4760
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004761 /* In case we tried to reuse a session but it failed */
4762 if( ssl->session_negotiate->peer_cert != NULL )
4763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004764 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4765 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004766 }
4767
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004768 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004769 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004770 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004772 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004773 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4774 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004775 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004776 }
4777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004778 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004779
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004780 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004781
4782 while( i < ssl->in_hslen )
4783 {
Philippe Antoine33e5c322018-07-09 10:39:02 +02004784 if ( i + 3 > ssl->in_hslen ) {
4785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4786 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4787 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4788 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
4789 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004790 if( ssl->in_msg[i] != 0 )
4791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004793 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4794 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004795 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004796 }
4797
4798 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4799 | (unsigned int) ssl->in_msg[i + 2];
4800 i += 3;
4801
4802 if( n < 128 || i + n > ssl->in_hslen )
4803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004805 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4806 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004807 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004808 }
4809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004810 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004811 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004812 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004813 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004814 case 0: /*ok*/
4815 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
4816 /* Ignore certificate with an unknown algorithm: maybe a
4817 prior certificate was already trusted. */
4818 break;
4819
4820 case MBEDTLS_ERR_X509_ALLOC_FAILED:
4821 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
4822 goto crt_parse_der_failed;
4823
4824 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
4825 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4826 goto crt_parse_der_failed;
4827
4828 default:
4829 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4830 crt_parse_der_failed:
4831 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004832 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004833 return( ret );
4834 }
4835
4836 i += n;
4837 }
4838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004840
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004841 /*
4842 * On client, make sure the server cert doesn't change during renego to
4843 * avoid "triple handshake" attack: https://secure-resumption.com/
4844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004846 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004848 {
4849 if( ssl->session->peer_cert == NULL )
4850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004852 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4853 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004854 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004855 }
4856
4857 if( ssl->session->peer_cert->raw.len !=
4858 ssl->session_negotiate->peer_cert->raw.len ||
4859 memcmp( ssl->session->peer_cert->raw.p,
4860 ssl->session_negotiate->peer_cert->raw.p,
4861 ssl->session->peer_cert->raw.len ) != 0 )
4862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004864 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4865 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004866 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004867 }
4868 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004869#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004870
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004871 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004872 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004873 mbedtls_x509_crt *ca_chain;
4874 mbedtls_x509_crl *ca_crl;
4875
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004876#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004877 if( ssl->handshake->sni_ca_chain != NULL )
4878 {
4879 ca_chain = ssl->handshake->sni_ca_chain;
4880 ca_crl = ssl->handshake->sni_ca_crl;
4881 }
4882 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004883#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004884 {
4885 ca_chain = ssl->conf->ca_chain;
4886 ca_crl = ssl->conf->ca_crl;
4887 }
4888
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004889 /*
4890 * Main check: verify certificate
4891 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004892 ret = mbedtls_x509_crt_verify_with_profile(
4893 ssl->session_negotiate->peer_cert,
4894 ca_chain, ca_crl,
4895 ssl->conf->cert_profile,
4896 ssl->hostname,
4897 &ssl->session_negotiate->verify_result,
4898 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004899
4900 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004903 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004904
4905 /*
4906 * Secondary checks: always done, but change 'ret' only if it was 0
4907 */
4908
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004909#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004911 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004912
4913 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004914 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004915 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004916 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004917 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
4918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004920 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004921 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004922 }
4923 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004924#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004926 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004927 ciphersuite_info,
4928 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004929 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004932 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004933 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004934 }
4935
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004936 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
4937 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
4938 * with details encoded in the verification flags. All other kinds
4939 * of error codes, including those from the user provided f_vrfy
4940 * functions, are treated as fatal and lead to a failure of
4941 * ssl_parse_certificate even if verification was optional. */
4942 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
4943 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
4944 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
4945 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004946 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004947 }
4948
4949 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
4950 {
4951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
4952 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
4953 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004954
4955 if( ret != 0 )
4956 {
4957 /* The certificate may have been rejected for several reasons.
4958 Pick one and send the corresponding alert. Which alert to send
4959 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004960 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
4961 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
4962 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
4963 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4964 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
4965 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4966 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
4967 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4968 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
4969 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4970 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
4971 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4972 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
4973 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4974 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
4975 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
4976 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
4977 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
4978 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
4979 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02004980 else
4981 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004982 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4983 alert );
4984 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004985
Hanno Beckere6706e62017-05-15 16:05:15 +01004986#if defined(MBEDTLS_DEBUG_C)
4987 if( ssl->session_negotiate->verify_result != 0 )
4988 {
4989 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
4990 ssl->session_negotiate->verify_result ) );
4991 }
4992 else
4993 {
4994 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
4995 }
4996#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004997 }
4998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005000
5001 return( ret );
5002}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005003#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5004 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5005 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5006 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5007 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5008 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5009 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005011int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005012{
5013 int ret;
5014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005017 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005018 ssl->out_msglen = 1;
5019 ssl->out_msg[0] = 1;
5020
Paul Bakker5121ce52009-01-03 21:22:43 +00005021 ssl->state++;
5022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005026 return( ret );
5027 }
5028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005030
5031 return( 0 );
5032}
5033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005034int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005035{
5036 int ret;
5037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005040 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005043 return( ret );
5044 }
5045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005046 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005049 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5050 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005051 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005052 }
5053
5054 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005057 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5058 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005059 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005060 }
5061
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005062 /*
5063 * Switch to our negotiated transform and session parameters for inbound
5064 * data.
5065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005066 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005067 ssl->transform_in = ssl->transform_negotiate;
5068 ssl->session_in = ssl->session_negotiate;
5069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005070#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005071 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005074 ssl_dtls_replay_reset( ssl );
5075#endif
5076
5077 /* Increment epoch */
5078 if( ++ssl->in_epoch == 0 )
5079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005081 /* This is highly unlikely to happen for legitimate reasons, so
5082 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005083 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005084 }
5085 }
5086 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005087#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005088 memset( ssl->in_ctr, 0, 8 );
5089
5090 /*
5091 * Set the in_msg pointer to the correct location based on IV length
5092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005093 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005094 {
5095 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
5096 ssl->transform_negotiate->fixed_ivlen;
5097 }
5098 else
5099 ssl->in_msg = ssl->in_iv;
5100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005101#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5102 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005104 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005106 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005107 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5108 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005110 }
5111 }
5112#endif
5113
Paul Bakker5121ce52009-01-03 21:22:43 +00005114 ssl->state++;
5115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005117
5118 return( 0 );
5119}
5120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5122 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005123{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005124 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5127 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5128 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005129 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005130 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005131#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5133#if defined(MBEDTLS_SHA512_C)
5134 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005135 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5136 else
5137#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005138#if defined(MBEDTLS_SHA256_C)
5139 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005140 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005141 else
5142#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005143#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005146 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005147 }
Paul Bakker380da532012-04-18 16:10:25 +00005148}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005150void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005151{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005152#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5153 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005154 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5155 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005156#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005157#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5158#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005159 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005161#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005162 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005163#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005164#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005165}
5166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005167static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005168 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005169{
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_update_ret( &ssl->handshake->fin_md5 , buf, len );
5173 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +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_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +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_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005181#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005182#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005183}
5184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005185#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5186 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5187static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005188 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005189{
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 Bakker380da532012-04-18 16:10:25 +00005192}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005193#endif
Paul Bakker380da532012-04-18 16:10:25 +00005194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005195#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5196#if defined(MBEDTLS_SHA256_C)
5197static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005198 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005199{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005200 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005201}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005202#endif
Paul Bakker380da532012-04-18 16:10:25 +00005203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005204#if defined(MBEDTLS_SHA512_C)
5205static void ssl_update_checksum_sha384( 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_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005209}
Paul Bakker769075d2012-11-24 11:26:46 +01005210#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005211#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005213#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005214static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005216{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005217 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005218 mbedtls_md5_context md5;
5219 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005220
Paul Bakker5121ce52009-01-03 21:22:43 +00005221 unsigned char padbuf[48];
5222 unsigned char md5sum[16];
5223 unsigned char sha1sum[20];
5224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005226 if( !session )
5227 session = ssl->session;
5228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005230
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005231 mbedtls_md5_init( &md5 );
5232 mbedtls_sha1_init( &sha1 );
5233
5234 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5235 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005236
5237 /*
5238 * SSLv3:
5239 * hash =
5240 * MD5( master + pad2 +
5241 * MD5( handshake + sender + master + pad1 ) )
5242 * + SHA1( master + pad2 +
5243 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005244 */
5245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005246#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005247 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5248 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005249#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005251#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005252 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5253 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005254#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005256 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005257 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005258
Paul Bakker1ef83d62012-04-11 12:09:53 +00005259 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005260
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005261 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5262 mbedtls_md5_update_ret( &md5, session->master, 48 );
5263 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5264 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005265
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005266 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5267 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5268 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5269 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005270
Paul Bakker1ef83d62012-04-11 12:09:53 +00005271 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005272
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005273 mbedtls_md5_starts_ret( &md5 );
5274 mbedtls_md5_update_ret( &md5, session->master, 48 );
5275 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5276 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5277 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005278
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005279 mbedtls_sha1_starts_ret( &sha1 );
5280 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5281 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5282 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5283 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005286
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005287 mbedtls_md5_free( &md5 );
5288 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005290 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
5291 mbedtls_zeroize( md5sum, sizeof( md5sum ) );
5292 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005295}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005298#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005299static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005300 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005301{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005302 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005303 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005304 mbedtls_md5_context md5;
5305 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005306 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005309 if( !session )
5310 session = ssl->session;
5311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005313
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005314 mbedtls_md5_init( &md5 );
5315 mbedtls_sha1_init( &sha1 );
5316
5317 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5318 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005319
Paul Bakker1ef83d62012-04-11 12:09:53 +00005320 /*
5321 * TLSv1:
5322 * hash = PRF( master, finished_label,
5323 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5324 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005327 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5328 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005329#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005332 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5333 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005334#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005337 ? "client finished"
5338 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005339
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005340 mbedtls_md5_finish_ret( &md5, padbuf );
5341 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005342
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005343 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005344 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005346 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005347
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005348 mbedtls_md5_free( &md5 );
5349 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005351 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005355#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005357#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5358#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005359static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005360 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005361{
5362 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005363 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005364 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005365 unsigned char padbuf[32];
5366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005368 if( !session )
5369 session = ssl->session;
5370
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005371 mbedtls_sha256_init( &sha256 );
5372
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005374
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005375 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005376
5377 /*
5378 * TLSv1.2:
5379 * hash = PRF( master, finished_label,
5380 * Hash( handshake ) )[0.11]
5381 */
5382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383#if !defined(MBEDTLS_SHA256_ALT)
5384 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005385 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005386#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005389 ? "client finished"
5390 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005391
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005392 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005393
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005394 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005395 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005398
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005399 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005405#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005408static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005409 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005410{
5411 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005412 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005413 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005414 unsigned char padbuf[48];
5415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005417 if( !session )
5418 session = ssl->session;
5419
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005420 mbedtls_sha512_init( &sha512 );
5421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005423
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005424 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005425
5426 /*
5427 * TLSv1.2:
5428 * hash = PRF( master, finished_label,
5429 * Hash( handshake ) )[0.11]
5430 */
5431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005432#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005433 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5434 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005435#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005438 ? "client finished"
5439 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005440
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005441 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005442
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005443 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005444 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005447
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005448 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005450 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005454#endif /* MBEDTLS_SHA512_C */
5455#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005457static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005458{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005459 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005460
5461 /*
5462 * Free our handshake params
5463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 mbedtls_ssl_handshake_free( ssl->handshake );
5465 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005466 ssl->handshake = NULL;
5467
5468 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005469 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005470 */
5471 if( ssl->transform )
5472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 mbedtls_ssl_transform_free( ssl->transform );
5474 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005475 }
5476 ssl->transform = ssl->transform_negotiate;
5477 ssl->transform_negotiate = NULL;
5478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005480}
5481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005483{
5484 int resume = ssl->handshake->resume;
5485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488#if defined(MBEDTLS_SSL_RENEGOTIATION)
5489 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005491 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005492 ssl->renego_records_seen = 0;
5493 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005494#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005495
5496 /*
5497 * Free the previous session and switch in the current one
5498 */
Paul Bakker0a597072012-09-25 21:55:46 +00005499 if( ssl->session )
5500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005502 /* RFC 7366 3.1: keep the EtM state */
5503 ssl->session_negotiate->encrypt_then_mac =
5504 ssl->session->encrypt_then_mac;
5505#endif
5506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005507 mbedtls_ssl_session_free( ssl->session );
5508 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005509 }
5510 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005511 ssl->session_negotiate = NULL;
5512
Paul Bakker0a597072012-09-25 21:55:46 +00005513 /*
5514 * Add cache entry
5515 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005516 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005517 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005518 resume == 0 )
5519 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005520 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005522 }
Paul Bakker0a597072012-09-25 21:55:46 +00005523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005525 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005526 ssl->handshake->flight != NULL )
5527 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005528 /* Cancel handshake timer */
5529 ssl_set_timer( ssl, 0 );
5530
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005531 /* Keep last flight around in case we need to resend it:
5532 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005533 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005534 }
5535 else
5536#endif
5537 ssl_handshake_wrapup_free_hs_transform( ssl );
5538
Paul Bakker48916f92012-09-16 19:57:18 +00005539 ssl->state++;
5540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005541 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005542}
5543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005544int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005545{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005546 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005549
Paul Bakker92be97b2013-01-02 17:30:03 +01005550 /*
5551 * Set the out_msg pointer to the correct location based on IV length
5552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005553 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005554 {
5555 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5556 ssl->transform_negotiate->fixed_ivlen;
5557 }
5558 else
5559 ssl->out_msg = ssl->out_iv;
5560
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005561 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005562
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005563 /*
5564 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5565 * may define some other value. Currently (early 2016), no defined
5566 * ciphersuite does this (and this is unlikely to change as activity has
5567 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005569 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005572 ssl->verify_data_len = hash_len;
5573 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005574#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005575
Paul Bakker5121ce52009-01-03 21:22:43 +00005576 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5578 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005579
5580 /*
5581 * In case of session resuming, invert the client and server
5582 * ChangeCipherSpec messages order.
5583 */
Paul Bakker0a597072012-09-25 21:55:46 +00005584 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005586#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005587 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005589#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005591 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005592 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005593#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005594 }
5595 else
5596 ssl->state++;
5597
Paul Bakker48916f92012-09-16 19:57:18 +00005598 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005599 * Switch to our negotiated transform and session parameters for outbound
5600 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005605 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005606 {
5607 unsigned char i;
5608
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005609 /* Remember current epoch settings for resending */
5610 ssl->handshake->alt_transform_out = ssl->transform_out;
5611 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5612
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005613 /* Set sequence_number to zero */
5614 memset( ssl->out_ctr + 2, 0, 6 );
5615
5616 /* Increment epoch */
5617 for( i = 2; i > 0; i-- )
5618 if( ++ssl->out_ctr[i - 1] != 0 )
5619 break;
5620
5621 /* The loop goes to its end iff the counter is wrapping */
5622 if( i == 0 )
5623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5625 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005626 }
5627 }
5628 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005630 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005631
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005632 ssl->transform_out = ssl->transform_negotiate;
5633 ssl->session_out = ssl->session_negotiate;
5634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5636 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5641 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005642 }
5643 }
5644#endif
5645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005646#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005647 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005649#endif
5650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005651 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005653 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005654 return( ret );
5655 }
5656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005658
5659 return( 0 );
5660}
5661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005662#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005663#define SSL_MAX_HASH_LEN 36
5664#else
5665#define SSL_MAX_HASH_LEN 12
5666#endif
5667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005668int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005669{
Paul Bakker23986e52011-04-24 08:57:21 +00005670 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005671 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005672 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005675
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005676 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005678 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005680 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005681 return( ret );
5682 }
5683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005684 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005687 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5688 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005690 }
5691
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005692 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005693#if defined(MBEDTLS_SSL_PROTO_SSL3)
5694 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005695 hash_len = 36;
5696 else
5697#endif
5698 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005700 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5701 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005704 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5705 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005707 }
5708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005710 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005713 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5714 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005716 }
5717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005718#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005719 ssl->verify_data_len = hash_len;
5720 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005721#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005722
Paul Bakker0a597072012-09-25 21:55:46 +00005723 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005726 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005728#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005730 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005732#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005733 }
5734 else
5735 ssl->state++;
5736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005738 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005739 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005740#endif
5741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005743
5744 return( 0 );
5745}
5746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005747static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005748{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005751#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5752 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5753 mbedtls_md5_init( &handshake->fin_md5 );
5754 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005755 mbedtls_md5_starts_ret( &handshake->fin_md5 );
5756 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005757#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5759#if defined(MBEDTLS_SHA256_C)
5760 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005761 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005762#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763#if defined(MBEDTLS_SHA512_C)
5764 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005765 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005766#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005767#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005768
5769 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005770
5771#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5772 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5773 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5774#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005776#if defined(MBEDTLS_DHM_C)
5777 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005778#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779#if defined(MBEDTLS_ECDH_C)
5780 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005781#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02005782#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005783 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02005784#if defined(MBEDTLS_SSL_CLI_C)
5785 handshake->ecjpake_cache = NULL;
5786 handshake->ecjpake_cache_len = 0;
5787#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005788#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005789
5790#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5791 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5792#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005793}
5794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005796{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005797 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5800 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802 mbedtls_md_init( &transform->md_ctx_enc );
5803 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005804}
5805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005807{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005808 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005809}
5810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005812{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005813 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005814 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005815 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005816 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005818 if( ssl->handshake )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819 mbedtls_ssl_handshake_free( ssl->handshake );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005820
5821 /*
5822 * Either the pointers are now NULL or cleared properly and can be freed.
5823 * Now allocate missing structures.
5824 */
5825 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005826 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005827 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005828 }
Paul Bakker48916f92012-09-16 19:57:18 +00005829
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005830 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005831 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005832 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005833 }
Paul Bakker48916f92012-09-16 19:57:18 +00005834
Paul Bakker82788fb2014-10-20 13:59:19 +02005835 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005836 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005837 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005838 }
Paul Bakker48916f92012-09-16 19:57:18 +00005839
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005840 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005841 if( ssl->handshake == NULL ||
5842 ssl->transform_negotiate == NULL ||
5843 ssl->session_negotiate == NULL )
5844 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005847 mbedtls_free( ssl->handshake );
5848 mbedtls_free( ssl->transform_negotiate );
5849 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005850
5851 ssl->handshake = NULL;
5852 ssl->transform_negotiate = NULL;
5853 ssl->session_negotiate = NULL;
5854
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005855 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005856 }
5857
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005858 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005860 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005861 ssl_handshake_params_init( ssl->handshake );
5862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005864 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5865 {
5866 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005867
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005868 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5869 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5870 else
5871 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005872
5873 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005874 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005875#endif
5876
Paul Bakker48916f92012-09-16 19:57:18 +00005877 return( 0 );
5878}
5879
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005880#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005881/* Dummy cookie callbacks for defaults */
5882static int ssl_cookie_write_dummy( void *ctx,
5883 unsigned char **p, unsigned char *end,
5884 const unsigned char *cli_id, size_t cli_id_len )
5885{
5886 ((void) ctx);
5887 ((void) p);
5888 ((void) end);
5889 ((void) cli_id);
5890 ((void) cli_id_len);
5891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005893}
5894
5895static int ssl_cookie_check_dummy( void *ctx,
5896 const unsigned char *cookie, size_t cookie_len,
5897 const unsigned char *cli_id, size_t cli_id_len )
5898{
5899 ((void) ctx);
5900 ((void) cookie);
5901 ((void) cookie_len);
5902 ((void) cli_id);
5903 ((void) cli_id_len);
5904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005905 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005906}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005907#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005908
Paul Bakker5121ce52009-01-03 21:22:43 +00005909/*
5910 * Initialize an SSL context
5911 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005912void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
5913{
5914 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
5915}
5916
5917/*
5918 * Setup an SSL context
5919 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005920int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02005921 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00005922{
Paul Bakker48916f92012-09-16 19:57:18 +00005923 int ret;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005924 const size_t len = MBEDTLS_SSL_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005925
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005926 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00005927
5928 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005929 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00005930 */
k-stachowiakc2eddee2018-07-09 10:39:20 +02005931 ssl->in_buf = NULL;
5932 ssl->out_buf = NULL;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005933 if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL ||
5934 ( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005935 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", len ) );
k-stachowiak2c161142018-07-31 16:52:32 +02005937 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiakc2eddee2018-07-09 10:39:20 +02005938 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005939 }
5940
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005941#if defined(MBEDTLS_SSL_PROTO_DTLS)
5942 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5943 {
5944 ssl->out_hdr = ssl->out_buf;
5945 ssl->out_ctr = ssl->out_buf + 3;
5946 ssl->out_len = ssl->out_buf + 11;
5947 ssl->out_iv = ssl->out_buf + 13;
5948 ssl->out_msg = ssl->out_buf + 13;
5949
5950 ssl->in_hdr = ssl->in_buf;
5951 ssl->in_ctr = ssl->in_buf + 3;
5952 ssl->in_len = ssl->in_buf + 11;
5953 ssl->in_iv = ssl->in_buf + 13;
5954 ssl->in_msg = ssl->in_buf + 13;
5955 }
5956 else
5957#endif
5958 {
5959 ssl->out_ctr = ssl->out_buf;
5960 ssl->out_hdr = ssl->out_buf + 8;
5961 ssl->out_len = ssl->out_buf + 11;
5962 ssl->out_iv = ssl->out_buf + 13;
5963 ssl->out_msg = ssl->out_buf + 13;
5964
5965 ssl->in_ctr = ssl->in_buf;
5966 ssl->in_hdr = ssl->in_buf + 8;
5967 ssl->in_len = ssl->in_buf + 11;
5968 ssl->in_iv = ssl->in_buf + 13;
5969 ssl->in_msg = ssl->in_buf + 13;
5970 }
5971
Paul Bakker48916f92012-09-16 19:57:18 +00005972 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiakc2eddee2018-07-09 10:39:20 +02005973 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005974
5975 return( 0 );
k-stachowiakc2eddee2018-07-09 10:39:20 +02005976
5977error:
5978 mbedtls_free( ssl->in_buf );
5979 mbedtls_free( ssl->out_buf );
5980
5981 ssl->conf = NULL;
5982
5983 ssl->in_buf = NULL;
5984 ssl->out_buf = NULL;
5985
5986 ssl->in_hdr = NULL;
5987 ssl->in_ctr = NULL;
5988 ssl->in_len = NULL;
5989 ssl->in_iv = NULL;
5990 ssl->in_msg = NULL;
5991
5992 ssl->out_hdr = NULL;
5993 ssl->out_ctr = NULL;
5994 ssl->out_len = NULL;
5995 ssl->out_iv = NULL;
5996 ssl->out_msg = NULL;
5997
k-stachowiak2c161142018-07-31 16:52:32 +02005998 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005999}
6000
6001/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006002 * Reset an initialized and used SSL context for re-use while retaining
6003 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006004 *
6005 * If partial is non-zero, keep data in the input buffer and client ID.
6006 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006007 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006008static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006009{
Paul Bakker48916f92012-09-16 19:57:18 +00006010 int ret;
6011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006013
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006014 /* Cancel any possibly running timer */
6015 ssl_set_timer( ssl, 0 );
6016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006017#if defined(MBEDTLS_SSL_RENEGOTIATION)
6018 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006019 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006020
6021 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6023 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006024#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006026
Paul Bakker7eb013f2011-10-06 12:37:39 +00006027 ssl->in_offt = NULL;
6028
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006029 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006030 ssl->in_msgtype = 0;
6031 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006032 if( partial == 0 )
6033 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006035 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006036 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006039 ssl_dtls_replay_reset( ssl );
6040#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006041
6042 ssl->in_hslen = 0;
6043 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006044
6045 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006046
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006047 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006048 ssl->out_msgtype = 0;
6049 ssl->out_msglen = 0;
6050 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6052 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006053 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006054#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006055
Paul Bakker48916f92012-09-16 19:57:18 +00006056 ssl->transform_in = NULL;
6057 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006058
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006059 ssl->session_in = NULL;
6060 ssl->session_out = NULL;
6061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062 memset( ssl->out_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006063
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006064 if( partial == 0 )
6065 memset( ssl->in_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00006066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6068 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6071 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6074 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006075 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006076 }
6077#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006078
Paul Bakker48916f92012-09-16 19:57:18 +00006079 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006081 mbedtls_ssl_transform_free( ssl->transform );
6082 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006083 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006084 }
Paul Bakker48916f92012-09-16 19:57:18 +00006085
Paul Bakkerc0463502013-02-14 11:19:38 +01006086 if( ssl->session )
6087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006088 mbedtls_ssl_session_free( ssl->session );
6089 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006090 ssl->session = NULL;
6091 }
6092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006094 ssl->alpn_chosen = NULL;
6095#endif
6096
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006097#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006098 if( partial == 0 )
6099 {
6100 mbedtls_free( ssl->cli_id );
6101 ssl->cli_id = NULL;
6102 ssl->cli_id_len = 0;
6103 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006104#endif
6105
Paul Bakker48916f92012-09-16 19:57:18 +00006106 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6107 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006108
6109 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006110}
6111
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006112/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006113 * Reset an initialized and used SSL context for re-use while retaining
6114 * all application-set variables, function pointers and data.
6115 */
6116int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6117{
6118 return( ssl_session_reset_int( ssl, 0 ) );
6119}
6120
6121/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006122 * SSL set accessors
6123 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006124void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006125{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006126 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006127}
6128
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006129void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006130{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006131 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006132}
6133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006135void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006136{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006137 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006138}
6139#endif
6140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006141#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006142void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006143{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006144 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006145}
6146#endif
6147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006149void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006150{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006151 conf->hs_timeout_min = min;
6152 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006153}
6154#endif
6155
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006156void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006157{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006158 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006159}
6160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006162void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006163 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006164 void *p_vrfy )
6165{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006166 conf->f_vrfy = f_vrfy;
6167 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006169#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006170
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006171void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006172 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006173 void *p_rng )
6174{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006175 conf->f_rng = f_rng;
6176 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006177}
6178
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006179void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006180 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006181 void *p_dbg )
6182{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006183 conf->f_dbg = f_dbg;
6184 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006185}
6186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006188 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006189 mbedtls_ssl_send_t *f_send,
6190 mbedtls_ssl_recv_t *f_recv,
6191 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006192{
6193 ssl->p_bio = p_bio;
6194 ssl->f_send = f_send;
6195 ssl->f_recv = f_recv;
6196 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006197}
6198
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006199void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006200{
6201 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006202}
6203
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006204void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6205 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006206 mbedtls_ssl_set_timer_t *f_set_timer,
6207 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006208{
6209 ssl->p_timer = p_timer;
6210 ssl->f_set_timer = f_set_timer;
6211 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006212
6213 /* Make sure we start with no timer running */
6214 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006215}
6216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006217#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006218void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006219 void *p_cache,
6220 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6221 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006222{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006223 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006224 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006225 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229#if defined(MBEDTLS_SSL_CLI_C)
6230int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006231{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006232 int ret;
6233
6234 if( ssl == NULL ||
6235 session == NULL ||
6236 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006237 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006240 }
6241
6242 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6243 return( ret );
6244
Paul Bakker0a597072012-09-25 21:55:46 +00006245 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006246
6247 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006248}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006250
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006251void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006252 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006253{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006254 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6255 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6256 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6257 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006258}
6259
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006260void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006261 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006262 int major, int minor )
6263{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006265 return;
6266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006267 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006268 return;
6269
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006270 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006271}
6272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006273#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006274void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006275 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006276{
6277 conf->cert_profile = profile;
6278}
6279
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006280/* Append a new keycert entry to a (possibly empty) list */
6281static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6282 mbedtls_x509_crt *cert,
6283 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006284{
niisatoa35dbf12018-06-25 19:05:48 +09006285 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006286
niisatoa35dbf12018-06-25 19:05:48 +09006287 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6288 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006289 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006290
niisatoa35dbf12018-06-25 19:05:48 +09006291 new_cert->cert = cert;
6292 new_cert->key = key;
6293 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006294
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006295 /* Update head is the list was null, else add to the end */
6296 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006297 {
niisatoa35dbf12018-06-25 19:05:48 +09006298 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006299 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006300 else
6301 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006302 mbedtls_ssl_key_cert *cur = *head;
6303 while( cur->next != NULL )
6304 cur = cur->next;
niisatoa35dbf12018-06-25 19:05:48 +09006305 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006306 }
6307
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006308 return( 0 );
6309}
6310
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006311int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006312 mbedtls_x509_crt *own_cert,
6313 mbedtls_pk_context *pk_key )
6314{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006315 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006316}
6317
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006318void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006319 mbedtls_x509_crt *ca_chain,
6320 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006321{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006322 conf->ca_chain = ca_chain;
6323 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006324}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006326
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006327#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6328int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6329 mbedtls_x509_crt *own_cert,
6330 mbedtls_pk_context *pk_key )
6331{
6332 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6333 own_cert, pk_key ) );
6334}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006335
6336void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6337 mbedtls_x509_crt *ca_chain,
6338 mbedtls_x509_crl *ca_crl )
6339{
6340 ssl->handshake->sni_ca_chain = ca_chain;
6341 ssl->handshake->sni_ca_crl = ca_crl;
6342}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006343
6344void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6345 int authmode )
6346{
6347 ssl->handshake->sni_authmode = authmode;
6348}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006349#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6350
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006351#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006352/*
6353 * Set EC J-PAKE password for current handshake
6354 */
6355int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6356 const unsigned char *pw,
6357 size_t pw_len )
6358{
6359 mbedtls_ecjpake_role role;
6360
Janos Follath8eb64132016-06-03 15:40:57 +01006361 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006362 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6363
6364 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6365 role = MBEDTLS_ECJPAKE_SERVER;
6366 else
6367 role = MBEDTLS_ECJPAKE_CLIENT;
6368
6369 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6370 role,
6371 MBEDTLS_MD_SHA256,
6372 MBEDTLS_ECP_DP_SECP256R1,
6373 pw, pw_len ) );
6374}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006375#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006377#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006378int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006379 const unsigned char *psk, size_t psk_len,
6380 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006381{
Paul Bakker6db455e2013-09-18 17:29:31 +02006382 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6386 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006387
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006388 /* Identity len will be encoded on two bytes */
6389 if( ( psk_identity_len >> 16 ) != 0 ||
6390 psk_identity_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
6391 {
6392 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6393 }
6394
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006395 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006396 {
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006397 mbedtls_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006398
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006399 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006400 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006401 conf->psk_len = 0;
6402 }
6403 if( conf->psk_identity != NULL )
6404 {
6405 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006406 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006407 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006408 }
6409
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006410 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6411 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006412 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006413 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006414 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006415 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006416 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006417 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006418 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006419
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006420 conf->psk_len = psk_len;
6421 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006422
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006423 memcpy( conf->psk, psk, conf->psk_len );
6424 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006425
6426 return( 0 );
6427}
6428
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006429int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6430 const unsigned char *psk, size_t psk_len )
6431{
6432 if( psk == NULL || ssl->handshake == NULL )
6433 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6434
6435 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6437
6438 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006439 {
6440 mbedtls_zeroize( ssl->handshake->psk, ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006441 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006442 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006443 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006444
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006445 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006446 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006447
6448 ssl->handshake->psk_len = psk_len;
6449 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6450
6451 return( 0 );
6452}
6453
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006454void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006456 size_t),
6457 void *p_psk )
6458{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006459 conf->f_psk = f_psk;
6460 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006462#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006463
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006464#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006465
6466#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006467int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006468{
6469 int ret;
6470
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006471 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6472 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6473 {
6474 mbedtls_mpi_free( &conf->dhm_P );
6475 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006476 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006478
6479 return( 0 );
6480}
Hanno Becker470a8c42017-10-04 15:28:46 +01006481#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006482
Hanno Beckera90658f2017-10-04 15:29:08 +01006483int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6484 const unsigned char *dhm_P, size_t P_len,
6485 const unsigned char *dhm_G, size_t G_len )
6486{
6487 int ret;
6488
6489 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6490 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6491 {
6492 mbedtls_mpi_free( &conf->dhm_P );
6493 mbedtls_mpi_free( &conf->dhm_G );
6494 return( ret );
6495 }
6496
6497 return( 0 );
6498}
Paul Bakker5121ce52009-01-03 21:22:43 +00006499
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006500int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006501{
6502 int ret;
6503
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006504 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6505 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6506 {
6507 mbedtls_mpi_free( &conf->dhm_P );
6508 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006509 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006510 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006511
6512 return( 0 );
6513}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006514#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006515
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006516#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6517/*
6518 * Set the minimum length for Diffie-Hellman parameters
6519 */
6520void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6521 unsigned int bitlen )
6522{
6523 conf->dhm_min_bitlen = bitlen;
6524}
6525#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6526
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006527#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006528/*
6529 * Set allowed/preferred hashes for handshake signatures
6530 */
6531void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6532 const int *hashes )
6533{
6534 conf->sig_hashes = hashes;
6535}
Hanno Becker947194e2017-04-07 13:25:49 +01006536#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006537
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006538#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006539/*
6540 * Set the allowed elliptic curves
6541 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006542void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006543 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006544{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006545 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006546}
Hanno Becker947194e2017-04-07 13:25:49 +01006547#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006548
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006549#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006551{
Hanno Becker947194e2017-04-07 13:25:49 +01006552 /* Initialize to suppress unnecessary compiler warning */
6553 size_t hostname_len = 0;
6554
6555 /* Check if new hostname is valid before
6556 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006557 if( hostname != NULL )
6558 {
6559 hostname_len = strlen( hostname );
6560
6561 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6563 }
6564
6565 /* Now it's clear that we will overwrite the old hostname,
6566 * so we can free it safely */
6567
6568 if( ssl->hostname != NULL )
6569 {
6570 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
6571 mbedtls_free( ssl->hostname );
6572 }
6573
6574 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006575
Paul Bakker5121ce52009-01-03 21:22:43 +00006576 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006577 {
6578 ssl->hostname = NULL;
6579 }
6580 else
6581 {
6582 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006583 if( ssl->hostname == NULL )
6584 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006585
Hanno Becker947194e2017-04-07 13:25:49 +01006586 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006587
Hanno Becker947194e2017-04-07 13:25:49 +01006588 ssl->hostname[hostname_len] = '\0';
6589 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006590
6591 return( 0 );
6592}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006593#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006594
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006595#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006596void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006597 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006598 const unsigned char *, size_t),
6599 void *p_sni )
6600{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006601 conf->f_sni = f_sni;
6602 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006603}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006607int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006608{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006609 size_t cur_len, tot_len;
6610 const char **p;
6611
6612 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006613 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6614 * MUST NOT be truncated."
6615 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006616 */
6617 tot_len = 0;
6618 for( p = protos; *p != NULL; p++ )
6619 {
6620 cur_len = strlen( *p );
6621 tot_len += cur_len;
6622
Ronald Crona32236c2020-04-23 16:41:44 +02006623 if( ( cur_len == 0 ) ||
6624 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
6625 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006627 }
6628
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006629 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006630
6631 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006632}
6633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006634const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006635{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006636 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006637}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006639
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006640void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006641{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006642 conf->max_major_ver = major;
6643 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006644}
6645
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006646void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006647{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006648 conf->min_major_ver = major;
6649 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006650}
6651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006653void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006654{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006655 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006656}
6657#endif
6658
Janos Follath088ce432017-04-10 12:42:31 +01006659#if defined(MBEDTLS_SSL_SRV_C)
6660void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6661 char cert_req_ca_list )
6662{
6663 conf->cert_req_ca_list = cert_req_ca_list;
6664}
6665#endif
6666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006668void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006669{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006670 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006671}
6672#endif
6673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006675void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006676{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006677 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006678}
6679#endif
6680
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006681#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006682void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006683{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006684 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006685}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006686#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006689int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006690{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
6692 mfl_code_to_length[mfl_code] > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006695 }
6696
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006697 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006698
6699 return( 0 );
6700}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006704void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006705{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006706 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006707}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006708#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006711void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006712{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006713 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006714}
6715#endif
6716
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006717void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006718{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006719 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006720}
6721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006723void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006724{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006725 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006726}
6727
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006728void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006729{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006730 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006731}
6732
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006733void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006734 const unsigned char period[8] )
6735{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006736 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006737}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006738#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006741#if defined(MBEDTLS_SSL_CLI_C)
6742void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006743{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006744 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006745}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006746#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006747
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006748#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006749void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6750 mbedtls_ssl_ticket_write_t *f_ticket_write,
6751 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6752 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006753{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006754 conf->f_ticket_write = f_ticket_write;
6755 conf->f_ticket_parse = f_ticket_parse;
6756 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006757}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006758#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006760
Robert Cragie4feb7ae2015-10-02 13:33:37 +01006761#if defined(MBEDTLS_SSL_EXPORT_KEYS)
6762void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
6763 mbedtls_ssl_export_keys_t *f_export_keys,
6764 void *p_export_keys )
6765{
6766 conf->f_export_keys = f_export_keys;
6767 conf->p_export_keys = p_export_keys;
6768}
6769#endif
6770
Paul Bakker5121ce52009-01-03 21:22:43 +00006771/*
6772 * SSL get accessors
6773 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006775{
6776 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6777}
6778
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006779uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006780{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006781 if( ssl->session != NULL )
6782 return( ssl->session->verify_result );
6783
6784 if( ssl->session_negotiate != NULL )
6785 return( ssl->session_negotiate->verify_result );
6786
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006787 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006788}
6789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006791{
Paul Bakker926c8e42013-03-06 10:23:34 +01006792 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006793 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006796}
6797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006798const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006799{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006801 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006802 {
6803 switch( ssl->minor_ver )
6804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006806 return( "DTLSv1.0" );
6807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006809 return( "DTLSv1.2" );
6810
6811 default:
6812 return( "unknown (DTLS)" );
6813 }
6814 }
6815#endif
6816
Paul Bakker43ca69c2011-01-15 17:35:19 +00006817 switch( ssl->minor_ver )
6818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006820 return( "SSLv3.0" );
6821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006822 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006823 return( "TLSv1.0" );
6824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006825 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006826 return( "TLSv1.1" );
6827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00006829 return( "TLSv1.2" );
6830
Paul Bakker43ca69c2011-01-15 17:35:19 +00006831 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006832 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00006833 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00006834}
6835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006837{
Hanno Becker12f7ede2018-08-17 15:28:19 +01006838 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006840 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006841
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006842 if( transform == NULL )
6843 return( (int) mbedtls_ssl_hdr_len( ssl ) );
6844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845#if defined(MBEDTLS_ZLIB_SUPPORT)
6846 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
6847 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006848#endif
6849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 case MBEDTLS_MODE_GCM:
6853 case MBEDTLS_MODE_CCM:
6854 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006855 transform_expansion = transform->minlen;
6856 break;
6857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006858 case MBEDTLS_MODE_CBC:
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006859
6860 block_size = mbedtls_cipher_get_block_size(
6861 &transform->cipher_ctx_enc );
6862
Hanno Becker12f7ede2018-08-17 15:28:19 +01006863 /* Expansion due to the addition of the MAC. */
6864 transform_expansion += transform->maclen;
6865
6866 /* Expansion due to the addition of CBC padding;
6867 * Theoretically up to 256 bytes, but we never use
6868 * more than the block size of the underlying cipher. */
6869 transform_expansion += block_size;
6870
6871 /* For TLS 1.1 or higher, an explicit IV is added
6872 * after the record header. */
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006873#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
6874 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker12f7ede2018-08-17 15:28:19 +01006875 transform_expansion += block_size;
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006876#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker12f7ede2018-08-17 15:28:19 +01006877
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006878 break;
6879
6880 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006882 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006883 }
6884
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006885 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006886}
6887
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02006888#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
6889size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
6890{
6891 size_t max_len;
6892
6893 /*
6894 * Assume mfl_code is correct since it was checked when set
6895 */
6896 max_len = mfl_code_to_length[ssl->conf->mfl_code];
6897
6898 /*
6899 * Check if a smaller max length was negotiated
6900 */
6901 if( ssl->session_out != NULL &&
6902 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6903 {
6904 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6905 }
6906
6907 return max_len;
6908}
6909#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
6910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911#if defined(MBEDTLS_X509_CRT_PARSE_C)
6912const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00006913{
6914 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006915 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006916
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006917 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006918}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00006920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921#if defined(MBEDTLS_SSL_CLI_C)
6922int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006923{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006924 if( ssl == NULL ||
6925 dst == NULL ||
6926 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006927 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006929 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006930 }
6931
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006932 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006933}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006934#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006935
Paul Bakker5121ce52009-01-03 21:22:43 +00006936/*
Paul Bakker1961b702013-01-25 14:49:24 +01006937 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00006938 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006940{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006942
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006943 if( ssl == NULL || ssl->conf == NULL )
6944 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006947 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006949#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006951 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006953#endif
6954
Paul Bakker1961b702013-01-25 14:49:24 +01006955 return( ret );
6956}
6957
6958/*
6959 * Perform the SSL handshake
6960 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01006962{
6963 int ret = 0;
6964
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006965 if( ssl == NULL || ssl->conf == NULL )
6966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01006969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01006971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01006973
6974 if( ret != 0 )
6975 break;
6976 }
6977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006979
6980 return( ret );
6981}
6982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983#if defined(MBEDTLS_SSL_RENEGOTIATION)
6984#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006985/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006986 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00006987 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006989{
6990 int ret;
6991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006993
6994 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6996 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007001 return( ret );
7002 }
7003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007005
7006 return( 0 );
7007}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007009
7010/*
7011 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012 * - any side: calling mbedtls_ssl_renegotiate(),
7013 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7014 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007015 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007016 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007018 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007020{
7021 int ret;
7022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007024
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007025 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7026 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007027
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007028 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7029 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007033 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007034 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007035 ssl->handshake->out_msg_seq = 1;
7036 else
7037 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007038 }
7039#endif
7040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7042 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007044 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007046 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007047 return( ret );
7048 }
7049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007051
7052 return( 0 );
7053}
7054
7055/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007056 * Renegotiate current connection on client,
7057 * or request renegotiation on server
7058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007061 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007062
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007063 if( ssl == NULL || ssl->conf == NULL )
7064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007067 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007068 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007070 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007074
7075 /* Did we already try/start sending HelloRequest? */
7076 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007078
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007079 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007080 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007084 /*
7085 * On client, either start the renegotiation process or,
7086 * if already in progress, continue the handshake
7087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007092
7093 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007096 return( ret );
7097 }
7098 }
7099 else
7100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007101 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007104 return( ret );
7105 }
7106 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007108
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007109 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007110}
7111
7112/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007113 * Check record counters and renegotiate if they're above the limit.
7114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007116{
Andres AG2196c7f2016-12-15 17:01:16 +00007117 size_t ep_len = ssl_ep_len( ssl );
7118 int in_ctr_cmp;
7119 int out_ctr_cmp;
7120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7122 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007123 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007124 {
7125 return( 0 );
7126 }
7127
Andres AG2196c7f2016-12-15 17:01:16 +00007128 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7129 ssl->conf->renego_period + ep_len, 8 - ep_len );
7130 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
7131 ssl->conf->renego_period + ep_len, 8 - ep_len );
7132
7133 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007134 {
7135 return( 0 );
7136 }
7137
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007140}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007142
7143/*
7144 * Receive application data decrypted from the SSL layer
7145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007147{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007148 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007149 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007150
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007151 if( ssl == NULL || ssl->conf == NULL )
7152 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007157 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007160 return( ret );
7161
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007162 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007166 return( ret );
7167 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007168 }
7169#endif
7170
Hanno Becker4a810fb2017-05-24 16:27:30 +01007171 /*
7172 * Check if renegotiation is necessary and/or handshake is
7173 * in process. If yes, perform/continue, and fall through
7174 * if an unexpected packet is received while the client
7175 * is waiting for the ServerHello.
7176 *
7177 * (There is no equivalent to the last condition on
7178 * the server-side as it is not treated as within
7179 * a handshake while waiting for the ClientHello
7180 * after a renegotiation request.)
7181 */
7182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007183#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007184 ret = ssl_check_ctr_renegotiate( ssl );
7185 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7186 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007189 return( ret );
7190 }
7191#endif
7192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007196 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7197 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007200 return( ret );
7201 }
7202 }
7203
7204 if( ssl->in_offt == NULL )
7205 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007206 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007207 if( ssl->f_get_timer != NULL &&
7208 ssl->f_get_timer( ssl->p_timer ) == -1 )
7209 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007210 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007211 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007212
Hanno Becker4a810fb2017-05-24 16:27:30 +01007213 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007214 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007215 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7216 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007217
Hanno Becker4a810fb2017-05-24 16:27:30 +01007218 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7219 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007220 }
7221
7222 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007224 {
7225 /*
7226 * OpenSSL sends empty messages to randomize the IV
7227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007231 return( 0 );
7232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007234 return( ret );
7235 }
7236 }
7237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007241
Hanno Becker4a810fb2017-05-24 16:27:30 +01007242 /*
7243 * - For client-side, expect SERVER_HELLO_REQUEST.
7244 * - For server-side, expect CLIENT_HELLO.
7245 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7246 */
7247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007249 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007251 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007254
7255 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007257 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007258 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007259#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007261 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007262#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007263
Hanno Becker4a810fb2017-05-24 16:27:30 +01007264#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007265 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007269
7270 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007272 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007273 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007274#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007276 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007277#endif /* MBEDTLS_SSL_SRV_C */
7278
Hanno Becker21df7f92017-10-17 11:03:26 +01007279#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007280 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007281 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7282 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7283 ssl->conf->allow_legacy_renegotiation ==
7284 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7285 {
7286 /*
7287 * Accept renegotiation request
7288 */
Paul Bakker48916f92012-09-16 19:57:18 +00007289
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007290 /* DTLS clients need to know renego is server-initiated */
7291#if defined(MBEDTLS_SSL_PROTO_DTLS)
7292 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7293 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7294 {
7295 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7296 }
7297#endif
7298 ret = ssl_start_renegotiation( ssl );
7299 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7300 ret != 0 )
7301 {
7302 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7303 return( ret );
7304 }
7305 }
7306 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007307#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007308 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007309 /*
7310 * Refuse renegotiation
7311 */
7312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007315#if defined(MBEDTLS_SSL_PROTO_SSL3)
7316 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007317 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007318 /* SSLv3 does not have a "no_renegotiation" warning, so
7319 we send a fatal alert and abort the connection. */
7320 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7321 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7322 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007323 }
7324 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7326#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7327 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7328 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7331 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7332 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007333 {
7334 return( ret );
7335 }
Paul Bakker48916f92012-09-16 19:57:18 +00007336 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007337 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7339 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7342 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007343 }
Paul Bakker48916f92012-09-16 19:57:18 +00007344 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007345
Hanno Becker4a810fb2017-05-24 16:27:30 +01007346 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00007347 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007348#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007349 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007350 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007351 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007352 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007353 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007356 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007358 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007359 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007360 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007361#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007363 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7364 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007367 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007368 }
7369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7373 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007374 }
7375
7376 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007377
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007378 /* We're going to return something now, cancel timer,
7379 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007381 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007382
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007383#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007384 /* If we requested renego but received AppData, resend HelloRequest.
7385 * Do it now, after setting in_offt, to avoid taking this branch
7386 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007388 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007390 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007391 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007394 return( ret );
7395 }
7396 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007398#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007399 }
7400
7401 n = ( len < ssl->in_msglen )
7402 ? len : ssl->in_msglen;
7403
7404 memcpy( buf, ssl->in_offt, n );
7405 ssl->in_msglen -= n;
7406
gabor-mezei-armef738752020-07-15 10:55:00 +02007407 /* Zeroising the plaintext buffer to erase unused application data
7408 from the memory. */
7409 mbedtls_zeroize( ssl->in_offt, n );
7410
Paul Bakker5121ce52009-01-03 21:22:43 +00007411 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007412 {
7413 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007414 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007415 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007416 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007417 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007418 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007419 /* more data available */
7420 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007424
Paul Bakker23986e52011-04-24 08:57:21 +00007425 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007426}
7427
7428/*
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007429 * Send application data to be encrypted by the SSL layer, taking care of max
7430 * fragment length and buffer size.
7431 *
7432 * According to RFC 5246 Section 6.2.1:
7433 *
7434 * Zero-length fragments of Application data MAY be sent as they are
7435 * potentially useful as a traffic analysis countermeasure.
7436 *
7437 * Therefore, it is possible that the input message length is 0 and the
7438 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007439 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007440static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007441 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007442{
Paul Bakker23986e52011-04-24 08:57:21 +00007443 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007445 size_t max_len = mbedtls_ssl_get_max_frag_len( ssl );
Florin0b7b83f2017-07-22 09:01:44 +02007446#else
7447 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
7448#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007449 if( len > max_len )
7450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007452 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007455 "maximum fragment length: %d > %d",
7456 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007458 }
7459 else
7460#endif
7461 len = max_len;
7462 }
Paul Bakker887bd502011-06-08 13:10:54 +00007463
Paul Bakker5121ce52009-01-03 21:22:43 +00007464 if( ssl->out_left != 0 )
7465 {
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007466 /*
7467 * The user has previously tried to send the data and
7468 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7469 * written. In this case, we expect the high-level write function
7470 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007475 return( ret );
7476 }
7477 }
Paul Bakker887bd502011-06-08 13:10:54 +00007478 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007479 {
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007480 /*
7481 * The user is trying to send a message the first time, so we need to
7482 * copy the data into the internal buffers and setup the data structure
7483 * to keep track of partial writes
7484 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007485 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007486 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007487 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007492 return( ret );
7493 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007494 }
7495
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007496 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007497}
7498
7499/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007500 * Write application data, doing 1/n-1 splitting if necessary.
7501 *
7502 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007503 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01007504 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007507static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007508 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007509{
7510 int ret;
7511
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007512 if( ssl->conf->cbc_record_splitting ==
7513 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007514 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7516 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7517 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007518 {
7519 return( ssl_write_real( ssl, buf, len ) );
7520 }
7521
7522 if( ssl->split_done == 0 )
7523 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007524 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007525 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007526 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007527 }
7528
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007529 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7530 return( ret );
7531 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007532
7533 return( ret + 1 );
7534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007535#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007536
7537/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007538 * Write application data (public-facing wrapper)
7539 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007540int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007541{
7542 int ret;
7543
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007545
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007546 if( ssl == NULL || ssl->conf == NULL )
7547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7548
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007549#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007550 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7551 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007552 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007553 return( ret );
7554 }
7555#endif
7556
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007557 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007558 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007559 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007560 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007561 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007562 return( ret );
7563 }
7564 }
7565
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007566#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007567 ret = ssl_write_split( ssl, buf, len );
7568#else
7569 ret = ssl_write_real( ssl, buf, len );
7570#endif
7571
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007573
7574 return( ret );
7575}
7576
7577/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007578 * Notify the peer that the connection is being closed
7579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007581{
7582 int ret;
7583
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007584 if( ssl == NULL || ssl->conf == NULL )
7585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007588
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007589 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7595 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7596 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007599 return( ret );
7600 }
7601 }
7602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007604
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007605 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007606}
7607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00007609{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007610 if( transform == NULL )
7611 return;
7612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00007614 deflateEnd( &transform->ctx_deflate );
7615 inflateEnd( &transform->ctx_inflate );
7616#endif
7617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618 mbedtls_cipher_free( &transform->cipher_ctx_enc );
7619 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02007620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621 mbedtls_md_free( &transform->md_ctx_enc );
7622 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02007623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624 mbedtls_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007625}
7626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007627#if defined(MBEDTLS_X509_CRT_PARSE_C)
7628static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007629{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007631
7632 while( cur != NULL )
7633 {
7634 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007636 cur = next;
7637 }
7638}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake )
Paul Bakker48916f92012-09-16 19:57:18 +00007642{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007643 if( handshake == NULL )
7644 return;
7645
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007646#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7647 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7648 mbedtls_md5_free( &handshake->fin_md5 );
7649 mbedtls_sha1_free( &handshake->fin_sha1 );
7650#endif
7651#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7652#if defined(MBEDTLS_SHA256_C)
7653 mbedtls_sha256_free( &handshake->fin_sha256 );
7654#endif
7655#if defined(MBEDTLS_SHA512_C)
7656 mbedtls_sha512_free( &handshake->fin_sha512 );
7657#endif
7658#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660#if defined(MBEDTLS_DHM_C)
7661 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007662#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663#if defined(MBEDTLS_ECDH_C)
7664 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007665#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007666#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007667 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007668#if defined(MBEDTLS_SSL_CLI_C)
7669 mbedtls_free( handshake->ecjpake_cache );
7670 handshake->ecjpake_cache = NULL;
7671 handshake->ecjpake_cache_len = 0;
7672#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007673#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02007674
Janos Follath4ae5c292016-02-10 11:27:43 +00007675#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
7676 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02007677 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007678 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007679#endif
7680
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007681#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7682 if( handshake->psk != NULL )
7683 {
7684 mbedtls_zeroize( handshake->psk, handshake->psk_len );
7685 mbedtls_free( handshake->psk );
7686 }
7687#endif
7688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7690 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007691 /*
7692 * Free only the linked list wrapper, not the keys themselves
7693 * since the belong to the SNI callback
7694 */
7695 if( handshake->sni_key_cert != NULL )
7696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007697 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007698
7699 while( cur != NULL )
7700 {
7701 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007703 cur = next;
7704 }
7705 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708#if defined(MBEDTLS_SSL_PROTO_DTLS)
7709 mbedtls_free( handshake->verify_cookie );
7710 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007711 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007712#endif
7713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714 mbedtls_zeroize( handshake, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007715}
7716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007718{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007719 if( session == NULL )
7720 return;
7721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007723 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725 mbedtls_x509_crt_free( session->peer_cert );
7726 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007727 }
Paul Bakkered27a042013-04-18 22:46:23 +02007728#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007729
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007730#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007732#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734 mbedtls_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007735}
7736
Paul Bakker5121ce52009-01-03 21:22:43 +00007737/*
7738 * Free an SSL context
7739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007741{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007742 if( ssl == NULL )
7743 return;
7744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007746
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007747 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007749 mbedtls_zeroize( ssl->out_buf, MBEDTLS_SSL_BUFFER_LEN );
7750 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007751 }
7752
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007753 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755 mbedtls_zeroize( ssl->in_buf, MBEDTLS_SSL_BUFFER_LEN );
7756 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007757 }
7758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02007760 if( ssl->compress_buf != NULL )
7761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762 mbedtls_zeroize( ssl->compress_buf, MBEDTLS_SSL_BUFFER_LEN );
7763 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02007764 }
7765#endif
7766
Paul Bakker48916f92012-09-16 19:57:18 +00007767 if( ssl->transform )
7768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769 mbedtls_ssl_transform_free( ssl->transform );
7770 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007771 }
7772
7773 if( ssl->handshake )
7774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775 mbedtls_ssl_handshake_free( ssl->handshake );
7776 mbedtls_ssl_transform_free( ssl->transform_negotiate );
7777 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007779 mbedtls_free( ssl->handshake );
7780 mbedtls_free( ssl->transform_negotiate );
7781 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007782 }
7783
Paul Bakkerc0463502013-02-14 11:19:38 +01007784 if( ssl->session )
7785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786 mbedtls_ssl_session_free( ssl->session );
7787 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007788 }
7789
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02007790#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02007791 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007792 {
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007793 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00007795 }
Paul Bakker0be444a2013-08-27 21:55:01 +02007796#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7799 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
7802 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00007803 }
7804#endif
7805
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007806#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007808#endif
7809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00007811
Paul Bakker86f04f42013-02-14 11:20:09 +01007812 /* Actually clear after last debug message */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813 mbedtls_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007814}
7815
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007816/*
7817 * Initialze mbedtls_ssl_config
7818 */
7819void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
7820{
7821 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
7822}
7823
Simon Butcherc97b6972015-12-27 23:48:17 +00007824#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007825static int ssl_preset_default_hashes[] = {
7826#if defined(MBEDTLS_SHA512_C)
7827 MBEDTLS_MD_SHA512,
7828 MBEDTLS_MD_SHA384,
7829#endif
7830#if defined(MBEDTLS_SHA256_C)
7831 MBEDTLS_MD_SHA256,
7832 MBEDTLS_MD_SHA224,
7833#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02007834#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007835 MBEDTLS_MD_SHA1,
7836#endif
7837 MBEDTLS_MD_NONE
7838};
Simon Butcherc97b6972015-12-27 23:48:17 +00007839#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007840
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007841static int ssl_preset_suiteb_ciphersuites[] = {
7842 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
7843 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
7844 0
7845};
7846
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007847#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007848static int ssl_preset_suiteb_hashes[] = {
7849 MBEDTLS_MD_SHA256,
7850 MBEDTLS_MD_SHA384,
7851 MBEDTLS_MD_NONE
7852};
7853#endif
7854
7855#if defined(MBEDTLS_ECP_C)
7856static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007857#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007858 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007859#endif
7860#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007861 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007862#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007863 MBEDTLS_ECP_DP_NONE
7864};
7865#endif
7866
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007867/*
Tillmann Karras588ad502015-09-25 04:27:22 +02007868 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007869 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007870int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007871 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007872{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007873#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007874 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007875#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007876
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02007877 /* Use the functions here so that they are covered in tests,
7878 * but otherwise access member directly for efficiency */
7879 mbedtls_ssl_conf_endpoint( conf, endpoint );
7880 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007881
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007882 /*
7883 * Things that are common to all presets
7884 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007885#if defined(MBEDTLS_SSL_CLI_C)
7886 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7887 {
7888 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
7889#if defined(MBEDTLS_SSL_SESSION_TICKETS)
7890 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
7891#endif
7892 }
7893#endif
7894
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007895#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007896 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007897#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007898
7899#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7900 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
7901#endif
7902
7903#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7904 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
7905#endif
7906
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007907#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7908 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7909#endif
7910
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007911#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007912 conf->f_cookie_write = ssl_cookie_write_dummy;
7913 conf->f_cookie_check = ssl_cookie_check_dummy;
7914#endif
7915
7916#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7917 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7918#endif
7919
Janos Follath088ce432017-04-10 12:42:31 +01007920#if defined(MBEDTLS_SSL_SRV_C)
7921 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
7922#endif
7923
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007924#if defined(MBEDTLS_SSL_PROTO_DTLS)
7925 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7926 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7927#endif
7928
7929#if defined(MBEDTLS_SSL_RENEGOTIATION)
7930 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00007931 memset( conf->renego_period, 0x00, 2 );
7932 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007933#endif
7934
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007935#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
7936 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7937 {
Hanno Becker00d0a682017-10-04 13:14:29 +01007938 const unsigned char dhm_p[] =
7939 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
7940 const unsigned char dhm_g[] =
7941 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
7942
Hanno Beckera90658f2017-10-04 15:29:08 +01007943 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
7944 dhm_p, sizeof( dhm_p ),
7945 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007946 {
7947 return( ret );
7948 }
7949 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007950#endif
7951
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007952 /*
7953 * Preset-specific defaults
7954 */
7955 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007956 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007957 /*
7958 * NSA Suite B
7959 */
7960 case MBEDTLS_SSL_PRESET_SUITEB:
7961 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7962 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7963 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7964 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7965
7966 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7967 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7968 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7969 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7970 ssl_preset_suiteb_ciphersuites;
7971
7972#if defined(MBEDTLS_X509_CRT_PARSE_C)
7973 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007974#endif
7975
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007976#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007977 conf->sig_hashes = ssl_preset_suiteb_hashes;
7978#endif
7979
7980#if defined(MBEDTLS_ECP_C)
7981 conf->curve_list = ssl_preset_suiteb_curves;
7982#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007983 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007984
7985 /*
7986 * Default
7987 */
7988 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03007989 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
7990 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
7991 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7992 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7993 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
7994 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
7995 MBEDTLS_SSL_MIN_MINOR_VERSION :
7996 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007997 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7998 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7999
8000#if defined(MBEDTLS_SSL_PROTO_DTLS)
8001 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8002 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8003#endif
8004
8005 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8006 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8007 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8008 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8009 mbedtls_ssl_list_ciphersuites();
8010
8011#if defined(MBEDTLS_X509_CRT_PARSE_C)
8012 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8013#endif
8014
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008015#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008016 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008017#endif
8018
8019#if defined(MBEDTLS_ECP_C)
8020 conf->curve_list = mbedtls_ecp_grp_id_list();
8021#endif
8022
8023#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8024 conf->dhm_min_bitlen = 1024;
8025#endif
8026 }
8027
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008028 return( 0 );
8029}
8030
8031/*
8032 * Free mbedtls_ssl_config
8033 */
8034void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8035{
8036#if defined(MBEDTLS_DHM_C)
8037 mbedtls_mpi_free( &conf->dhm_P );
8038 mbedtls_mpi_free( &conf->dhm_G );
8039#endif
8040
8041#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8042 if( conf->psk != NULL )
8043 {
8044 mbedtls_zeroize( conf->psk, conf->psk_len );
8045 mbedtls_zeroize( conf->psk_identity, conf->psk_identity_len );
8046 mbedtls_free( conf->psk );
8047 mbedtls_free( conf->psk_identity );
8048 conf->psk_len = 0;
8049 conf->psk_identity_len = 0;
8050 }
8051#endif
8052
8053#if defined(MBEDTLS_X509_CRT_PARSE_C)
8054 ssl_key_cert_free( conf->key_cert );
8055#endif
8056
8057 mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
8058}
8059
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008060#if defined(MBEDTLS_PK_C) && \
8061 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008062/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008063 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008064 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067#if defined(MBEDTLS_RSA_C)
8068 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8069 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008070#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008071#if defined(MBEDTLS_ECDSA_C)
8072 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8073 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008074#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008075 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008076}
8077
Hanno Becker7e5437a2017-04-28 17:15:26 +01008078unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8079{
8080 switch( type ) {
8081 case MBEDTLS_PK_RSA:
8082 return( MBEDTLS_SSL_SIG_RSA );
8083 case MBEDTLS_PK_ECDSA:
8084 case MBEDTLS_PK_ECKEY:
8085 return( MBEDTLS_SSL_SIG_ECDSA );
8086 default:
8087 return( MBEDTLS_SSL_SIG_ANON );
8088 }
8089}
8090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008091mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008092{
8093 switch( sig )
8094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008095#if defined(MBEDTLS_RSA_C)
8096 case MBEDTLS_SSL_SIG_RSA:
8097 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008098#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099#if defined(MBEDTLS_ECDSA_C)
8100 case MBEDTLS_SSL_SIG_ECDSA:
8101 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008102#endif
8103 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008104 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008105 }
8106}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008107#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008108
Hanno Becker7e5437a2017-04-28 17:15:26 +01008109#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8110 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8111
8112/* Find an entry in a signature-hash set matching a given hash algorithm. */
8113mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8114 mbedtls_pk_type_t sig_alg )
8115{
8116 switch( sig_alg )
8117 {
8118 case MBEDTLS_PK_RSA:
8119 return( set->rsa );
8120 case MBEDTLS_PK_ECDSA:
8121 return( set->ecdsa );
8122 default:
8123 return( MBEDTLS_MD_NONE );
8124 }
8125}
8126
8127/* Add a signature-hash-pair to a signature-hash set */
8128void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8129 mbedtls_pk_type_t sig_alg,
8130 mbedtls_md_type_t md_alg )
8131{
8132 switch( sig_alg )
8133 {
8134 case MBEDTLS_PK_RSA:
8135 if( set->rsa == MBEDTLS_MD_NONE )
8136 set->rsa = md_alg;
8137 break;
8138
8139 case MBEDTLS_PK_ECDSA:
8140 if( set->ecdsa == MBEDTLS_MD_NONE )
8141 set->ecdsa = md_alg;
8142 break;
8143
8144 default:
8145 break;
8146 }
8147}
8148
8149/* Allow exactly one hash algorithm for each signature. */
8150void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8151 mbedtls_md_type_t md_alg )
8152{
8153 set->rsa = md_alg;
8154 set->ecdsa = md_alg;
8155}
8156
8157#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8158 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8159
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008160/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008161 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008163mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008164{
8165 switch( hash )
8166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008167#if defined(MBEDTLS_MD5_C)
8168 case MBEDTLS_SSL_HASH_MD5:
8169 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008170#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171#if defined(MBEDTLS_SHA1_C)
8172 case MBEDTLS_SSL_HASH_SHA1:
8173 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008174#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175#if defined(MBEDTLS_SHA256_C)
8176 case MBEDTLS_SSL_HASH_SHA224:
8177 return( MBEDTLS_MD_SHA224 );
8178 case MBEDTLS_SSL_HASH_SHA256:
8179 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008180#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181#if defined(MBEDTLS_SHA512_C)
8182 case MBEDTLS_SSL_HASH_SHA384:
8183 return( MBEDTLS_MD_SHA384 );
8184 case MBEDTLS_SSL_HASH_SHA512:
8185 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008186#endif
8187 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008188 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008189 }
8190}
8191
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008192/*
8193 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8194 */
8195unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8196{
8197 switch( md )
8198 {
8199#if defined(MBEDTLS_MD5_C)
8200 case MBEDTLS_MD_MD5:
8201 return( MBEDTLS_SSL_HASH_MD5 );
8202#endif
8203#if defined(MBEDTLS_SHA1_C)
8204 case MBEDTLS_MD_SHA1:
8205 return( MBEDTLS_SSL_HASH_SHA1 );
8206#endif
8207#if defined(MBEDTLS_SHA256_C)
8208 case MBEDTLS_MD_SHA224:
8209 return( MBEDTLS_SSL_HASH_SHA224 );
8210 case MBEDTLS_MD_SHA256:
8211 return( MBEDTLS_SSL_HASH_SHA256 );
8212#endif
8213#if defined(MBEDTLS_SHA512_C)
8214 case MBEDTLS_MD_SHA384:
8215 return( MBEDTLS_SSL_HASH_SHA384 );
8216 case MBEDTLS_MD_SHA512:
8217 return( MBEDTLS_SSL_HASH_SHA512 );
8218#endif
8219 default:
8220 return( MBEDTLS_SSL_HASH_NONE );
8221 }
8222}
8223
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008224#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008225/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008226 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008227 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008228 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008229int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008230{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008231 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008232
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008233 if( ssl->conf->curve_list == NULL )
8234 return( -1 );
8235
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008236 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008237 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008238 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008239
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008240 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008241}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008242#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008243
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008244#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008245/*
8246 * Check if a hash proposed by the peer is in our list.
8247 * Return 0 if we're willing to use it, -1 otherwise.
8248 */
8249int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8250 mbedtls_md_type_t md )
8251{
8252 const int *cur;
8253
8254 if( ssl->conf->sig_hashes == NULL )
8255 return( -1 );
8256
8257 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8258 if( *cur == (int) md )
8259 return( 0 );
8260
8261 return( -1 );
8262}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008263#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008265#if defined(MBEDTLS_X509_CRT_PARSE_C)
8266int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8267 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008268 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008269 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008270{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008271 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008273 int usage = 0;
8274#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008276 const char *ext_oid;
8277 size_t ext_len;
8278#endif
8279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8281 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008282 ((void) cert);
8283 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008284 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008285#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8288 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008289 {
8290 /* Server part of the key exchange */
8291 switch( ciphersuite->key_exchange )
8292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293 case MBEDTLS_KEY_EXCHANGE_RSA:
8294 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008295 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008296 break;
8297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008298 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8299 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8300 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8301 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008302 break;
8303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8305 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008306 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008307 break;
8308
8309 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310 case MBEDTLS_KEY_EXCHANGE_NONE:
8311 case MBEDTLS_KEY_EXCHANGE_PSK:
8312 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8313 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008314 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008315 usage = 0;
8316 }
8317 }
8318 else
8319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8321 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008322 }
8323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008325 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008326 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008327 ret = -1;
8328 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008329#else
8330 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8334 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8337 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008338 }
8339 else
8340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8342 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008343 }
8344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008346 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008347 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008348 ret = -1;
8349 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008351
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008352 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008355
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008356/*
8357 * Convert version numbers to/from wire format
8358 * and, for DTLS, to/from TLS equivalent.
8359 *
8360 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008361 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008362 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8363 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008366 unsigned char ver[2] )
8367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008368#if defined(MBEDTLS_SSL_PROTO_DTLS)
8369 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008371 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008372 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8373
8374 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8375 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8376 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008377 else
8378#else
8379 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008380#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008381 {
8382 ver[0] = (unsigned char) major;
8383 ver[1] = (unsigned char) minor;
8384 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008385}
8386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008387void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008388 const unsigned char ver[2] )
8389{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008390#if defined(MBEDTLS_SSL_PROTO_DTLS)
8391 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008392 {
8393 *major = 255 - ver[0] + 2;
8394 *minor = 255 - ver[1] + 1;
8395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008397 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8398 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008399 else
8400#else
8401 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008402#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008403 {
8404 *major = ver[0];
8405 *minor = ver[1];
8406 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008407}
8408
Simon Butcher99000142016-10-13 17:21:01 +01008409int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8410{
8411#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8412 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8413 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8414
8415 switch( md )
8416 {
8417#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8418#if defined(MBEDTLS_MD5_C)
8419 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008420 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008421#endif
8422#if defined(MBEDTLS_SHA1_C)
8423 case MBEDTLS_SSL_HASH_SHA1:
8424 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8425 break;
8426#endif
8427#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8428#if defined(MBEDTLS_SHA512_C)
8429 case MBEDTLS_SSL_HASH_SHA384:
8430 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8431 break;
8432#endif
8433#if defined(MBEDTLS_SHA256_C)
8434 case MBEDTLS_SSL_HASH_SHA256:
8435 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8436 break;
8437#endif
8438 default:
8439 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8440 }
8441
8442 return 0;
8443#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8444 (void) ssl;
8445 (void) md;
8446
8447 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8448#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8449}
8450
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008451#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8452 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8453int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8454 unsigned char *output,
8455 unsigned char *data, size_t data_len )
8456{
8457 int ret = 0;
8458 mbedtls_md5_context mbedtls_md5;
8459 mbedtls_sha1_context mbedtls_sha1;
8460
8461 mbedtls_md5_init( &mbedtls_md5 );
8462 mbedtls_sha1_init( &mbedtls_sha1 );
8463
8464 /*
8465 * digitally-signed struct {
8466 * opaque md5_hash[16];
8467 * opaque sha_hash[20];
8468 * };
8469 *
8470 * md5_hash
8471 * MD5(ClientHello.random + ServerHello.random
8472 * + ServerParams);
8473 * sha_hash
8474 * SHA(ClientHello.random + ServerHello.random
8475 * + ServerParams);
8476 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008477 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008478 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008479 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008480 goto exit;
8481 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008482 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008483 ssl->handshake->randbytes, 64 ) ) != 0 )
8484 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008486 goto exit;
8487 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008488 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008489 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008490 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008491 goto exit;
8492 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008493 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008494 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008495 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008496 goto exit;
8497 }
8498
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008499 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008500 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008502 goto exit;
8503 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008504 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008505 ssl->handshake->randbytes, 64 ) ) != 0 )
8506 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008508 goto exit;
8509 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008510 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008511 data_len ) ) != 0 )
8512 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008513 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008514 goto exit;
8515 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008516 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008517 output + 16 ) ) != 0 )
8518 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008519 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008520 goto exit;
8521 }
8522
8523exit:
8524 mbedtls_md5_free( &mbedtls_md5 );
8525 mbedtls_sha1_free( &mbedtls_sha1 );
8526
8527 if( ret != 0 )
8528 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8529 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8530
8531 return( ret );
8532
8533}
8534#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
8535 MBEDTLS_SSL_PROTO_TLS1_1 */
8536
8537#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8538 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8539int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8540 unsigned char *output,
8541 unsigned char *data, size_t data_len,
8542 mbedtls_md_type_t md_alg )
8543{
8544 int ret = 0;
8545 mbedtls_md_context_t ctx;
8546 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8547
8548 mbedtls_md_init( &ctx );
8549
8550 /*
8551 * digitally-signed struct {
8552 * opaque client_random[32];
8553 * opaque server_random[32];
8554 * ServerDHParams params;
8555 * };
8556 */
8557 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8558 {
8559 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8560 goto exit;
8561 }
8562 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8563 {
8564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8565 goto exit;
8566 }
8567 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8568 {
8569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8570 goto exit;
8571 }
8572 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8573 {
8574 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8575 goto exit;
8576 }
8577 if( ( ret = mbedtls_md_finish( &ctx, output ) ) != 0 )
8578 {
8579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8580 goto exit;
8581 }
8582
8583exit:
8584 mbedtls_md_free( &ctx );
8585
8586 if( ret != 0 )
8587 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8588 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8589
8590 return( ret );
8591}
8592#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
8593 MBEDTLS_SSL_PROTO_TLS1_2 */
8594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595#endif /* MBEDTLS_SSL_TLS_C */