blob: 2b1aa0110fbcb32aabdbd60830dc8bdd99208006 [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)
Rodrigo Dias Corread7853a82020-11-25 00:42:28 -03005408
Rodrigo Dias Correaf75fbab2020-11-25 07:30:26 -03005409typedef int (*finish_sha384_t)(mbedtls_sha512_context*, unsigned char*);
Rodrigo Dias Corread7853a82020-11-25 00:42:28 -03005410
Paul Bakkerca4ab492012-04-18 14:23:57 +00005411static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005412 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005413{
5414 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005415 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005416 mbedtls_sha512_context sha512;
Rodrigo Dias Corread7853a82020-11-25 00:42:28 -03005417 unsigned char padbuf[48];
Rodrigo Dias Correa9c7e92b2020-11-28 14:59:56 -03005418 /*
5419 * For SHA-384, we can save 16 bytes by keeping padbuf 48 bytes long.
5420 * However, to avoid stringop-overflow warning in gcc, we have to cast
5421 * mbedtls_sha512_finish_ret().
5422 */
5423 finish_sha384_t finish_sha384 = (finish_sha384_t)mbedtls_sha512_finish_ret;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005425 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005426 if( !session )
5427 session = ssl->session;
5428
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005429 mbedtls_sha512_init( &sha512 );
5430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005432
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005433 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005434
5435 /*
5436 * TLSv1.2:
5437 * hash = PRF( master, finished_label,
5438 * Hash( handshake ) )[0.11]
5439 */
5440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005441#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005442 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5443 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005444#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005447 ? "client finished"
5448 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005449
Rodrigo Dias Correa9c7e92b2020-11-28 14:59:56 -03005450 finish_sha384( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005451
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005452 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005453 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005456
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005457 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005459 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005462}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005463#endif /* MBEDTLS_SHA512_C */
5464#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005467{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005469
5470 /*
5471 * Free our handshake params
5472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 mbedtls_ssl_handshake_free( ssl->handshake );
5474 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005475 ssl->handshake = NULL;
5476
5477 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005478 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005479 */
5480 if( ssl->transform )
5481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482 mbedtls_ssl_transform_free( ssl->transform );
5483 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005484 }
5485 ssl->transform = ssl->transform_negotiate;
5486 ssl->transform_negotiate = NULL;
5487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005489}
5490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005491void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005492{
5493 int resume = ssl->handshake->resume;
5494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497#if defined(MBEDTLS_SSL_RENEGOTIATION)
5498 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005500 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005501 ssl->renego_records_seen = 0;
5502 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005503#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005504
5505 /*
5506 * Free the previous session and switch in the current one
5507 */
Paul Bakker0a597072012-09-25 21:55:46 +00005508 if( ssl->session )
5509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005510#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005511 /* RFC 7366 3.1: keep the EtM state */
5512 ssl->session_negotiate->encrypt_then_mac =
5513 ssl->session->encrypt_then_mac;
5514#endif
5515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005516 mbedtls_ssl_session_free( ssl->session );
5517 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005518 }
5519 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005520 ssl->session_negotiate = NULL;
5521
Paul Bakker0a597072012-09-25 21:55:46 +00005522 /*
5523 * Add cache entry
5524 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005525 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005526 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005527 resume == 0 )
5528 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005529 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005531 }
Paul Bakker0a597072012-09-25 21:55:46 +00005532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005533#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005534 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005535 ssl->handshake->flight != NULL )
5536 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005537 /* Cancel handshake timer */
5538 ssl_set_timer( ssl, 0 );
5539
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005540 /* Keep last flight around in case we need to resend it:
5541 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005543 }
5544 else
5545#endif
5546 ssl_handshake_wrapup_free_hs_transform( ssl );
5547
Paul Bakker48916f92012-09-16 19:57:18 +00005548 ssl->state++;
5549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005551}
5552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005553int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005554{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005555 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005558
Paul Bakker92be97b2013-01-02 17:30:03 +01005559 /*
5560 * Set the out_msg pointer to the correct location based on IV length
5561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005562 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005563 {
5564 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5565 ssl->transform_negotiate->fixed_ivlen;
5566 }
5567 else
5568 ssl->out_msg = ssl->out_iv;
5569
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005570 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005571
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005572 /*
5573 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5574 * may define some other value. Currently (early 2016), no defined
5575 * ciphersuite does this (and this is unlikely to change as activity has
5576 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005578 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005581 ssl->verify_data_len = hash_len;
5582 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005583#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005584
Paul Bakker5121ce52009-01-03 21:22:43 +00005585 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005586 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5587 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005588
5589 /*
5590 * In case of session resuming, invert the client and server
5591 * ChangeCipherSpec messages order.
5592 */
Paul Bakker0a597072012-09-25 21:55:46 +00005593 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005596 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005597 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005598#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005600 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005601 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005602#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005603 }
5604 else
5605 ssl->state++;
5606
Paul Bakker48916f92012-09-16 19:57:18 +00005607 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005608 * Switch to our negotiated transform and session parameters for outbound
5609 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005614 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005615 {
5616 unsigned char i;
5617
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005618 /* Remember current epoch settings for resending */
5619 ssl->handshake->alt_transform_out = ssl->transform_out;
5620 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5621
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005622 /* Set sequence_number to zero */
5623 memset( ssl->out_ctr + 2, 0, 6 );
5624
5625 /* Increment epoch */
5626 for( i = 2; i > 0; i-- )
5627 if( ++ssl->out_ctr[i - 1] != 0 )
5628 break;
5629
5630 /* The loop goes to its end iff the counter is wrapping */
5631 if( i == 0 )
5632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5634 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005635 }
5636 }
5637 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005639 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005640
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005641 ssl->transform_out = ssl->transform_negotiate;
5642 ssl->session_out = ssl->session_negotiate;
5643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005644#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5645 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005647 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5650 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005651 }
5652 }
5653#endif
5654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005656 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005657 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005658#endif
5659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005660 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005662 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005663 return( ret );
5664 }
5665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005667
5668 return( 0 );
5669}
5670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005671#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005672#define SSL_MAX_HASH_LEN 36
5673#else
5674#define SSL_MAX_HASH_LEN 12
5675#endif
5676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005678{
Paul Bakker23986e52011-04-24 08:57:21 +00005679 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005680 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005681 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005684
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005685 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005690 return( ret );
5691 }
5692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005693 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005696 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5697 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005698 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005699 }
5700
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005701 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702#if defined(MBEDTLS_SSL_PROTO_SSL3)
5703 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005704 hash_len = 36;
5705 else
5706#endif
5707 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5710 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
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( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005719 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005722 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5723 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005724 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005725 }
5726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005728 ssl->verify_data_len = hash_len;
5729 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005730#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005731
Paul Bakker0a597072012-09-25 21:55:46 +00005732 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005735 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005736 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005737#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005739 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005741#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005742 }
5743 else
5744 ssl->state++;
5745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005747 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005748 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005749#endif
5750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005752
5753 return( 0 );
5754}
5755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005756static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005757{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005760#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5761 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5762 mbedtls_md5_init( &handshake->fin_md5 );
5763 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005764 mbedtls_md5_starts_ret( &handshake->fin_md5 );
5765 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005766#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005767#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5768#if defined(MBEDTLS_SHA256_C)
5769 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005770 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005771#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005772#if defined(MBEDTLS_SHA512_C)
5773 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005774 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005775#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005776#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005777
5778 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005779
5780#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5781 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5782 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5783#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785#if defined(MBEDTLS_DHM_C)
5786 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005787#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005788#if defined(MBEDTLS_ECDH_C)
5789 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005790#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02005791#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005792 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02005793#if defined(MBEDTLS_SSL_CLI_C)
5794 handshake->ecjpake_cache = NULL;
5795 handshake->ecjpake_cache_len = 0;
5796#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005797#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005798
5799#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5800 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5801#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005802}
5803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005805{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005808 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5809 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811 mbedtls_md_init( &transform->md_ctx_enc );
5812 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005813}
5814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005815void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005816{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005818}
5819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005821{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005822 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005823 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005825 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005826 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005827 if( ssl->handshake )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005828 mbedtls_ssl_handshake_free( ssl->handshake );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005829
5830 /*
5831 * Either the pointers are now NULL or cleared properly and can be freed.
5832 * Now allocate missing structures.
5833 */
5834 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005835 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005836 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005837 }
Paul Bakker48916f92012-09-16 19:57:18 +00005838
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005839 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005840 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005841 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005842 }
Paul Bakker48916f92012-09-16 19:57:18 +00005843
Paul Bakker82788fb2014-10-20 13:59:19 +02005844 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005845 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005846 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005847 }
Paul Bakker48916f92012-09-16 19:57:18 +00005848
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005849 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005850 if( ssl->handshake == NULL ||
5851 ssl->transform_negotiate == NULL ||
5852 ssl->session_negotiate == NULL )
5853 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 mbedtls_free( ssl->handshake );
5857 mbedtls_free( ssl->transform_negotiate );
5858 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005859
5860 ssl->handshake = NULL;
5861 ssl->transform_negotiate = NULL;
5862 ssl->session_negotiate = NULL;
5863
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005864 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005865 }
5866
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005867 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005869 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005870 ssl_handshake_params_init( ssl->handshake );
5871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005873 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5874 {
5875 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005876
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005877 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5878 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5879 else
5880 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005881
5882 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005883 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005884#endif
5885
Paul Bakker48916f92012-09-16 19:57:18 +00005886 return( 0 );
5887}
5888
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005889#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005890/* Dummy cookie callbacks for defaults */
5891static int ssl_cookie_write_dummy( void *ctx,
5892 unsigned char **p, unsigned char *end,
5893 const unsigned char *cli_id, size_t cli_id_len )
5894{
5895 ((void) ctx);
5896 ((void) p);
5897 ((void) end);
5898 ((void) cli_id);
5899 ((void) cli_id_len);
5900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005901 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005902}
5903
5904static int ssl_cookie_check_dummy( void *ctx,
5905 const unsigned char *cookie, size_t cookie_len,
5906 const unsigned char *cli_id, size_t cli_id_len )
5907{
5908 ((void) ctx);
5909 ((void) cookie);
5910 ((void) cookie_len);
5911 ((void) cli_id);
5912 ((void) cli_id_len);
5913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005914 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005915}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005916#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005917
Paul Bakker5121ce52009-01-03 21:22:43 +00005918/*
5919 * Initialize an SSL context
5920 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005921void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
5922{
5923 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
5924}
5925
5926/*
5927 * Setup an SSL context
5928 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005929int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02005930 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00005931{
Paul Bakker48916f92012-09-16 19:57:18 +00005932 int ret;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005933 const size_t len = MBEDTLS_SSL_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005934
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005935 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00005936
5937 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005938 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00005939 */
k-stachowiakc2eddee2018-07-09 10:39:20 +02005940 ssl->in_buf = NULL;
5941 ssl->out_buf = NULL;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005942 if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL ||
5943 ( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005944 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", len ) );
k-stachowiak2c161142018-07-31 16:52:32 +02005946 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiakc2eddee2018-07-09 10:39:20 +02005947 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005948 }
5949
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005950#if defined(MBEDTLS_SSL_PROTO_DTLS)
5951 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5952 {
5953 ssl->out_hdr = ssl->out_buf;
5954 ssl->out_ctr = ssl->out_buf + 3;
5955 ssl->out_len = ssl->out_buf + 11;
5956 ssl->out_iv = ssl->out_buf + 13;
5957 ssl->out_msg = ssl->out_buf + 13;
5958
5959 ssl->in_hdr = ssl->in_buf;
5960 ssl->in_ctr = ssl->in_buf + 3;
5961 ssl->in_len = ssl->in_buf + 11;
5962 ssl->in_iv = ssl->in_buf + 13;
5963 ssl->in_msg = ssl->in_buf + 13;
5964 }
5965 else
5966#endif
5967 {
5968 ssl->out_ctr = ssl->out_buf;
5969 ssl->out_hdr = ssl->out_buf + 8;
5970 ssl->out_len = ssl->out_buf + 11;
5971 ssl->out_iv = ssl->out_buf + 13;
5972 ssl->out_msg = ssl->out_buf + 13;
5973
5974 ssl->in_ctr = ssl->in_buf;
5975 ssl->in_hdr = ssl->in_buf + 8;
5976 ssl->in_len = ssl->in_buf + 11;
5977 ssl->in_iv = ssl->in_buf + 13;
5978 ssl->in_msg = ssl->in_buf + 13;
5979 }
5980
Paul Bakker48916f92012-09-16 19:57:18 +00005981 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiakc2eddee2018-07-09 10:39:20 +02005982 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005983
5984 return( 0 );
k-stachowiakc2eddee2018-07-09 10:39:20 +02005985
5986error:
5987 mbedtls_free( ssl->in_buf );
5988 mbedtls_free( ssl->out_buf );
5989
5990 ssl->conf = NULL;
5991
5992 ssl->in_buf = NULL;
5993 ssl->out_buf = NULL;
5994
5995 ssl->in_hdr = NULL;
5996 ssl->in_ctr = NULL;
5997 ssl->in_len = NULL;
5998 ssl->in_iv = NULL;
5999 ssl->in_msg = NULL;
6000
6001 ssl->out_hdr = NULL;
6002 ssl->out_ctr = NULL;
6003 ssl->out_len = NULL;
6004 ssl->out_iv = NULL;
6005 ssl->out_msg = NULL;
6006
k-stachowiak2c161142018-07-31 16:52:32 +02006007 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006008}
6009
6010/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006011 * Reset an initialized and used SSL context for re-use while retaining
6012 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006013 *
6014 * If partial is non-zero, keep data in the input buffer and client ID.
6015 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006016 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006017static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006018{
Paul Bakker48916f92012-09-16 19:57:18 +00006019 int ret;
6020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006022
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006023 /* Cancel any possibly running timer */
6024 ssl_set_timer( ssl, 0 );
6025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026#if defined(MBEDTLS_SSL_RENEGOTIATION)
6027 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006028 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006029
6030 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6032 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006033#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006035
Paul Bakker7eb013f2011-10-06 12:37:39 +00006036 ssl->in_offt = NULL;
6037
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006038 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006039 ssl->in_msgtype = 0;
6040 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006041 if( partial == 0 )
6042 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006044 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006045 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006046#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006048 ssl_dtls_replay_reset( ssl );
6049#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006050
6051 ssl->in_hslen = 0;
6052 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006053
6054 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006055
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006056 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006057 ssl->out_msgtype = 0;
6058 ssl->out_msglen = 0;
6059 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6061 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006062 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006063#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006064
Paul Bakker48916f92012-09-16 19:57:18 +00006065 ssl->transform_in = NULL;
6066 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006067
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006068 ssl->session_in = NULL;
6069 ssl->session_out = NULL;
6070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071 memset( ssl->out_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006072
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006073 if( partial == 0 )
6074 memset( ssl->in_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00006075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006076#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6077 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6080 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6083 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006084 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006085 }
6086#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006087
Paul Bakker48916f92012-09-16 19:57:18 +00006088 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006090 mbedtls_ssl_transform_free( ssl->transform );
6091 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006092 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006093 }
Paul Bakker48916f92012-09-16 19:57:18 +00006094
Paul Bakkerc0463502013-02-14 11:19:38 +01006095 if( ssl->session )
6096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097 mbedtls_ssl_session_free( ssl->session );
6098 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006099 ssl->session = NULL;
6100 }
6101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006103 ssl->alpn_chosen = NULL;
6104#endif
6105
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006106#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006107 if( partial == 0 )
6108 {
6109 mbedtls_free( ssl->cli_id );
6110 ssl->cli_id = NULL;
6111 ssl->cli_id_len = 0;
6112 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006113#endif
6114
Paul Bakker48916f92012-09-16 19:57:18 +00006115 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6116 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006117
6118 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006119}
6120
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006121/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006122 * Reset an initialized and used SSL context for re-use while retaining
6123 * all application-set variables, function pointers and data.
6124 */
6125int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6126{
6127 return( ssl_session_reset_int( ssl, 0 ) );
6128}
6129
6130/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006131 * SSL set accessors
6132 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006133void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006134{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006135 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006136}
6137
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006138void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006139{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006140 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006141}
6142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006143#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006144void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006145{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006146 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006147}
6148#endif
6149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006151void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006152{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006153 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006154}
6155#endif
6156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006158void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006159{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006160 conf->hs_timeout_min = min;
6161 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006162}
6163#endif
6164
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006165void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006166{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006167 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006168}
6169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006171void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006172 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006173 void *p_vrfy )
6174{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006175 conf->f_vrfy = f_vrfy;
6176 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006177}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006179
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006180void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006181 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006182 void *p_rng )
6183{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006184 conf->f_rng = f_rng;
6185 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006186}
6187
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006188void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006189 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006190 void *p_dbg )
6191{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006192 conf->f_dbg = f_dbg;
6193 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006194}
6195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006197 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006198 mbedtls_ssl_send_t *f_send,
6199 mbedtls_ssl_recv_t *f_recv,
6200 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006201{
6202 ssl->p_bio = p_bio;
6203 ssl->f_send = f_send;
6204 ssl->f_recv = f_recv;
6205 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006206}
6207
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006208void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006209{
6210 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006211}
6212
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006213void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6214 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006215 mbedtls_ssl_set_timer_t *f_set_timer,
6216 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006217{
6218 ssl->p_timer = p_timer;
6219 ssl->f_set_timer = f_set_timer;
6220 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006221
6222 /* Make sure we start with no timer running */
6223 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006224}
6225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006226#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006227void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006228 void *p_cache,
6229 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6230 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006231{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006232 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006233 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006234 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006235}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006236#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238#if defined(MBEDTLS_SSL_CLI_C)
6239int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006240{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006241 int ret;
6242
6243 if( ssl == NULL ||
6244 session == NULL ||
6245 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006246 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006249 }
6250
6251 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6252 return( ret );
6253
Paul Bakker0a597072012-09-25 21:55:46 +00006254 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006255
6256 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006257}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006258#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006259
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006260void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006261 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006262{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006263 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6264 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6265 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6266 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006267}
6268
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006269void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006270 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006271 int major, int minor )
6272{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006273 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006274 return;
6275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006277 return;
6278
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006279 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006280}
6281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006283void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006284 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006285{
6286 conf->cert_profile = profile;
6287}
6288
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006289/* Append a new keycert entry to a (possibly empty) list */
6290static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6291 mbedtls_x509_crt *cert,
6292 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006293{
niisatoa35dbf12018-06-25 19:05:48 +09006294 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006295
niisatoa35dbf12018-06-25 19:05:48 +09006296 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6297 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006298 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006299
niisatoa35dbf12018-06-25 19:05:48 +09006300 new_cert->cert = cert;
6301 new_cert->key = key;
6302 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006303
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006304 /* Update head is the list was null, else add to the end */
6305 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006306 {
niisatoa35dbf12018-06-25 19:05:48 +09006307 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006308 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006309 else
6310 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006311 mbedtls_ssl_key_cert *cur = *head;
6312 while( cur->next != NULL )
6313 cur = cur->next;
niisatoa35dbf12018-06-25 19:05:48 +09006314 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006315 }
6316
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006317 return( 0 );
6318}
6319
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006320int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006321 mbedtls_x509_crt *own_cert,
6322 mbedtls_pk_context *pk_key )
6323{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006324 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006325}
6326
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006327void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006328 mbedtls_x509_crt *ca_chain,
6329 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006330{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006331 conf->ca_chain = ca_chain;
6332 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006335
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006336#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6337int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6338 mbedtls_x509_crt *own_cert,
6339 mbedtls_pk_context *pk_key )
6340{
6341 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6342 own_cert, pk_key ) );
6343}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006344
6345void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6346 mbedtls_x509_crt *ca_chain,
6347 mbedtls_x509_crl *ca_crl )
6348{
6349 ssl->handshake->sni_ca_chain = ca_chain;
6350 ssl->handshake->sni_ca_crl = ca_crl;
6351}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006352
6353void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6354 int authmode )
6355{
6356 ssl->handshake->sni_authmode = authmode;
6357}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006358#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6359
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006360#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006361/*
6362 * Set EC J-PAKE password for current handshake
6363 */
6364int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6365 const unsigned char *pw,
6366 size_t pw_len )
6367{
6368 mbedtls_ecjpake_role role;
6369
Janos Follath8eb64132016-06-03 15:40:57 +01006370 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006371 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6372
6373 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6374 role = MBEDTLS_ECJPAKE_SERVER;
6375 else
6376 role = MBEDTLS_ECJPAKE_CLIENT;
6377
6378 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6379 role,
6380 MBEDTLS_MD_SHA256,
6381 MBEDTLS_ECP_DP_SECP256R1,
6382 pw, pw_len ) );
6383}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006384#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006387int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006388 const unsigned char *psk, size_t psk_len,
6389 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006390{
Paul Bakker6db455e2013-09-18 17:29:31 +02006391 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006392 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006394 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6395 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006396
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006397 /* Identity len will be encoded on two bytes */
6398 if( ( psk_identity_len >> 16 ) != 0 ||
6399 psk_identity_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
6400 {
6401 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6402 }
6403
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006404 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006405 {
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006406 mbedtls_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006407
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006408 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006409 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006410 conf->psk_len = 0;
6411 }
6412 if( conf->psk_identity != NULL )
6413 {
6414 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006415 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006416 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006417 }
6418
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006419 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6420 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006421 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006422 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006423 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006424 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006425 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006426 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006427 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006428
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006429 conf->psk_len = psk_len;
6430 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006431
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006432 memcpy( conf->psk, psk, conf->psk_len );
6433 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006434
6435 return( 0 );
6436}
6437
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006438int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6439 const unsigned char *psk, size_t psk_len )
6440{
6441 if( psk == NULL || ssl->handshake == NULL )
6442 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6443
6444 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6445 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6446
6447 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006448 {
6449 mbedtls_zeroize( ssl->handshake->psk, ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006450 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006451 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006452 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006453
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006454 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006455 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006456
6457 ssl->handshake->psk_len = psk_len;
6458 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6459
6460 return( 0 );
6461}
6462
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006463void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006465 size_t),
6466 void *p_psk )
6467{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006468 conf->f_psk = f_psk;
6469 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006470}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006472
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006473#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006474
6475#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006476int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006477{
6478 int ret;
6479
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006480 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6481 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6482 {
6483 mbedtls_mpi_free( &conf->dhm_P );
6484 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006485 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006486 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006487
6488 return( 0 );
6489}
Hanno Becker470a8c42017-10-04 15:28:46 +01006490#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006491
Hanno Beckera90658f2017-10-04 15:29:08 +01006492int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6493 const unsigned char *dhm_P, size_t P_len,
6494 const unsigned char *dhm_G, size_t G_len )
6495{
6496 int ret;
6497
6498 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6499 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6500 {
6501 mbedtls_mpi_free( &conf->dhm_P );
6502 mbedtls_mpi_free( &conf->dhm_G );
6503 return( ret );
6504 }
6505
6506 return( 0 );
6507}
Paul Bakker5121ce52009-01-03 21:22:43 +00006508
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006509int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006510{
6511 int ret;
6512
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006513 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6514 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6515 {
6516 mbedtls_mpi_free( &conf->dhm_P );
6517 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006518 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006519 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006520
6521 return( 0 );
6522}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006523#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006524
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006525#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6526/*
6527 * Set the minimum length for Diffie-Hellman parameters
6528 */
6529void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6530 unsigned int bitlen )
6531{
6532 conf->dhm_min_bitlen = bitlen;
6533}
6534#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6535
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006536#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006537/*
6538 * Set allowed/preferred hashes for handshake signatures
6539 */
6540void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6541 const int *hashes )
6542{
6543 conf->sig_hashes = hashes;
6544}
Hanno Becker947194e2017-04-07 13:25:49 +01006545#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006546
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006547#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006548/*
6549 * Set the allowed elliptic curves
6550 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006551void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006552 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006553{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006554 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006555}
Hanno Becker947194e2017-04-07 13:25:49 +01006556#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006557
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006558#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006560{
Hanno Becker947194e2017-04-07 13:25:49 +01006561 /* Initialize to suppress unnecessary compiler warning */
6562 size_t hostname_len = 0;
6563
6564 /* Check if new hostname is valid before
6565 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006566 if( hostname != NULL )
6567 {
6568 hostname_len = strlen( hostname );
6569
6570 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6572 }
6573
6574 /* Now it's clear that we will overwrite the old hostname,
6575 * so we can free it safely */
6576
6577 if( ssl->hostname != NULL )
6578 {
6579 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
6580 mbedtls_free( ssl->hostname );
6581 }
6582
6583 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006584
Paul Bakker5121ce52009-01-03 21:22:43 +00006585 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006586 {
6587 ssl->hostname = NULL;
6588 }
6589 else
6590 {
6591 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006592 if( ssl->hostname == NULL )
6593 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006594
Hanno Becker947194e2017-04-07 13:25:49 +01006595 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006596
Hanno Becker947194e2017-04-07 13:25:49 +01006597 ssl->hostname[hostname_len] = '\0';
6598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006599
6600 return( 0 );
6601}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006602#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006603
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006604#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006605void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006607 const unsigned char *, size_t),
6608 void *p_sni )
6609{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006610 conf->f_sni = f_sni;
6611 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006612}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006616int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006617{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006618 size_t cur_len, tot_len;
6619 const char **p;
6620
6621 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006622 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6623 * MUST NOT be truncated."
6624 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006625 */
6626 tot_len = 0;
6627 for( p = protos; *p != NULL; p++ )
6628 {
6629 cur_len = strlen( *p );
6630 tot_len += cur_len;
6631
Ronald Crona32236c2020-04-23 16:41:44 +02006632 if( ( cur_len == 0 ) ||
6633 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
6634 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006636 }
6637
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006638 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006639
6640 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006641}
6642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006644{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006645 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006646}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006648
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006649void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006650{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006651 conf->max_major_ver = major;
6652 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006653}
6654
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006655void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006656{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006657 conf->min_major_ver = major;
6658 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006659}
6660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006661#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006662void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006663{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006664 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006665}
6666#endif
6667
Janos Follath088ce432017-04-10 12:42:31 +01006668#if defined(MBEDTLS_SSL_SRV_C)
6669void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6670 char cert_req_ca_list )
6671{
6672 conf->cert_req_ca_list = cert_req_ca_list;
6673}
6674#endif
6675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006677void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006678{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006679 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006680}
6681#endif
6682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006683#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006684void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006685{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006686 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006687}
6688#endif
6689
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006690#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006691void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006692{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006693 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006694}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006695#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006698int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006699{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006700 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
6701 mfl_code_to_length[mfl_code] > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006704 }
6705
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006706 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006707
6708 return( 0 );
6709}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006713void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006714{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006715 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006716}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006720void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006721{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006722 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006723}
6724#endif
6725
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006726void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006727{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006728 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006729}
6730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006731#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006732void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006733{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006734 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006735}
6736
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006737void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006738{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006739 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006740}
6741
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006742void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006743 const unsigned char period[8] )
6744{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006745 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006746}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006750#if defined(MBEDTLS_SSL_CLI_C)
6751void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006752{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006753 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006754}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006755#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006756
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006757#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006758void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6759 mbedtls_ssl_ticket_write_t *f_ticket_write,
6760 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6761 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006762{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006763 conf->f_ticket_write = f_ticket_write;
6764 conf->f_ticket_parse = f_ticket_parse;
6765 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006766}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006767#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006769
Robert Cragie4feb7ae2015-10-02 13:33:37 +01006770#if defined(MBEDTLS_SSL_EXPORT_KEYS)
6771void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
6772 mbedtls_ssl_export_keys_t *f_export_keys,
6773 void *p_export_keys )
6774{
6775 conf->f_export_keys = f_export_keys;
6776 conf->p_export_keys = p_export_keys;
6777}
6778#endif
6779
Paul Bakker5121ce52009-01-03 21:22:43 +00006780/*
6781 * SSL get accessors
6782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006784{
6785 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6786}
6787
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006788uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006789{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006790 if( ssl->session != NULL )
6791 return( ssl->session->verify_result );
6792
6793 if( ssl->session_negotiate != NULL )
6794 return( ssl->session_negotiate->verify_result );
6795
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006796 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006797}
6798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006800{
Paul Bakker926c8e42013-03-06 10:23:34 +01006801 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006802 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006805}
6806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006808{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006810 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006811 {
6812 switch( ssl->minor_ver )
6813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006815 return( "DTLSv1.0" );
6816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006818 return( "DTLSv1.2" );
6819
6820 default:
6821 return( "unknown (DTLS)" );
6822 }
6823 }
6824#endif
6825
Paul Bakker43ca69c2011-01-15 17:35:19 +00006826 switch( ssl->minor_ver )
6827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006829 return( "SSLv3.0" );
6830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006832 return( "TLSv1.0" );
6833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006835 return( "TLSv1.1" );
6836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00006838 return( "TLSv1.2" );
6839
Paul Bakker43ca69c2011-01-15 17:35:19 +00006840 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006841 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00006842 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00006843}
6844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006846{
Hanno Becker12f7ede2018-08-17 15:28:19 +01006847 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006849 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006850
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006851 if( transform == NULL )
6852 return( (int) mbedtls_ssl_hdr_len( ssl ) );
6853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854#if defined(MBEDTLS_ZLIB_SUPPORT)
6855 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
6856 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006857#endif
6858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861 case MBEDTLS_MODE_GCM:
6862 case MBEDTLS_MODE_CCM:
6863 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006864 transform_expansion = transform->minlen;
6865 break;
6866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006867 case MBEDTLS_MODE_CBC:
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006868
6869 block_size = mbedtls_cipher_get_block_size(
6870 &transform->cipher_ctx_enc );
6871
Hanno Becker12f7ede2018-08-17 15:28:19 +01006872 /* Expansion due to the addition of the MAC. */
6873 transform_expansion += transform->maclen;
6874
6875 /* Expansion due to the addition of CBC padding;
6876 * Theoretically up to 256 bytes, but we never use
6877 * more than the block size of the underlying cipher. */
6878 transform_expansion += block_size;
6879
6880 /* For TLS 1.1 or higher, an explicit IV is added
6881 * after the record header. */
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006882#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
6883 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker12f7ede2018-08-17 15:28:19 +01006884 transform_expansion += block_size;
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006885#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker12f7ede2018-08-17 15:28:19 +01006886
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006887 break;
6888
6889 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006892 }
6893
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006894 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006895}
6896
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02006897#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
6898size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
6899{
6900 size_t max_len;
6901
6902 /*
6903 * Assume mfl_code is correct since it was checked when set
6904 */
6905 max_len = mfl_code_to_length[ssl->conf->mfl_code];
6906
6907 /*
6908 * Check if a smaller max length was negotiated
6909 */
6910 if( ssl->session_out != NULL &&
6911 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6912 {
6913 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6914 }
6915
6916 return max_len;
6917}
6918#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
6919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920#if defined(MBEDTLS_X509_CRT_PARSE_C)
6921const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00006922{
6923 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006924 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006925
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006926 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006927}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00006929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930#if defined(MBEDTLS_SSL_CLI_C)
6931int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006932{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006933 if( ssl == NULL ||
6934 dst == NULL ||
6935 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006936 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006939 }
6940
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006941 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006942}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006944
Paul Bakker5121ce52009-01-03 21:22:43 +00006945/*
Paul Bakker1961b702013-01-25 14:49:24 +01006946 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00006947 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006949{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006951
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006952 if( ssl == NULL || ssl->conf == NULL )
6953 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006956 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006957 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006958#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006960 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006962#endif
6963
Paul Bakker1961b702013-01-25 14:49:24 +01006964 return( ret );
6965}
6966
6967/*
6968 * Perform the SSL handshake
6969 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01006971{
6972 int ret = 0;
6973
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006974 if( ssl == NULL || ssl->conf == NULL )
6975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01006978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01006980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01006982
6983 if( ret != 0 )
6984 break;
6985 }
6986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006988
6989 return( ret );
6990}
6991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992#if defined(MBEDTLS_SSL_RENEGOTIATION)
6993#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006994/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006995 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00006996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006998{
6999 int ret;
7000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007002
7003 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7005 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007010 return( ret );
7011 }
7012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007014
7015 return( 0 );
7016}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007018
7019/*
7020 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007021 * - any side: calling mbedtls_ssl_renegotiate(),
7022 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7023 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007024 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007025 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007029{
7030 int ret;
7031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007033
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007034 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7035 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007036
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007037 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7038 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007039#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007040 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007042 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007043 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007044 ssl->handshake->out_msg_seq = 1;
7045 else
7046 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007047 }
7048#endif
7049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7051 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007056 return( ret );
7057 }
7058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007060
7061 return( 0 );
7062}
7063
7064/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007065 * Renegotiate current connection on client,
7066 * or request renegotiation on server
7067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007069{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007070 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007071
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007072 if( ssl == NULL || ssl->conf == NULL )
7073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007076 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007077 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007083
7084 /* Did we already try/start sending HelloRequest? */
7085 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007087
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007088 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007089 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007093 /*
7094 * On client, either start the renegotiation process or,
7095 * if already in progress, continue the handshake
7096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007101
7102 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007105 return( ret );
7106 }
7107 }
7108 else
7109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007113 return( ret );
7114 }
7115 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007117
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007118 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007119}
7120
7121/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007122 * Check record counters and renegotiate if they're above the limit.
7123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007125{
Andres AG2196c7f2016-12-15 17:01:16 +00007126 size_t ep_len = ssl_ep_len( ssl );
7127 int in_ctr_cmp;
7128 int out_ctr_cmp;
7129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7131 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007132 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007133 {
7134 return( 0 );
7135 }
7136
Andres AG2196c7f2016-12-15 17:01:16 +00007137 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7138 ssl->conf->renego_period + ep_len, 8 - ep_len );
7139 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
7140 ssl->conf->renego_period + ep_len, 8 - ep_len );
7141
7142 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007143 {
7144 return( 0 );
7145 }
7146
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007151
7152/*
7153 * Receive application data decrypted from the SSL layer
7154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007156{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007157 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007158 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007159
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007160 if( ssl == NULL || ssl->conf == NULL )
7161 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007166 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007169 return( ret );
7170
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007171 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007175 return( ret );
7176 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007177 }
7178#endif
7179
Hanno Becker4a810fb2017-05-24 16:27:30 +01007180 /*
7181 * Check if renegotiation is necessary and/or handshake is
7182 * in process. If yes, perform/continue, and fall through
7183 * if an unexpected packet is received while the client
7184 * is waiting for the ServerHello.
7185 *
7186 * (There is no equivalent to the last condition on
7187 * the server-side as it is not treated as within
7188 * a handshake while waiting for the ClientHello
7189 * after a renegotiation request.)
7190 */
7191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007193 ret = ssl_check_ctr_renegotiate( ssl );
7194 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7195 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007198 return( ret );
7199 }
7200#endif
7201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007205 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7206 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007209 return( ret );
7210 }
7211 }
7212
7213 if( ssl->in_offt == NULL )
7214 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007215 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007216 if( ssl->f_get_timer != NULL &&
7217 ssl->f_get_timer( ssl->p_timer ) == -1 )
7218 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007219 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007220 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007221
Hanno Becker4a810fb2017-05-24 16:27:30 +01007222 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007223 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007224 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7225 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007226
Hanno Becker4a810fb2017-05-24 16:27:30 +01007227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7228 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007229 }
7230
7231 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007233 {
7234 /*
7235 * OpenSSL sends empty messages to randomize the IV
7236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007237 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007239 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007240 return( 0 );
7241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007242 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007243 return( ret );
7244 }
7245 }
7246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007250
Hanno Becker4a810fb2017-05-24 16:27:30 +01007251 /*
7252 * - For client-side, expect SERVER_HELLO_REQUEST.
7253 * - For server-side, expect CLIENT_HELLO.
7254 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7255 */
7256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007258 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007260 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007263
7264 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007266 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007267 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007268#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007270 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007271#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007272
Hanno Becker4a810fb2017-05-24 16:27:30 +01007273#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007274 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007278
7279 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007281 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007282 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007283#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007285 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007286#endif /* MBEDTLS_SSL_SRV_C */
7287
Hanno Becker21df7f92017-10-17 11:03:26 +01007288#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007289 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007290 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7291 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7292 ssl->conf->allow_legacy_renegotiation ==
7293 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7294 {
7295 /*
7296 * Accept renegotiation request
7297 */
Paul Bakker48916f92012-09-16 19:57:18 +00007298
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007299 /* DTLS clients need to know renego is server-initiated */
7300#if defined(MBEDTLS_SSL_PROTO_DTLS)
7301 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7302 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7303 {
7304 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7305 }
7306#endif
7307 ret = ssl_start_renegotiation( ssl );
7308 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7309 ret != 0 )
7310 {
7311 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7312 return( ret );
7313 }
7314 }
7315 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007316#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007317 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007318 /*
7319 * Refuse renegotiation
7320 */
7321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324#if defined(MBEDTLS_SSL_PROTO_SSL3)
7325 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007326 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007327 /* SSLv3 does not have a "no_renegotiation" warning, so
7328 we send a fatal alert and abort the connection. */
7329 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7330 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7331 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007332 }
7333 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7335#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7336 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7337 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7340 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7341 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007342 {
7343 return( ret );
7344 }
Paul Bakker48916f92012-09-16 19:57:18 +00007345 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007346 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7348 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7351 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007352 }
Paul Bakker48916f92012-09-16 19:57:18 +00007353 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007354
Hanno Becker4a810fb2017-05-24 16:27:30 +01007355 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00007356 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007357#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007359 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007360 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007361 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007362 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007365 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007367 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007368 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007369 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7373 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007376 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007377 }
7378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7382 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007383 }
7384
7385 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007386
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007387 /* We're going to return something now, cancel timer,
7388 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007390 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007391
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007392#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007393 /* If we requested renego but received AppData, resend HelloRequest.
7394 * Do it now, after setting in_offt, to avoid taking this branch
7395 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007396#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007397 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007399 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007400 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007403 return( ret );
7404 }
7405 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007407#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007408 }
7409
7410 n = ( len < ssl->in_msglen )
7411 ? len : ssl->in_msglen;
7412
7413 memcpy( buf, ssl->in_offt, n );
7414 ssl->in_msglen -= n;
7415
gabor-mezei-armef738752020-07-15 10:55:00 +02007416 /* Zeroising the plaintext buffer to erase unused application data
7417 from the memory. */
7418 mbedtls_zeroize( ssl->in_offt, n );
7419
Paul Bakker5121ce52009-01-03 21:22:43 +00007420 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007421 {
7422 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007423 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007424 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007426 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007427 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007428 /* more data available */
7429 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007430 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007433
Paul Bakker23986e52011-04-24 08:57:21 +00007434 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007435}
7436
7437/*
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007438 * Send application data to be encrypted by the SSL layer, taking care of max
7439 * fragment length and buffer size.
7440 *
7441 * According to RFC 5246 Section 6.2.1:
7442 *
7443 * Zero-length fragments of Application data MAY be sent as they are
7444 * potentially useful as a traffic analysis countermeasure.
7445 *
7446 * Therefore, it is possible that the input message length is 0 and the
7447 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007448 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007449static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007450 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007451{
Paul Bakker23986e52011-04-24 08:57:21 +00007452 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007453#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007454 size_t max_len = mbedtls_ssl_get_max_frag_len( ssl );
Florin0b7b83f2017-07-22 09:01:44 +02007455#else
7456 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
7457#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007458 if( len > max_len )
7459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007460#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007461 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007464 "maximum fragment length: %d > %d",
7465 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007467 }
7468 else
7469#endif
7470 len = max_len;
7471 }
Paul Bakker887bd502011-06-08 13:10:54 +00007472
Paul Bakker5121ce52009-01-03 21:22:43 +00007473 if( ssl->out_left != 0 )
7474 {
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007475 /*
7476 * The user has previously tried to send the data and
7477 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7478 * written. In this case, we expect the high-level write function
7479 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007484 return( ret );
7485 }
7486 }
Paul Bakker887bd502011-06-08 13:10:54 +00007487 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007488 {
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007489 /*
7490 * The user is trying to send a message the first time, so we need to
7491 * copy the data into the internal buffers and setup the data structure
7492 * to keep track of partial writes
7493 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007494 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007496 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007501 return( ret );
7502 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007503 }
7504
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007505 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007506}
7507
7508/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007509 * Write application data, doing 1/n-1 splitting if necessary.
7510 *
7511 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007512 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01007513 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007516static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007517 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007518{
7519 int ret;
7520
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007521 if( ssl->conf->cbc_record_splitting ==
7522 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007523 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7525 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7526 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007527 {
7528 return( ssl_write_real( ssl, buf, len ) );
7529 }
7530
7531 if( ssl->split_done == 0 )
7532 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007533 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007534 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007535 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007536 }
7537
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007538 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7539 return( ret );
7540 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007541
7542 return( ret + 1 );
7543}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007545
7546/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007547 * Write application data (public-facing wrapper)
7548 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007549int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007550{
7551 int ret;
7552
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007554
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007555 if( ssl == NULL || ssl->conf == NULL )
7556 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7557
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007558#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007559 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7560 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007561 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007562 return( ret );
7563 }
7564#endif
7565
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007566 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007567 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007568 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007569 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007571 return( ret );
7572 }
7573 }
7574
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007575#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007576 ret = ssl_write_split( ssl, buf, len );
7577#else
7578 ret = ssl_write_real( ssl, buf, len );
7579#endif
7580
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007582
7583 return( ret );
7584}
7585
7586/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007587 * Notify the peer that the connection is being closed
7588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007590{
7591 int ret;
7592
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007593 if( ssl == NULL || ssl->conf == NULL )
7594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007597
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007598 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7604 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7605 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007608 return( ret );
7609 }
7610 }
7611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007613
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007614 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007615}
7616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00007618{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007619 if( transform == NULL )
7620 return;
7621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00007623 deflateEnd( &transform->ctx_deflate );
7624 inflateEnd( &transform->ctx_inflate );
7625#endif
7626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007627 mbedtls_cipher_free( &transform->cipher_ctx_enc );
7628 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02007629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630 mbedtls_md_free( &transform->md_ctx_enc );
7631 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02007632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007633 mbedtls_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007634}
7635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636#if defined(MBEDTLS_X509_CRT_PARSE_C)
7637static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007638{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007640
7641 while( cur != NULL )
7642 {
7643 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007645 cur = next;
7646 }
7647}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake )
Paul Bakker48916f92012-09-16 19:57:18 +00007651{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007652 if( handshake == NULL )
7653 return;
7654
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007655#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7656 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7657 mbedtls_md5_free( &handshake->fin_md5 );
7658 mbedtls_sha1_free( &handshake->fin_sha1 );
7659#endif
7660#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7661#if defined(MBEDTLS_SHA256_C)
7662 mbedtls_sha256_free( &handshake->fin_sha256 );
7663#endif
7664#if defined(MBEDTLS_SHA512_C)
7665 mbedtls_sha512_free( &handshake->fin_sha512 );
7666#endif
7667#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007669#if defined(MBEDTLS_DHM_C)
7670 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007671#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672#if defined(MBEDTLS_ECDH_C)
7673 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007674#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007675#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007676 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007677#if defined(MBEDTLS_SSL_CLI_C)
7678 mbedtls_free( handshake->ecjpake_cache );
7679 handshake->ecjpake_cache = NULL;
7680 handshake->ecjpake_cache_len = 0;
7681#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007682#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02007683
Janos Follath4ae5c292016-02-10 11:27:43 +00007684#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
7685 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02007686 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007688#endif
7689
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007690#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7691 if( handshake->psk != NULL )
7692 {
7693 mbedtls_zeroize( handshake->psk, handshake->psk_len );
7694 mbedtls_free( handshake->psk );
7695 }
7696#endif
7697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7699 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007700 /*
7701 * Free only the linked list wrapper, not the keys themselves
7702 * since the belong to the SNI callback
7703 */
7704 if( handshake->sni_key_cert != NULL )
7705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007707
7708 while( cur != NULL )
7709 {
7710 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007712 cur = next;
7713 }
7714 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007715#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717#if defined(MBEDTLS_SSL_PROTO_DTLS)
7718 mbedtls_free( handshake->verify_cookie );
7719 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007720 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007721#endif
7722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723 mbedtls_zeroize( handshake, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007724}
7725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007727{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007728 if( session == NULL )
7729 return;
7730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007732 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734 mbedtls_x509_crt_free( session->peer_cert );
7735 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007736 }
Paul Bakkered27a042013-04-18 22:46:23 +02007737#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007738
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007739#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007741#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743 mbedtls_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007744}
7745
Paul Bakker5121ce52009-01-03 21:22:43 +00007746/*
7747 * Free an SSL context
7748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007749void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007750{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007751 if( ssl == NULL )
7752 return;
7753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007755
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007756 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 mbedtls_zeroize( ssl->out_buf, MBEDTLS_SSL_BUFFER_LEN );
7759 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007760 }
7761
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007762 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764 mbedtls_zeroize( ssl->in_buf, MBEDTLS_SSL_BUFFER_LEN );
7765 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007766 }
7767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02007769 if( ssl->compress_buf != NULL )
7770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771 mbedtls_zeroize( ssl->compress_buf, MBEDTLS_SSL_BUFFER_LEN );
7772 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02007773 }
7774#endif
7775
Paul Bakker48916f92012-09-16 19:57:18 +00007776 if( ssl->transform )
7777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778 mbedtls_ssl_transform_free( ssl->transform );
7779 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007780 }
7781
7782 if( ssl->handshake )
7783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784 mbedtls_ssl_handshake_free( ssl->handshake );
7785 mbedtls_ssl_transform_free( ssl->transform_negotiate );
7786 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788 mbedtls_free( ssl->handshake );
7789 mbedtls_free( ssl->transform_negotiate );
7790 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007791 }
7792
Paul Bakkerc0463502013-02-14 11:19:38 +01007793 if( ssl->session )
7794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795 mbedtls_ssl_session_free( ssl->session );
7796 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007797 }
7798
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02007799#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02007800 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007801 {
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007802 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00007804 }
Paul Bakker0be444a2013-08-27 21:55:01 +02007805#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7808 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
7811 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00007812 }
7813#endif
7814
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007815#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007817#endif
7818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00007820
Paul Bakker86f04f42013-02-14 11:20:09 +01007821 /* Actually clear after last debug message */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007822 mbedtls_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007823}
7824
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007825/*
7826 * Initialze mbedtls_ssl_config
7827 */
7828void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
7829{
7830 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
7831}
7832
Simon Butcherc97b6972015-12-27 23:48:17 +00007833#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007834static int ssl_preset_default_hashes[] = {
7835#if defined(MBEDTLS_SHA512_C)
7836 MBEDTLS_MD_SHA512,
7837 MBEDTLS_MD_SHA384,
7838#endif
7839#if defined(MBEDTLS_SHA256_C)
7840 MBEDTLS_MD_SHA256,
7841 MBEDTLS_MD_SHA224,
7842#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02007843#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007844 MBEDTLS_MD_SHA1,
7845#endif
7846 MBEDTLS_MD_NONE
7847};
Simon Butcherc97b6972015-12-27 23:48:17 +00007848#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007849
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007850static int ssl_preset_suiteb_ciphersuites[] = {
7851 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
7852 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
7853 0
7854};
7855
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007856#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007857static int ssl_preset_suiteb_hashes[] = {
7858 MBEDTLS_MD_SHA256,
7859 MBEDTLS_MD_SHA384,
7860 MBEDTLS_MD_NONE
7861};
7862#endif
7863
7864#if defined(MBEDTLS_ECP_C)
7865static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007866#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007867 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007868#endif
7869#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007870 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007871#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007872 MBEDTLS_ECP_DP_NONE
7873};
7874#endif
7875
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007876/*
Tillmann Karras588ad502015-09-25 04:27:22 +02007877 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007878 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007879int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007880 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007881{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007882#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007883 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007884#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007885
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02007886 /* Use the functions here so that they are covered in tests,
7887 * but otherwise access member directly for efficiency */
7888 mbedtls_ssl_conf_endpoint( conf, endpoint );
7889 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007890
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007891 /*
7892 * Things that are common to all presets
7893 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007894#if defined(MBEDTLS_SSL_CLI_C)
7895 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7896 {
7897 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
7898#if defined(MBEDTLS_SSL_SESSION_TICKETS)
7899 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
7900#endif
7901 }
7902#endif
7903
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007904#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007905 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007906#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007907
7908#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7909 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
7910#endif
7911
7912#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7913 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
7914#endif
7915
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007916#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7917 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7918#endif
7919
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007920#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007921 conf->f_cookie_write = ssl_cookie_write_dummy;
7922 conf->f_cookie_check = ssl_cookie_check_dummy;
7923#endif
7924
7925#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7926 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7927#endif
7928
Janos Follath088ce432017-04-10 12:42:31 +01007929#if defined(MBEDTLS_SSL_SRV_C)
7930 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
7931#endif
7932
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007933#if defined(MBEDTLS_SSL_PROTO_DTLS)
7934 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7935 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7936#endif
7937
7938#if defined(MBEDTLS_SSL_RENEGOTIATION)
7939 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00007940 memset( conf->renego_period, 0x00, 2 );
7941 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007942#endif
7943
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007944#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
7945 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7946 {
Hanno Becker00d0a682017-10-04 13:14:29 +01007947 const unsigned char dhm_p[] =
7948 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
7949 const unsigned char dhm_g[] =
7950 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
7951
Hanno Beckera90658f2017-10-04 15:29:08 +01007952 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
7953 dhm_p, sizeof( dhm_p ),
7954 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007955 {
7956 return( ret );
7957 }
7958 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007959#endif
7960
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007961 /*
7962 * Preset-specific defaults
7963 */
7964 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007965 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007966 /*
7967 * NSA Suite B
7968 */
7969 case MBEDTLS_SSL_PRESET_SUITEB:
7970 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7971 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7972 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7973 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7974
7975 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7976 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7977 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7978 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7979 ssl_preset_suiteb_ciphersuites;
7980
7981#if defined(MBEDTLS_X509_CRT_PARSE_C)
7982 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007983#endif
7984
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007985#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007986 conf->sig_hashes = ssl_preset_suiteb_hashes;
7987#endif
7988
7989#if defined(MBEDTLS_ECP_C)
7990 conf->curve_list = ssl_preset_suiteb_curves;
7991#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007992 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007993
7994 /*
7995 * Default
7996 */
7997 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03007998 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
7999 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8000 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8001 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8002 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8003 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8004 MBEDTLS_SSL_MIN_MINOR_VERSION :
8005 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008006 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8007 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8008
8009#if defined(MBEDTLS_SSL_PROTO_DTLS)
8010 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8011 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8012#endif
8013
8014 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8015 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8016 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8017 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8018 mbedtls_ssl_list_ciphersuites();
8019
8020#if defined(MBEDTLS_X509_CRT_PARSE_C)
8021 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8022#endif
8023
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008024#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008025 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008026#endif
8027
8028#if defined(MBEDTLS_ECP_C)
8029 conf->curve_list = mbedtls_ecp_grp_id_list();
8030#endif
8031
8032#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8033 conf->dhm_min_bitlen = 1024;
8034#endif
8035 }
8036
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008037 return( 0 );
8038}
8039
8040/*
8041 * Free mbedtls_ssl_config
8042 */
8043void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8044{
8045#if defined(MBEDTLS_DHM_C)
8046 mbedtls_mpi_free( &conf->dhm_P );
8047 mbedtls_mpi_free( &conf->dhm_G );
8048#endif
8049
8050#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8051 if( conf->psk != NULL )
8052 {
8053 mbedtls_zeroize( conf->psk, conf->psk_len );
8054 mbedtls_zeroize( conf->psk_identity, conf->psk_identity_len );
8055 mbedtls_free( conf->psk );
8056 mbedtls_free( conf->psk_identity );
8057 conf->psk_len = 0;
8058 conf->psk_identity_len = 0;
8059 }
8060#endif
8061
8062#if defined(MBEDTLS_X509_CRT_PARSE_C)
8063 ssl_key_cert_free( conf->key_cert );
8064#endif
8065
8066 mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
8067}
8068
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008069#if defined(MBEDTLS_PK_C) && \
8070 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008071/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008072 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076#if defined(MBEDTLS_RSA_C)
8077 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8078 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008079#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080#if defined(MBEDTLS_ECDSA_C)
8081 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8082 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008083#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008084 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008085}
8086
Hanno Becker7e5437a2017-04-28 17:15:26 +01008087unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8088{
8089 switch( type ) {
8090 case MBEDTLS_PK_RSA:
8091 return( MBEDTLS_SSL_SIG_RSA );
8092 case MBEDTLS_PK_ECDSA:
8093 case MBEDTLS_PK_ECKEY:
8094 return( MBEDTLS_SSL_SIG_ECDSA );
8095 default:
8096 return( MBEDTLS_SSL_SIG_ANON );
8097 }
8098}
8099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008101{
8102 switch( sig )
8103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008104#if defined(MBEDTLS_RSA_C)
8105 case MBEDTLS_SSL_SIG_RSA:
8106 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008107#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108#if defined(MBEDTLS_ECDSA_C)
8109 case MBEDTLS_SSL_SIG_ECDSA:
8110 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008111#endif
8112 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008113 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008114 }
8115}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008116#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008117
Hanno Becker7e5437a2017-04-28 17:15:26 +01008118#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8119 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8120
8121/* Find an entry in a signature-hash set matching a given hash algorithm. */
8122mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8123 mbedtls_pk_type_t sig_alg )
8124{
8125 switch( sig_alg )
8126 {
8127 case MBEDTLS_PK_RSA:
8128 return( set->rsa );
8129 case MBEDTLS_PK_ECDSA:
8130 return( set->ecdsa );
8131 default:
8132 return( MBEDTLS_MD_NONE );
8133 }
8134}
8135
8136/* Add a signature-hash-pair to a signature-hash set */
8137void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8138 mbedtls_pk_type_t sig_alg,
8139 mbedtls_md_type_t md_alg )
8140{
8141 switch( sig_alg )
8142 {
8143 case MBEDTLS_PK_RSA:
8144 if( set->rsa == MBEDTLS_MD_NONE )
8145 set->rsa = md_alg;
8146 break;
8147
8148 case MBEDTLS_PK_ECDSA:
8149 if( set->ecdsa == MBEDTLS_MD_NONE )
8150 set->ecdsa = md_alg;
8151 break;
8152
8153 default:
8154 break;
8155 }
8156}
8157
8158/* Allow exactly one hash algorithm for each signature. */
8159void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8160 mbedtls_md_type_t md_alg )
8161{
8162 set->rsa = md_alg;
8163 set->ecdsa = md_alg;
8164}
8165
8166#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8167 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8168
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008169/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008170 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008173{
8174 switch( hash )
8175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176#if defined(MBEDTLS_MD5_C)
8177 case MBEDTLS_SSL_HASH_MD5:
8178 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008179#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180#if defined(MBEDTLS_SHA1_C)
8181 case MBEDTLS_SSL_HASH_SHA1:
8182 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008183#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184#if defined(MBEDTLS_SHA256_C)
8185 case MBEDTLS_SSL_HASH_SHA224:
8186 return( MBEDTLS_MD_SHA224 );
8187 case MBEDTLS_SSL_HASH_SHA256:
8188 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008189#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190#if defined(MBEDTLS_SHA512_C)
8191 case MBEDTLS_SSL_HASH_SHA384:
8192 return( MBEDTLS_MD_SHA384 );
8193 case MBEDTLS_SSL_HASH_SHA512:
8194 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008195#endif
8196 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008198 }
8199}
8200
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008201/*
8202 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8203 */
8204unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8205{
8206 switch( md )
8207 {
8208#if defined(MBEDTLS_MD5_C)
8209 case MBEDTLS_MD_MD5:
8210 return( MBEDTLS_SSL_HASH_MD5 );
8211#endif
8212#if defined(MBEDTLS_SHA1_C)
8213 case MBEDTLS_MD_SHA1:
8214 return( MBEDTLS_SSL_HASH_SHA1 );
8215#endif
8216#if defined(MBEDTLS_SHA256_C)
8217 case MBEDTLS_MD_SHA224:
8218 return( MBEDTLS_SSL_HASH_SHA224 );
8219 case MBEDTLS_MD_SHA256:
8220 return( MBEDTLS_SSL_HASH_SHA256 );
8221#endif
8222#if defined(MBEDTLS_SHA512_C)
8223 case MBEDTLS_MD_SHA384:
8224 return( MBEDTLS_SSL_HASH_SHA384 );
8225 case MBEDTLS_MD_SHA512:
8226 return( MBEDTLS_SSL_HASH_SHA512 );
8227#endif
8228 default:
8229 return( MBEDTLS_SSL_HASH_NONE );
8230 }
8231}
8232
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008233#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008234/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008235 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008236 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008237 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008238int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008240 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008241
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008242 if( ssl->conf->curve_list == NULL )
8243 return( -1 );
8244
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008245 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008246 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008247 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008248
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008249 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008250}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008251#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008252
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008253#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008254/*
8255 * Check if a hash proposed by the peer is in our list.
8256 * Return 0 if we're willing to use it, -1 otherwise.
8257 */
8258int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8259 mbedtls_md_type_t md )
8260{
8261 const int *cur;
8262
8263 if( ssl->conf->sig_hashes == NULL )
8264 return( -1 );
8265
8266 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8267 if( *cur == (int) md )
8268 return( 0 );
8269
8270 return( -1 );
8271}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008272#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008274#if defined(MBEDTLS_X509_CRT_PARSE_C)
8275int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8276 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008277 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008278 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008279{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008280 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008281#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008282 int usage = 0;
8283#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008285 const char *ext_oid;
8286 size_t ext_len;
8287#endif
8288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008289#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8290 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008291 ((void) cert);
8292 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008293 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008294#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8297 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008298 {
8299 /* Server part of the key exchange */
8300 switch( ciphersuite->key_exchange )
8301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302 case MBEDTLS_KEY_EXCHANGE_RSA:
8303 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008304 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008305 break;
8306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8308 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8309 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8310 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008311 break;
8312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8314 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008315 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008316 break;
8317
8318 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008319 case MBEDTLS_KEY_EXCHANGE_NONE:
8320 case MBEDTLS_KEY_EXCHANGE_PSK:
8321 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8322 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008323 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008324 usage = 0;
8325 }
8326 }
8327 else
8328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008329 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8330 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008331 }
8332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008334 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008335 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008336 ret = -1;
8337 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008338#else
8339 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008340#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8343 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8346 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008347 }
8348 else
8349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8351 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008352 }
8353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008355 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008356 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008357 ret = -1;
8358 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008360
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008361 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008362}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008363#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008364
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008365/*
8366 * Convert version numbers to/from wire format
8367 * and, for DTLS, to/from TLS equivalent.
8368 *
8369 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008370 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008371 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8372 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008375 unsigned char ver[2] )
8376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008377#if defined(MBEDTLS_SSL_PROTO_DTLS)
8378 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008380 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008381 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8382
8383 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8384 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8385 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008386 else
8387#else
8388 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008389#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008390 {
8391 ver[0] = (unsigned char) major;
8392 ver[1] = (unsigned char) minor;
8393 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008394}
8395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008397 const unsigned char ver[2] )
8398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399#if defined(MBEDTLS_SSL_PROTO_DTLS)
8400 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008401 {
8402 *major = 255 - ver[0] + 2;
8403 *minor = 255 - ver[1] + 1;
8404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008406 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8407 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008408 else
8409#else
8410 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008411#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008412 {
8413 *major = ver[0];
8414 *minor = ver[1];
8415 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008416}
8417
Simon Butcher99000142016-10-13 17:21:01 +01008418int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8419{
8420#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8421 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8422 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8423
8424 switch( md )
8425 {
8426#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8427#if defined(MBEDTLS_MD5_C)
8428 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008429 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008430#endif
8431#if defined(MBEDTLS_SHA1_C)
8432 case MBEDTLS_SSL_HASH_SHA1:
8433 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8434 break;
8435#endif
8436#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8437#if defined(MBEDTLS_SHA512_C)
8438 case MBEDTLS_SSL_HASH_SHA384:
8439 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8440 break;
8441#endif
8442#if defined(MBEDTLS_SHA256_C)
8443 case MBEDTLS_SSL_HASH_SHA256:
8444 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8445 break;
8446#endif
8447 default:
8448 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8449 }
8450
8451 return 0;
8452#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8453 (void) ssl;
8454 (void) md;
8455
8456 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8457#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8458}
8459
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008460#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8461 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8462int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8463 unsigned char *output,
8464 unsigned char *data, size_t data_len )
8465{
8466 int ret = 0;
8467 mbedtls_md5_context mbedtls_md5;
8468 mbedtls_sha1_context mbedtls_sha1;
8469
8470 mbedtls_md5_init( &mbedtls_md5 );
8471 mbedtls_sha1_init( &mbedtls_sha1 );
8472
8473 /*
8474 * digitally-signed struct {
8475 * opaque md5_hash[16];
8476 * opaque sha_hash[20];
8477 * };
8478 *
8479 * md5_hash
8480 * MD5(ClientHello.random + ServerHello.random
8481 * + ServerParams);
8482 * sha_hash
8483 * SHA(ClientHello.random + ServerHello.random
8484 * + ServerParams);
8485 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008486 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008487 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008488 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008489 goto exit;
8490 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008491 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008492 ssl->handshake->randbytes, 64 ) ) != 0 )
8493 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008494 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008495 goto exit;
8496 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008497 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008498 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008500 goto exit;
8501 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008502 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008503 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008505 goto exit;
8506 }
8507
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008508 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008509 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008510 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008511 goto exit;
8512 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008513 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008514 ssl->handshake->randbytes, 64 ) ) != 0 )
8515 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008516 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008517 goto exit;
8518 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008519 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008520 data_len ) ) != 0 )
8521 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008522 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008523 goto exit;
8524 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008525 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008526 output + 16 ) ) != 0 )
8527 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008528 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008529 goto exit;
8530 }
8531
8532exit:
8533 mbedtls_md5_free( &mbedtls_md5 );
8534 mbedtls_sha1_free( &mbedtls_sha1 );
8535
8536 if( ret != 0 )
8537 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8538 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8539
8540 return( ret );
8541
8542}
8543#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
8544 MBEDTLS_SSL_PROTO_TLS1_1 */
8545
8546#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8547 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8548int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8549 unsigned char *output,
8550 unsigned char *data, size_t data_len,
8551 mbedtls_md_type_t md_alg )
8552{
8553 int ret = 0;
8554 mbedtls_md_context_t ctx;
8555 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8556
8557 mbedtls_md_init( &ctx );
8558
8559 /*
8560 * digitally-signed struct {
8561 * opaque client_random[32];
8562 * opaque server_random[32];
8563 * ServerDHParams params;
8564 * };
8565 */
8566 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8567 {
8568 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8569 goto exit;
8570 }
8571 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8572 {
8573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8574 goto exit;
8575 }
8576 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8577 {
8578 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8579 goto exit;
8580 }
8581 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8582 {
8583 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8584 goto exit;
8585 }
8586 if( ( ret = mbedtls_md_finish( &ctx, output ) ) != 0 )
8587 {
8588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8589 goto exit;
8590 }
8591
8592exit:
8593 mbedtls_md_free( &ctx );
8594
8595 if( ret != 0 )
8596 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8597 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8598
8599 return( ret );
8600}
8601#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
8602 MBEDTLS_SSL_PROTO_TLS1_2 */
8603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008604#endif /* MBEDTLS_SSL_TLS_C */