blob: a3212cceddcff101c128e6959294d332470aac47 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46/*
47 * The SSL 3.0 specification was drafted by Netscape in 1996,
48 * and became an IETF standard in 1999.
49 *
50 * http://wp.netscape.com/eng/ssl3/
51 * http://www.ietf.org/rfc/rfc2246.txt
52 * http://www.ietf.org/rfc/rfc4346.txt
53 */
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020059#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000062
SimonBd5800b72016-04-26 07:43:27 +010063#if defined(MBEDTLS_PLATFORM_C)
64#include "mbedtls/platform.h"
65#else
66#include <stdlib.h>
67#define mbedtls_calloc calloc
68#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010069#endif
70
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/debug.h"
72#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020073#include "mbedtls/ssl_internal.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020074
Rich Evans00ab4702015-02-06 13:43:58 +000075#include <string.h>
76
Janos Follath23bdca02016-10-07 14:47:14 +010077#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000078#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020079#endif
80
Paul Bakker34617722014-06-13 17:20:13 +020081/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020083 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
84}
85
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010086/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020090 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010091 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010092#else
93 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010094#endif
95 return( 0 );
96}
97
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098/*
99 * Start a timer.
100 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200103{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200104 if( ssl->f_set_timer == NULL )
105 return;
106
107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
108 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200109}
110
111/*
112 * Return -1 is timer is expired, 0 if it isn't.
113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200115{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200116 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200117 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200118
119 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200120 {
121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200122 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200123 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200124
125 return( 0 );
126}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200127
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200128#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200129/*
130 * Double the retransmit timeout value, within the allowed range,
131 * returning -1 if the maximum value has already been reached.
132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200134{
135 uint32_t new_timeout;
136
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200137 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200138 return( -1 );
139
140 new_timeout = 2 * ssl->handshake->retransmit_timeout;
141
142 /* Avoid arithmetic overflow and range overflow */
143 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200144 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200145 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200146 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200147 }
148
149 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200151 ssl->handshake->retransmit_timeout ) );
152
153 return( 0 );
154}
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200157{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200158 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200160 ssl->handshake->retransmit_timeout ) );
161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200165/*
166 * Convert max_fragment_length codes to length.
167 * RFC 6066 says:
168 * enum{
169 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
170 * } MaxFragmentLength;
171 * and we add 0 -> extension unused
172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173static unsigned int mfl_code_to_length[MBEDTLS_SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200174{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 MBEDTLS_SSL_MAX_CONTENT_LEN, /* MBEDTLS_SSL_MAX_FRAG_LEN_NONE */
176 512, /* MBEDTLS_SSL_MAX_FRAG_LEN_512 */
177 1024, /* MBEDTLS_SSL_MAX_FRAG_LEN_1024 */
178 2048, /* MBEDTLS_SSL_MAX_FRAG_LEN_2048 */
179 4096, /* MBEDTLS_SSL_MAX_FRAG_LEN_4096 */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200180};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200182
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200183#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200185{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_ssl_session_free( dst );
187 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200190 if( src->peer_cert != NULL )
191 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200192 int ret;
193
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200194 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200195 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200196 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200201 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200204 dst->peer_cert = NULL;
205 return( ret );
206 }
207 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200209
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200210#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200211 if( src->ticket != NULL )
212 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200213 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200214 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200215 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200216
217 memcpy( dst->ticket, src->ticket, src->ticket_len );
218 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200219#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200220
221 return( 0 );
222}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200223#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
226int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200227 const unsigned char *key_enc, const unsigned char *key_dec,
228 size_t keylen,
229 const unsigned char *iv_enc, const unsigned char *iv_dec,
230 size_t ivlen,
231 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200232 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
234int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
235int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
236int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
237int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
238#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000239
Paul Bakker5121ce52009-01-03 21:22:43 +0000240/*
241 * Key material generation
242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200244static int ssl3_prf( const unsigned char *secret, size_t slen,
245 const char *label,
246 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000247 unsigned char *dstbuf, size_t dlen )
248{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100249 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000250 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200251 mbedtls_md5_context md5;
252 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000253 unsigned char padding[16];
254 unsigned char sha1sum[20];
255 ((void)label);
256
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200257 mbedtls_md5_init( &md5 );
258 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200259
Paul Bakker5f70b252012-09-13 14:23:06 +0000260 /*
261 * SSLv3:
262 * block =
263 * MD5( secret + SHA1( 'A' + secret + random ) ) +
264 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
265 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
266 * ...
267 */
268 for( i = 0; i < dlen / 16; i++ )
269 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200270 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000271
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100272 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100273 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100274 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100275 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100276 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100277 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100278 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100279 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100280 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100281 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000282
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100283 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100284 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100285 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100286 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100287 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100288 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100289 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100290 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000291 }
292
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100293exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200294 mbedtls_md5_free( &md5 );
295 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_zeroize( padding, sizeof( padding ) );
298 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000299
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100300 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000301}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200305static int tls1_prf( const unsigned char *secret, size_t slen,
306 const char *label,
307 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000308 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000309{
Paul Bakker23986e52011-04-24 08:57:21 +0000310 size_t nb, hs;
311 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200312 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 unsigned char tmp[128];
314 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 const mbedtls_md_info_t *md_info;
316 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100317 int ret;
318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
321 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
324 hs = ( slen + 1 ) / 2;
325 S1 = secret;
326 S2 = secret + slen - hs;
327
328 nb = strlen( label );
329 memcpy( tmp + 20, label, nb );
330 memcpy( tmp + 20 + nb, random, rlen );
331 nb += rlen;
332
333 /*
334 * First compute P_md5(secret,label+random)[0..dlen]
335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100340 return( ret );
341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
343 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
344 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
346 for( i = 0; i < dlen; i += 16 )
347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_md_hmac_reset ( &md_ctx );
349 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
350 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_md_hmac_reset ( &md_ctx );
353 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
354 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
356 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
357
358 for( j = 0; j < k; j++ )
359 dstbuf[i + j] = h_i[j];
360 }
361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100363
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 /*
365 * XOR out with P_sha1(secret,label+random)[0..dlen]
366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
368 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100371 return( ret );
372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
374 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
375 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376
377 for( i = 0; i < dlen; i += 20 )
378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_md_hmac_reset ( &md_ctx );
380 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
381 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_md_hmac_reset ( &md_ctx );
384 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
385 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000386
387 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
388
389 for( j = 0; j < k; j++ )
390 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
391 }
392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_zeroize( tmp, sizeof( tmp ) );
396 mbedtls_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000397
398 return( 0 );
399}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
403static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100404 const unsigned char *secret, size_t slen,
405 const char *label,
406 const unsigned char *random, size_t rlen,
407 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000408{
409 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100410 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000411 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
413 const mbedtls_md_info_t *md_info;
414 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100415 int ret;
416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
420 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100423
424 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000426
427 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100428 memcpy( tmp + md_len, label, nb );
429 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000430 nb += rlen;
431
432 /*
433 * Compute P_<hash>(secret, label + random)[0..dlen]
434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100436 return( ret );
437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
439 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
440 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100441
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100442 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_md_hmac_reset ( &md_ctx );
445 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
446 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_md_hmac_reset ( &md_ctx );
449 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
450 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000451
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100452 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000453
454 for( j = 0; j < k; j++ )
455 dstbuf[i + j] = h_i[j];
456 }
457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_zeroize( tmp, sizeof( tmp ) );
461 mbedtls_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000462
463 return( 0 );
464}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100467static int tls_prf_sha256( const unsigned char *secret, size_t slen,
468 const char *label,
469 const unsigned char *random, size_t rlen,
470 unsigned char *dstbuf, size_t dlen )
471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100473 label, random, rlen, dstbuf, dlen ) );
474}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200478static int tls_prf_sha384( const unsigned char *secret, size_t slen,
479 const char *label,
480 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000481 unsigned char *dstbuf, size_t dlen )
482{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100484 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#endif /* MBEDTLS_SHA512_C */
487#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
492 defined(MBEDTLS_SSL_PROTO_TLS1_1)
493static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200494#endif
Paul Bakker380da532012-04-18 16:10:25 +0000495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496#if defined(MBEDTLS_SSL_PROTO_SSL3)
497static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
498static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200499#endif
500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
502static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
503static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200504#endif
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
507#if defined(MBEDTLS_SHA256_C)
508static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
509static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
510static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200511#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#if defined(MBEDTLS_SHA512_C)
514static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
515static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
516static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100517#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000521{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200522 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 unsigned char keyblk[256];
525 unsigned char *key1;
526 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100527 unsigned char *mac_enc;
528 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000529 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200530 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 const mbedtls_cipher_info_t *cipher_info;
532 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_ssl_session *session = ssl->session_negotiate;
535 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
536 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100541 if( cipher_info == NULL )
542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100544 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100546 }
547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100549 if( md_info == NULL )
550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100552 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100554 }
555
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000557 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559#if defined(MBEDTLS_SSL_PROTO_SSL3)
560 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000561 {
Paul Bakker48916f92012-09-16 19:57:18 +0000562 handshake->tls_prf = ssl3_prf;
563 handshake->calc_verify = ssl_calc_verify_ssl;
564 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000565 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200566 else
567#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
569 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000570 {
Paul Bakker48916f92012-09-16 19:57:18 +0000571 handshake->tls_prf = tls1_prf;
572 handshake->calc_verify = ssl_calc_verify_tls;
573 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000574 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200575 else
576#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
578#if defined(MBEDTLS_SHA512_C)
579 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
580 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000581 {
Paul Bakker48916f92012-09-16 19:57:18 +0000582 handshake->tls_prf = tls_prf_sha384;
583 handshake->calc_verify = ssl_calc_verify_tls_sha384;
584 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000585 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000586 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200587#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588#if defined(MBEDTLS_SHA256_C)
589 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000590 {
Paul Bakker48916f92012-09-16 19:57:18 +0000591 handshake->tls_prf = tls_prf_sha256;
592 handshake->calc_verify = ssl_calc_verify_tls_sha256;
593 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000594 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200595 else
596#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
600 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200601 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000602
603 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 * SSLv3:
605 * master =
606 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
607 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
608 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200609 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200610 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 * master = PRF( premaster, "master secret", randbytes )[0..47]
612 */
Paul Bakker0a597072012-09-25 21:55:46 +0000613 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000616 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
619 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200620 {
621 unsigned char session_hash[48];
622 size_t hash_len;
623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200625
626 ssl->handshake->calc_verify( ssl, session_hash );
627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
629 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200632 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200634 {
635 hash_len = 48;
636 }
637 else
638#endif
639 hash_len = 32;
640 }
641 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200643 hash_len = 36;
644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200646
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100647 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
648 "extended master secret",
649 session_hash, hash_len,
650 session->master, 48 );
651 if( ret != 0 )
652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100654 return( ret );
655 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200656
657 }
658 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200659#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100660 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
661 "master secret",
662 handshake->randbytes, 64,
663 session->master, 48 );
664 if( ret != 0 )
665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100667 return( ret );
668 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 }
672 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
675 /*
676 * Swap the client and server random values.
677 */
Paul Bakker48916f92012-09-16 19:57:18 +0000678 memcpy( tmp, handshake->randbytes, 64 );
679 memcpy( handshake->randbytes, tmp + 32, 32 );
680 memcpy( handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000682
683 /*
684 * SSLv3:
685 * key block =
686 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
687 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
688 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
689 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
690 * ...
691 *
692 * TLSv1:
693 * key block = PRF( master, "key expansion", randbytes )
694 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100695 ret = handshake->tls_prf( session->master, 48, "key expansion",
696 handshake->randbytes, 64, keyblk, 256 );
697 if( ret != 0 )
698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100700 return( ret );
701 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
704 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
705 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
706 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
707 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
711 /*
712 * Determine the appropriate key, IV and MAC length.
713 */
Paul Bakker68884e32013-01-07 18:20:04 +0100714
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200715 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
718 cipher_info->mode == MBEDTLS_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200720 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000721 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200722
Paul Bakker68884e32013-01-07 18:20:04 +0100723 transform->ivlen = 12;
724 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200725
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200726 /* Minimum length is expicit IV + tag */
727 transform->minlen = transform->ivlen - transform->fixed_ivlen
728 + ( transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100730 }
731 else
732 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200733 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
735 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200738 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100739 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200741 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000742 mac_key_len = mbedtls_md_get_size( md_info );
743 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200746 /*
747 * If HMAC is to be truncated, we shall keep the leftmost bytes,
748 * (rfc 6066 page 13 or rfc 2104 section 4),
749 * so we only need to adjust the length here.
750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000754
755#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
756 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000757 * HMAC implementation which also truncates the key
758 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000759 mac_key_len = transform->maclen;
760#endif
761 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200763
764 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100765 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000766
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200767 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200769 transform->minlen = transform->maclen;
770 else
Paul Bakker68884e32013-01-07 18:20:04 +0100771 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200772 /*
773 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100774 * 1. if EtM is in use: one block plus MAC
775 * otherwise: * first multiple of blocklen greater than maclen
776 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
779 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100780 {
781 transform->minlen = transform->maclen
782 + cipher_info->block_size;
783 }
784 else
785#endif
786 {
787 transform->minlen = transform->maclen
788 + cipher_info->block_size
789 - transform->maclen % cipher_info->block_size;
790 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
793 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
794 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200795 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100796 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200797#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
799 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
800 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200801 {
802 transform->minlen += transform->ivlen;
803 }
804 else
805#endif
806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
808 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200809 }
Paul Bakker68884e32013-01-07 18:20:04 +0100810 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000811 }
812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000814 transform->keylen, transform->minlen, transform->ivlen,
815 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000816
817 /*
818 * Finally setup the cipher contexts, IVs and MAC secrets.
819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200821 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000822 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000823 key1 = keyblk + mac_key_len * 2;
824 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000825
Paul Bakker68884e32013-01-07 18:20:04 +0100826 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000827 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000828
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000829 /*
830 * This is not used in TLS v1.1.
831 */
Paul Bakker48916f92012-09-16 19:57:18 +0000832 iv_copy_len = ( transform->fixed_ivlen ) ?
833 transform->fixed_ivlen : transform->ivlen;
834 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
835 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000836 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 }
838 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839#endif /* MBEDTLS_SSL_CLI_C */
840#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200841 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000842 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000843 key1 = keyblk + mac_key_len * 2 + transform->keylen;
844 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Hanno Becker81c7b182017-11-09 18:39:33 +0000846 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100847 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000848
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000849 /*
850 * This is not used in TLS v1.1.
851 */
Paul Bakker48916f92012-09-16 19:57:18 +0000852 iv_copy_len = ( transform->fixed_ivlen ) ?
853 transform->fixed_ivlen : transform->ivlen;
854 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
855 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000856 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100858 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
862 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100863 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#if defined(MBEDTLS_SSL_PROTO_SSL3)
866 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100867 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000868 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
871 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100872 }
873
Hanno Becker81c7b182017-11-09 18:39:33 +0000874 memcpy( transform->mac_enc, mac_enc, mac_key_len );
875 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100876 }
877 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#endif /* MBEDTLS_SSL_PROTO_SSL3 */
879#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
880 defined(MBEDTLS_SSL_PROTO_TLS1_2)
881 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100882 {
Gilles Peskine21701302018-03-19 19:06:08 +0100883 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
884 For AEAD-based ciphersuites, there is nothing to do here. */
885 if( mac_key_len != 0 )
886 {
887 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
888 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
889 }
Paul Bakker68884e32013-01-07 18:20:04 +0100890 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200891 else
892#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
895 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200896 }
Paul Bakker68884e32013-01-07 18:20:04 +0100897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
899 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100904 transform->iv_enc, transform->iv_dec,
905 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100906 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +0000907 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
910 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000911 }
912 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000914
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200915#if defined(MBEDTLS_SSL_EXPORT_KEYS)
916 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100917 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200918 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
919 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +0000920 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100921 iv_copy_len );
922 }
923#endif
924
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200925 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200926 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000927 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200929 return( ret );
930 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200931
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200932 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200933 cipher_info ) ) != 0 )
934 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200935 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200936 return( ret );
937 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200940 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200944 return( ret );
945 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200948 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200952 return( ret );
953 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#if defined(MBEDTLS_CIPHER_MODE_CBC)
956 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
959 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200962 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200963 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
966 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200969 return( ret );
970 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000971 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 mbedtls_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +0000977 // Initialize compression
978 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000980 {
Paul Bakker16770332013-10-11 09:59:44 +0200981 if( ssl->compress_buf == NULL )
982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200984 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +0200985 if( ssl->compress_buf == NULL )
986 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 MBEDTLS_SSL_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200989 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +0200990 }
991 }
992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000994
Paul Bakker48916f92012-09-16 19:57:18 +0000995 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
996 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000997
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200998 if( deflateInit( &transform->ctx_deflate,
999 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001000 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1003 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001004 }
1005 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001009
1010 return( 0 );
1011}
1012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#if defined(MBEDTLS_SSL_PROTO_SSL3)
1014void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001015{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001016 mbedtls_md5_context md5;
1017 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 unsigned char pad_1[48];
1019 unsigned char pad_2[48];
1020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001022
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001023 mbedtls_md5_init( &md5 );
1024 mbedtls_sha1_init( &sha1 );
1025
1026 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1027 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Paul Bakker380da532012-04-18 16:10:25 +00001029 memset( pad_1, 0x36, 48 );
1030 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001032 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1033 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1034 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001035
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001036 mbedtls_md5_starts_ret( &md5 );
1037 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1038 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1039 mbedtls_md5_update_ret( &md5, hash, 16 );
1040 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001042 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1043 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1044 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001045
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001046 mbedtls_sha1_starts_ret( &sha1 );
1047 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1048 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1049 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1050 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001054
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001055 mbedtls_md5_free( &md5 );
1056 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001057
Paul Bakker380da532012-04-18 16:10:25 +00001058 return;
1059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1063void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001064{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001065 mbedtls_md5_context md5;
1066 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001069
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001070 mbedtls_md5_init( &md5 );
1071 mbedtls_sha1_init( &sha1 );
1072
1073 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1074 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001075
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001076 mbedtls_md5_finish_ret( &md5, hash );
1077 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001081
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001082 mbedtls_md5_free( &md5 );
1083 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001084
Paul Bakker380da532012-04-18 16:10:25 +00001085 return;
1086}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1090#if defined(MBEDTLS_SHA256_C)
1091void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001092{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001093 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001094
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001095 mbedtls_sha256_init( &sha256 );
1096
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001098
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001099 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001100 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001104
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001105 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001106
Paul Bakker380da532012-04-18 16:10:25 +00001107 return;
1108}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#if defined(MBEDTLS_SHA512_C)
1112void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001113{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001114 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001115
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001116 mbedtls_sha512_init( &sha512 );
1117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001119
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001120 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001121 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001126 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001127
Paul Bakker5121ce52009-01-03 21:22:43 +00001128 return;
1129}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130#endif /* MBEDTLS_SHA512_C */
1131#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1134int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001135{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001136 unsigned char *p = ssl->handshake->premaster;
1137 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001138 const unsigned char *psk = ssl->conf->psk;
1139 size_t psk_len = ssl->conf->psk_len;
1140
1141 /* If the psk callback was called, use its result */
1142 if( ssl->handshake->psk != NULL )
1143 {
1144 psk = ssl->handshake->psk;
1145 psk_len = ssl->handshake->psk_len;
1146 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001147
1148 /*
1149 * PMS = struct {
1150 * opaque other_secret<0..2^16-1>;
1151 * opaque psk<0..2^16-1>;
1152 * };
1153 * with "other_secret" depending on the particular key exchange
1154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1156 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001157 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001158 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001160
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001161 *(p++) = (unsigned char)( psk_len >> 8 );
1162 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001163
1164 if( end < p || (size_t)( end - p ) < psk_len )
1165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1166
1167 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001168 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001169 }
1170 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1172#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1173 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001174 {
1175 /*
1176 * other_secret already set by the ClientKeyExchange message,
1177 * and is 48 bytes long
1178 */
Philippe Antoine33e5c322018-07-09 10:39:02 +02001179 if( end - p < 2 )
1180 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1181
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001182 *p++ = 0;
1183 *p++ = 48;
1184 p += 48;
1185 }
1186 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1188#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1189 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001190 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001191 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001192 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001193
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001194 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001196 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001197 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001200 return( ret );
1201 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001202 *(p++) = (unsigned char)( len >> 8 );
1203 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001204 p += len;
1205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001207 }
1208 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1210#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1211 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001212 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001213 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001214 size_t zlen;
1215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001217 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001218 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001221 return( ret );
1222 }
1223
1224 *(p++) = (unsigned char)( zlen >> 8 );
1225 *(p++) = (unsigned char)( zlen );
1226 p += zlen;
1227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001229 }
1230 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1234 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001235 }
1236
1237 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001238 if( end - p < 2 )
1239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001240
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001241 *(p++) = (unsigned char)( psk_len >> 8 );
1242 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001243
1244 if( end < p || (size_t)( end - p ) < psk_len )
1245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1246
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001247 memcpy( p, psk, psk_len );
1248 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001249
1250 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1251
1252 return( 0 );
1253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001257/*
1258 * SSLv3.0 MAC functions
1259 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001260#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001261static void ssl_mac( mbedtls_md_context_t *md_ctx,
1262 const unsigned char *secret,
1263 const unsigned char *buf, size_t len,
1264 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001265 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001266{
1267 unsigned char header[11];
1268 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001269 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1271 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001272
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001273 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001275 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001276 else
Paul Bakker68884e32013-01-07 18:20:04 +01001277 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001278
1279 memcpy( header, ctr, 8 );
1280 header[ 8] = (unsigned char) type;
1281 header[ 9] = (unsigned char)( len >> 8 );
1282 header[10] = (unsigned char)( len );
1283
Paul Bakker68884e32013-01-07 18:20:04 +01001284 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_md_starts( md_ctx );
1286 mbedtls_md_update( md_ctx, secret, md_size );
1287 mbedtls_md_update( md_ctx, padding, padlen );
1288 mbedtls_md_update( md_ctx, header, 11 );
1289 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001290 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Paul Bakker68884e32013-01-07 18:20:04 +01001292 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_md_starts( md_ctx );
1294 mbedtls_md_update( md_ctx, secret, md_size );
1295 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001296 mbedtls_md_update( md_ctx, out, md_size );
1297 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001298}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001302 defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001303#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001304#endif
1305
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02001306/* The function below is only used in the Lucky 13 counter-measure in
1307 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1308#if defined(SSL_SOME_MODES_USE_MAC) && \
1309 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1310 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1311 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1312/* This function makes sure every byte in the memory region is accessed
1313 * (in ascending addresses order) */
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001314static void ssl_read_memory( const unsigned char *p, size_t len )
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02001315{
1316 unsigned char acc = 0;
1317 volatile unsigned char force;
1318
1319 for( ; len != 0; p++, len-- )
1320 acc ^= *p;
1321
1322 force = acc;
1323 (void) force;
1324}
1325#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1326
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001327/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001331{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001333 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001337 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1340 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001341 }
1342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001346 ssl->out_msg, ssl->out_msglen );
1347
Hanno Beckerd33f1ca2017-09-18 10:55:31 +01001348 if( ssl->out_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
1349 {
Hanno Becker184f6752017-10-04 13:47:33 +01001350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1351 (unsigned) ssl->out_msglen,
Hanno Beckerd33f1ca2017-09-18 10:55:31 +01001352 MBEDTLS_SSL_MAX_CONTENT_LEN ) );
1353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1354 }
1355
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001357 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001359#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 if( mode == MBEDTLS_MODE_STREAM ||
1361 ( mode == MBEDTLS_MODE_CBC
1362#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1363 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001364#endif
1365 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_SSL_PROTO_SSL3)
1368 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001369 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001370 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001371
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001372 ssl_mac( &ssl->transform_out->md_ctx_enc,
1373 ssl->transform_out->mac_enc,
1374 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001375 ssl->out_ctr, ssl->out_msgtype,
1376 mac );
1377
1378 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001379 }
1380 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001381#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1383 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1384 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001385 {
Hanno Becker992b6872017-11-09 18:57:39 +00001386 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1389 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1390 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1391 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001392 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001393 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001395
1396 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001397 }
1398 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001399#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1402 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001403 }
1404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001406 ssl->out_msg + ssl->out_msglen,
1407 ssl->transform_out->maclen );
1408
1409 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001410 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001411 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001412#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001413
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001414 /*
1415 * Encrypt
1416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1418 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001420 int ret;
1421 size_t olen = 0;
1422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 "including %d bytes of padding",
1425 ssl->out_msglen, 0 ) );
1426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001428 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001429 ssl->transform_out->ivlen,
1430 ssl->out_msg, ssl->out_msglen,
1431 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001434 return( ret );
1435 }
1436
1437 if( ssl->out_msglen != olen )
1438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1440 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001441 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 }
Paul Bakker68884e32013-01-07 18:20:04 +01001443 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1445#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1446 if( mode == MBEDTLS_MODE_GCM ||
1447 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001448 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001449 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001450 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001451 unsigned char *enc_msg;
1452 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001453 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001455
Paul Bakkerca4ab492012-04-18 14:23:57 +00001456 memcpy( add_data, ssl->out_ctr, 8 );
1457 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001459 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001460 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1461 add_data[12] = ssl->out_msglen & 0xFF;
1462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakkerca4ab492012-04-18 14:23:57 +00001464 add_data, 13 );
1465
Paul Bakker68884e32013-01-07 18:20:04 +01001466 /*
1467 * Generate IV
1468 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001469 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1470 {
1471 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1473 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001474 }
1475
1476 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1477 ssl->out_ctr, 8 );
1478 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001481 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001482
Paul Bakker68884e32013-01-07 18:20:04 +01001483 /*
1484 * Fix pointer positions and message length with added IV
1485 */
1486 enc_msg = ssl->out_msg;
1487 enc_msglen = ssl->out_msglen;
1488 ssl->out_msglen += ssl->transform_out->ivlen -
1489 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker68884e32013-01-07 18:20:04 +01001492 "including %d bytes of padding",
1493 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001494
Paul Bakker68884e32013-01-07 18:20:04 +01001495 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001496 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 if( ( ret = mbedtls_cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001499 ssl->transform_out->iv_enc,
1500 ssl->transform_out->ivlen,
1501 add_data, 13,
1502 enc_msg, enc_msglen,
1503 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001504 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001507 return( ret );
1508 }
1509
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001510 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1513 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001514 }
1515
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001516 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001517 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001520 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001521 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001523#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001525 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001526 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001527 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001528 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001529
Paul Bakker48916f92012-09-16 19:57:18 +00001530 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1531 ssl->transform_out->ivlen;
1532 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 padlen = 0;
1534
1535 for( i = 0; i <= padlen; i++ )
1536 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1537
1538 ssl->out_msglen += padlen + 1;
1539
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001540 enc_msglen = ssl->out_msglen;
1541 enc_msg = ssl->out_msg;
1542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001544 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001545 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1546 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001549 {
1550 /*
1551 * Generate IV
1552 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001553 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001554 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001555 if( ret != 0 )
1556 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001557
Paul Bakker92be97b2013-01-02 17:30:03 +01001558 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001559 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001560
1561 /*
1562 * Fix pointer positions and message length with added IV
1563 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001564 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001565 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001566 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001567 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001571 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001572 ssl->out_msglen, ssl->transform_out->ivlen,
1573 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001576 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001577 ssl->transform_out->ivlen,
1578 enc_msg, enc_msglen,
1579 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001582 return( ret );
1583 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001584
Paul Bakkercca5b812013-08-31 17:40:26 +02001585 if( enc_msglen != olen )
1586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001589 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1592 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001593 {
1594 /*
1595 * Save IV in SSL3 and TLS1
1596 */
1597 memcpy( ssl->transform_out->iv_enc,
1598 ssl->transform_out->cipher_ctx_enc.iv,
1599 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001600 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001601#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001604 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001605 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001606 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1607
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001608 /*
1609 * MAC(MAC_write_key, seq_num +
1610 * TLSCipherText.type +
1611 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001612 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001613 * IV + // except for TLS 1.0
1614 * ENC(content + padding + padding_length));
1615 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001616 unsigned char pseudo_hdr[13];
1617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001619
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001620 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1621 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001622 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1623 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1628 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001629 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001630 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001632
Hanno Becker3d8c9072018-01-05 16:24:22 +00001633 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1634 ssl->transform_out->maclen );
1635
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001636 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001637 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001638 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001640 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001641 else
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001642#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1645 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001646 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001647
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001648 /* Make extra sure authentication was performed, exactly once */
1649 if( auth_done != 1 )
1650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1652 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001653 }
1654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001656
1657 return( 0 );
1658}
1659
Manuel Pégourié-Gonnard41df0f22020-07-28 11:35:39 +02001660#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001661/*
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001662 * Constant-flow conditional memcpy:
1663 * - if c1 == c2, equivalent to memcpy(dst, src, len),
1664 * - otherwise, a no-op,
1665 * but with execution flow independent of the values of c1 and c2.
1666 *
1667 * Use only bit operations to avoid branches that could be used by some
1668 * compilers on some platforms to translate comparison operators.
1669 */
Manuel Pégourié-Gonnard2da9a542020-07-28 11:56:05 +02001670static void mbedtls_ssl_cf_memcpy_if_eq( unsigned char *dst,
1671 const unsigned char *src,
1672 size_t len,
1673 size_t c1, size_t c2 )
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001674{
1675 /* diff = 0 if c1 == c2, non-zero otherwise */
1676 const size_t diff = c1 ^ c2;
1677
1678 /* MSVC has a warning about unary minus on unsigned integer types,
1679 * but this is well-defined and precisely what we want to do here. */
1680#if defined(_MSC_VER)
1681#pragma warning( push )
1682#pragma warning( disable : 4146 )
1683#endif
1684
Manuel Pégourié-Gonnard2f484bd2020-07-28 11:57:25 +02001685 /* diff_msb's most significant bit is equal to c1 != c2 */
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001686 const size_t diff_msb = ( diff | -diff );
1687
1688 /* diff1 = c1 != c2 */
1689 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
1690
1691 /* mask = c1 != c2 ? 0xff : 0x00 */
Manuel Pégourié-Gonnard2f484bd2020-07-28 11:57:25 +02001692 const unsigned char mask = (unsigned char) -diff1;
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001693
1694#if defined(_MSC_VER)
1695#pragma warning( pop )
1696#endif
1697
1698 /* dst[i] = c1 != c2 ? dst[i] : src[i] */
Manuel Pégourié-Gonnarde05e5762020-07-29 10:04:36 +02001699 size_t i;
1700 for( i = 0; i < len; i++ )
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001701 dst[i] = ( dst[i] & mask ) | ( src[i] & ~mask );
1702}
1703
1704/*
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001705 * Compute HMAC of variable-length data with constant flow.
Manuel Pégourié-Gonnard7cf5ebc2020-07-29 12:54:04 +02001706 *
1707 * Only works with MD-5, SHA-1, SHA-256 and SHA-384.
1708 * (Otherwise, computation of block_size needs to be adapted.)
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001709 */
1710int mbedtls_ssl_cf_hmac(
1711 mbedtls_md_context_t *ctx,
1712 const unsigned char *add_data, size_t add_data_len,
1713 const unsigned char *data, size_t data_len_secret,
1714 size_t min_data_len, size_t max_data_len,
1715 unsigned char *output )
1716{
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001717 /*
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001718 * This function breaks the HMAC abstraction and uses the md_clone()
1719 * extension to the MD API in order to get constant-flow behaviour.
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001720 *
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001721 * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
Manuel Pégourié-Gonnardec956b12020-07-28 11:42:31 +02001722 * concatenation, and okey/ikey are the XOR of the key with some fixed bit
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001723 * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001724 *
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001725 * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
1726 * minlen, then cloning the context, and for each byte up to maxlen
1727 * finishing up the hash computation, keeping only the correct result.
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001728 *
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001729 * Then we only need to compute HASH(okey + inner_hash) and we're done.
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001730 */
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001731 const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info );
Manuel Pégourié-Gonnardec956b12020-07-28 11:42:31 +02001732 /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5,
1733 * all of which have the same block size except SHA-384. */
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001734 const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
Manuel Pégourié-Gonnardc9ef5a22020-07-28 11:45:02 +02001735 const unsigned char * const ikey = ctx->hmac_ctx;
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001736 const unsigned char * const okey = ikey + block_size;
1737 const size_t hash_size = mbedtls_md_get_size( ctx->md_info );
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001738
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001739 unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
1740 mbedtls_md_context_t aux;
1741 size_t offset;
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001742 int ret;
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001743
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001744 mbedtls_md_init( &aux );
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001745
1746#define MD_CHK( func_call ) \
1747 do { \
1748 ret = (func_call); \
1749 if( ret != 0 ) \
1750 goto cleanup; \
1751 } while( 0 )
1752
1753 MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) );
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001754
1755 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
1756 * so we can start directly with the message */
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001757 MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) );
1758 MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) );
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001759
1760 /* For each possible length, compute the hash up to that point */
1761 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001762 {
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001763 MD_CHK( mbedtls_md_clone( &aux, ctx ) );
1764 MD_CHK( mbedtls_md_finish( &aux, aux_out ) );
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001765 /* Keep only the correct inner_hash in the output buffer */
1766 mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size,
1767 offset, data_len_secret );
1768
1769 if( offset < max_data_len )
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001770 MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) );
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001771 }
1772
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001773 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001774 MD_CHK( mbedtls_md_starts( ctx ) );
1775 MD_CHK( mbedtls_md_update( ctx, okey, block_size ) );
1776 MD_CHK( mbedtls_md_update( ctx, output, hash_size ) );
1777 MD_CHK( mbedtls_md_finish( ctx, output ) );
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001778
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001779 /* Done, get ready for next time */
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001780 MD_CHK( mbedtls_md_hmac_reset( ctx ) );
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001781
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001782#undef MD_CHK
1783
1784cleanup:
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001785 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnard0cd0c732020-07-28 11:49:42 +02001786 return( ret );
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001787}
Manuel Pégourié-Gonnard41df0f22020-07-28 11:35:39 +02001788#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001791{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001792 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001794 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001795#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001796 size_t padlen = 0, correct = 1;
1797#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001800
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001801 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1804 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001805 }
1806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001808
Paul Bakker48916f92012-09-16 19:57:18 +00001809 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001812 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001814 }
1815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1817 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001818 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001819 int ret;
1820 size_t olen = 0;
1821
Paul Bakker68884e32013-01-07 18:20:04 +01001822 padlen = 0;
1823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001825 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001826 ssl->transform_in->ivlen,
1827 ssl->in_msg, ssl->in_msglen,
1828 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001831 return( ret );
1832 }
1833
1834 if( ssl->in_msglen != olen )
1835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1837 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001839 }
Paul Bakker68884e32013-01-07 18:20:04 +01001840 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1842#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1843 if( mode == MBEDTLS_MODE_GCM ||
1844 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001845 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001846 int ret;
1847 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001848 unsigned char *dec_msg;
1849 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001850 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001851 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001853 size_t explicit_iv_len = ssl->transform_in->ivlen -
1854 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001855
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001856 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001859 "+ taglen (%d)", ssl->in_msglen,
1860 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001862 }
1863 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1864
Paul Bakker68884e32013-01-07 18:20:04 +01001865 dec_msg = ssl->in_msg;
1866 dec_msg_result = ssl->in_msg;
1867 ssl->in_msglen = dec_msglen;
1868
1869 memcpy( add_data, ssl->in_ctr, 8 );
1870 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001872 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001873 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1874 add_data[12] = ssl->in_msglen & 0xFF;
1875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakker68884e32013-01-07 18:20:04 +01001877 add_data, 13 );
1878
1879 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1880 ssl->in_iv,
1881 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
Paul Bakker68884e32013-01-07 18:20:04 +01001884 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001886
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001887 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001888 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001891 ssl->transform_in->iv_dec,
1892 ssl->transform_in->ivlen,
1893 add_data, 13,
1894 dec_msg, dec_msglen,
1895 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001896 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1901 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001902
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001903 return( ret );
1904 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001905 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001906
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001907 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1910 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001911 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001912 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001913 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001915#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001917 {
Paul Bakker45829992013-01-03 14:52:21 +01001918 /*
1919 * Decrypt and check the padding
1920 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001921 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001922 unsigned char *dec_msg;
1923 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001924 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001925 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001926 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001927
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 /*
Paul Bakker45829992013-01-03 14:52:21 +01001929 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1932 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001933 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001934#endif
Paul Bakker45829992013-01-03 14:52:21 +01001935
1936 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1937 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001940 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1941 ssl->transform_in->ivlen,
1942 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001944 }
1945
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001946 dec_msglen = ssl->in_msglen;
1947 dec_msg = ssl->in_msg;
1948 dec_msg_result = ssl->in_msg;
1949
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001950 /*
1951 * Authenticate before decrypt if enabled
1952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1954 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001955 {
Hanno Becker992b6872017-11-09 18:57:39 +00001956 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001957 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001960
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001961 dec_msglen -= ssl->transform_in->maclen;
1962 ssl->in_msglen -= ssl->transform_in->maclen;
1963
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001964 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1965 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1966 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1967 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1972 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001973 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001974 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001978 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001979 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001980 ssl->transform_in->maclen );
1981
Hanno Becker992b6872017-11-09 18:57:39 +00001982 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1983 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001988 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001989 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001990 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001992
1993 /*
1994 * Check length sanity
1995 */
1996 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001999 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002001 }
2002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002004 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002005 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002006 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002008 {
Paul Bakker48916f92012-09-16 19:57:18 +00002009 dec_msglen -= ssl->transform_in->ivlen;
2010 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002011
Paul Bakker48916f92012-09-16 19:57:18 +00002012 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002013 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002014 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002018 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002019 ssl->transform_in->ivlen,
2020 dec_msg, dec_msglen,
2021 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002024 return( ret );
2025 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002026
Paul Bakkercca5b812013-08-31 17:40:26 +02002027 if( dec_msglen != olen )
2028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2030 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002031 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2034 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002035 {
2036 /*
2037 * Save IV in SSL3 and TLS1
2038 */
2039 memcpy( ssl->transform_in->iv_dec,
2040 ssl->transform_in->cipher_ctx_dec.iv,
2041 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002042 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002044
2045 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002046
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002047 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002048 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050#if defined(MBEDTLS_SSL_DEBUG_ALL)
2051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002052 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002053#endif
Paul Bakker45829992013-01-03 14:52:21 +01002054 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002055 correct = 0;
2056 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058#if defined(MBEDTLS_SSL_PROTO_SSL3)
2059 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 {
Paul Bakker48916f92012-09-16 19:57:18 +00002061 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063#if defined(MBEDTLS_SSL_DEBUG_ALL)
2064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002066 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002067#endif
Paul Bakker45829992013-01-03 14:52:21 +01002068 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 }
2070 }
2071 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2073#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2074 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2075 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002076 {
2077 /*
Paul Bakker45829992013-01-03 14:52:21 +01002078 * TLSv1+: always check the padding up to the first failure
2079 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002081 size_t pad_count = 0, real_count = 1;
Angus Gratton1ba8e912018-06-19 15:57:50 +10002082 size_t padding_idx = ssl->in_msglen - padlen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002083
Paul Bakker956c9e02013-12-19 14:42:28 +01002084 /*
2085 * Padding is guaranteed to be incorrect if:
Angus Gratton1ba8e912018-06-19 15:57:50 +10002086 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002087 *
Angus Gratton1ba8e912018-06-19 15:57:50 +10002088 * 2. padding_idx > MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002089 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002090 *
2091 * In both cases we reset padding_idx to a safe value (0) to
2092 * prevent out-of-buffer reads.
2093 */
Angus Gratton1ba8e912018-06-19 15:57:50 +10002094 correct &= ( padlen <= ssl->in_msglen );
2095 correct &= ( padding_idx <= MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002096 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002097
2098 padding_idx *= correct;
2099
Angus Gratton1ba8e912018-06-19 15:57:50 +10002100 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002101 {
Angus Gratton1ba8e912018-06-19 15:57:50 +10002102 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002103 pad_count += real_count *
2104 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2105 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002106
2107 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002110 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002112#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002113 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002115 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2117 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002121 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002122
2123 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002125 else
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02002126#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2129 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002130 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002131
Manuel Pégourié-Gonnard7c344322018-07-10 11:15:36 +02002132#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard7c344322018-07-10 11:15:36 +02002135#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002136
2137 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002138 * Authenticate if not done yet.
2139 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002141#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002142 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002143 {
Hanno Becker992b6872017-11-09 18:57:39 +00002144 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnardbf7a49e2020-08-25 11:07:25 +02002145 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002146
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002147 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002148
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002149 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2150 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#if defined(MBEDTLS_SSL_PROTO_SSL3)
2153 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002154 {
2155 ssl_mac( &ssl->transform_in->md_ctx_dec,
2156 ssl->transform_in->mac_dec,
2157 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002158 ssl->in_ctr, ssl->in_msgtype,
2159 mac_expect );
Manuel Pégourié-Gonnardbf7a49e2020-08-25 11:07:25 +02002160 memcpy( mac_peer, ssl->in_msg + ssl->in_msglen,
2161 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002162 }
2163 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2165#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2166 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2167 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002168 {
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002169 int ret;
2170 unsigned char add_data[13];
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002171
2172 /*
2173 * The next two sizes are the minimum and maximum values of
2174 * in_msglen over all padlen values.
2175 *
2176 * They're independent of padlen, since we previously did
2177 * in_msglen -= padlen.
2178 *
2179 * Note that max_len + maclen is never more than the buffer
2180 * length, as we previously did in_msglen -= maclen too.
2181 */
2182 const size_t max_len = ssl->in_msglen + padlen;
2183 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2184
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002185 memcpy( add_data + 0, ssl->in_ctr, 8 );
2186 memcpy( add_data + 8, ssl->in_hdr, 3 );
2187 memcpy( add_data + 11, ssl->in_len, 2 );
2188
2189 ret = mbedtls_ssl_cf_hmac( &ssl->transform_in->md_ctx_dec,
2190 add_data, sizeof( add_data ),
2191 ssl->in_msg, ssl->in_msglen,
2192 min_len, max_len,
2193 mac_expect );
2194 if( ret != 0 )
Gilles Peskinebb07ca02018-06-06 17:23:31 +02002195 {
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002196 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
2197 return( ret );
Gilles Peskinebb07ca02018-06-06 17:23:31 +02002198 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002199
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002200 /* Make sure we access all the memory that could contain the MAC,
2201 * before we check it in the next code block. This makes the
2202 * synchronisation requirements for just-in-time Prime+Probe
2203 * attacks much tighter and hopefully impractical. */
2204 ssl_read_memory( ssl->in_msg + min_len,
2205 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnardbf7a49e2020-08-25 11:07:25 +02002206 memcpy( mac_peer, ssl->in_msg + ssl->in_msglen,
2207 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002208 }
2209 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2211 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2214 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002215 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002216
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002217#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002218 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
Manuel Pégourié-Gonnardbf7a49e2020-08-25 11:07:25 +02002219 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, ssl->transform_in->maclen );
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002220#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002221
Manuel Pégourié-Gonnardbf7a49e2020-08-25 11:07:25 +02002222 if( mbedtls_ssl_safer_memcmp( mac_peer, mac_expect,
Hanno Becker992b6872017-11-09 18:57:39 +00002223 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225#if defined(MBEDTLS_SSL_DEBUG_ALL)
2226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002227#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002228 correct = 0;
2229 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002230 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002231 }
Hanno Beckerca31b472018-10-17 14:43:14 +01002232
2233 /*
2234 * Finally check the correct flag
2235 */
2236 if( correct == 0 )
2237 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002238#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002239
2240 /* Make extra sure authentication was performed, exactly once */
2241 if( auth_done != 1 )
2242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2244 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002245 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002246
2247 if( ssl->in_msglen == 0 )
2248 {
Angus Grattonb91cb6e2018-06-19 15:58:22 +10002249#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2250 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2251 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2252 {
2253 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2255 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2256 }
2257#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2258
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 ssl->nb_zero++;
2260
2261 /*
2262 * Three or more empty messages may be a DoS attack
2263 * (excessive CPU consumption).
2264 */
2265 if( ssl->nb_zero > 3 )
2266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 }
2271 }
2272 else
2273 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002276 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002277 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002278 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002279 }
2280 else
2281#endif
2282 {
2283 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2284 if( ++ssl->in_ctr[i - 1] != 0 )
2285 break;
2286
2287 /* The loop goes to its end iff the counter is wrapping */
2288 if( i == ssl_ep_len( ssl ) )
2289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2291 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002292 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002293 }
2294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002296
2297 return( 0 );
2298}
2299
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002300#undef MAC_NONE
2301#undef MAC_PLAINTEXT
2302#undef MAC_CIPHERTEXT
2303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002305/*
2306 * Compression/decompression functions
2307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002309{
2310 int ret;
2311 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002312 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002313 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002314 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002317
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002318 if( len_pre == 0 )
2319 return( 0 );
2320
Paul Bakker2770fbd2012-07-03 13:30:23 +00002321 memcpy( msg_pre, ssl->out_msg, len_pre );
2322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002324 ssl->out_msglen ) );
2325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002327 ssl->out_msg, ssl->out_msglen );
2328
Paul Bakker48916f92012-09-16 19:57:18 +00002329 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2330 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2331 ssl->transform_out->ctx_deflate.next_out = msg_post;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002332 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002333
Paul Bakker48916f92012-09-16 19:57:18 +00002334 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002335 if( ret != Z_OK )
2336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2338 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002339 }
2340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 ssl->out_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002342 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002345 ssl->out_msglen ) );
2346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002348 ssl->out_msg, ssl->out_msglen );
2349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002351
2352 return( 0 );
2353}
2354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002356{
2357 int ret;
2358 unsigned char *msg_post = ssl->in_msg;
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002359 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002360 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002361 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002364
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002365 if( len_pre == 0 )
2366 return( 0 );
2367
Paul Bakker2770fbd2012-07-03 13:30:23 +00002368 memcpy( msg_pre, ssl->in_msg, len_pre );
2369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002371 ssl->in_msglen ) );
2372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002374 ssl->in_msg, ssl->in_msglen );
2375
Paul Bakker48916f92012-09-16 19:57:18 +00002376 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2377 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2378 ssl->transform_in->ctx_inflate.next_out = msg_post;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002379 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002380 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002381
Paul Bakker48916f92012-09-16 19:57:18 +00002382 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002383 if( ret != Z_OK )
2384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2386 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002387 }
2388
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002389 ssl->in_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002390 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002393 ssl->in_msglen ) );
2394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002396 ssl->in_msg, ssl->in_msglen );
2397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002399
2400 return( 0 );
2401}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2405static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407#if defined(MBEDTLS_SSL_PROTO_DTLS)
2408static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002409{
2410 /* If renegotiation is not enforced, retransmit until we would reach max
2411 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002412 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002413 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002414 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002415 unsigned char doublings = 1;
2416
2417 while( ratio != 0 )
2418 {
2419 ++doublings;
2420 ratio >>= 1;
2421 }
2422
2423 if( ++ssl->renego_records_seen > doublings )
2424 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002426 return( 0 );
2427 }
2428 }
2429
2430 return( ssl_write_hello_request( ssl ) );
2431}
2432#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002434
Paul Bakker5121ce52009-01-03 21:22:43 +00002435/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002436 * Fill the input message buffer by appending data to it.
2437 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002438 *
2439 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2440 * available (from this read and/or a previous one). Otherwise, an error code
2441 * is returned (possibly EOF or WANT_READ).
2442 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002443 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2444 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2445 * since we always read a whole datagram at once.
2446 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002447 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002448 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002451{
Paul Bakker23986e52011-04-24 08:57:21 +00002452 int ret;
2453 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002457 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002460 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002462 }
2463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 if( nb_want > MBEDTLS_SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2467 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002468 }
2469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002471 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002473 uint32_t timeout;
2474
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002475 /* Just to be sure */
2476 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2477 {
2478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2479 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2480 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2481 }
2482
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002483 /*
2484 * The point is, we need to always read a full datagram at once, so we
2485 * sometimes read more then requested, and handle the additional data.
2486 * It could be the rest of the current record (while fetching the
2487 * header) and/or some other records in the same datagram.
2488 */
2489
2490 /*
2491 * Move to the next record in the already read datagram if applicable
2492 */
2493 if( ssl->next_record_offset != 0 )
2494 {
2495 if( ssl->in_left < ssl->next_record_offset )
2496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2498 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002499 }
2500
2501 ssl->in_left -= ssl->next_record_offset;
2502
2503 if( ssl->in_left != 0 )
2504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002506 ssl->next_record_offset ) );
2507 memmove( ssl->in_hdr,
2508 ssl->in_hdr + ssl->next_record_offset,
2509 ssl->in_left );
2510 }
2511
2512 ssl->next_record_offset = 0;
2513 }
2514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002517
2518 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002519 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002520 */
2521 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002524 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002525 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002526
2527 /*
Antonin Décimo8fd91562019-01-23 15:24:37 +01002528 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002529 * are not at the beginning of a new record, the caller did something
2530 * wrong.
2531 */
2532 if( ssl->in_left != 0 )
2533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002536 }
2537
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002538 /*
2539 * Don't even try to read if time's out already.
2540 * This avoids by-passing the timer when repeatedly receiving messages
2541 * that will end up being dropped.
2542 */
2543 if( ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002544 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002545 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 len = MBEDTLS_SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002550 timeout = ssl->handshake->retransmit_timeout;
2551 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002552 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002555
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002556 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002557 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2558 timeout );
2559 else
2560 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002563
2564 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002566 }
2567
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002568 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002571 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002574 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002575 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002578 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002579 }
2580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002584 return( ret );
2585 }
2586
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002587 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002588 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002590 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002592 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002593 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002596 return( ret );
2597 }
2598
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002599 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002600 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002602 }
2603
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 if( ret < 0 )
2605 return( ret );
2606
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002607 ssl->in_left = ret;
2608 }
2609 else
2610#endif
2611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002613 ssl->in_left, nb_want ) );
2614
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002615 while( ssl->in_left < nb_want )
2616 {
2617 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002618
2619 if( ssl_check_timer( ssl ) != 0 )
2620 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2621 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002622 {
2623 if( ssl->f_recv_timeout != NULL )
2624 {
2625 ret = ssl->f_recv_timeout( ssl->p_bio,
2626 ssl->in_hdr + ssl->in_left, len,
2627 ssl->conf->read_timeout );
2628 }
2629 else
2630 {
2631 ret = ssl->f_recv( ssl->p_bio,
2632 ssl->in_hdr + ssl->in_left, len );
2633 }
2634 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002637 ssl->in_left, nb_want ) );
2638 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002639
2640 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002642
2643 if( ret < 0 )
2644 return( ret );
2645
makise-homura329fe7e2020-08-24 18:39:56 +03002646 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad1603b11af862018-03-19 07:18:13 -07002647 {
Jaeden Amerob5f53b12018-04-03 12:09:45 +01002648 MBEDTLS_SSL_DEBUG_MSG( 1,
2649 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160329ed80f2018-04-02 07:34:26 -07002650 ret, (unsigned long)len ) );
mohammad1603b11af862018-03-19 07:18:13 -07002651 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2652 }
2653
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002654 ssl->in_left += ret;
2655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 }
2657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
2660 return( 0 );
2661}
2662
2663/*
2664 * Flush any data not yet written
2665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002667{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002668 int ret;
2669 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002673 if( ssl->f_send == NULL )
2674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002676 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002678 }
2679
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002680 /* Avoid incrementing counter if data is flushed */
2681 if( ssl->out_left == 0 )
2682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002684 return( 0 );
2685 }
2686
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 while( ssl->out_left > 0 )
2688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2690 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) +
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002693 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002694 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
2698 if( ret <= 0 )
2699 return( ret );
2700
makise-homura329fe7e2020-08-24 18:39:56 +03002701 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16036085c722018-02-22 04:29:04 -08002702 {
Jaeden Amerob5f53b12018-04-03 12:09:45 +01002703 MBEDTLS_SSL_DEBUG_MSG( 1,
2704 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160329ed80f2018-04-02 07:34:26 -07002705 ret, (unsigned long)ssl->out_left ) );
mohammad16036085c722018-02-22 04:29:04 -08002706 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2707 }
2708
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 ssl->out_left -= ret;
2710 }
2711
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002712 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002713 if( ++ssl->out_ctr[i - 1] != 0 )
2714 break;
2715
2716 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002717 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2720 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002721 }
2722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
2725 return( 0 );
2726}
2727
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002728/*
2729 * Functions to handle the DTLS retransmission state machine
2730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002732/*
2733 * Append current handshake message to current outgoing flight
2734 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002736{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 mbedtls_ssl_flight_item *msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002738
2739 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002740 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002741 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002744 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002745 }
2746
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002747 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002748 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002751 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002752 }
2753
2754 /* Copy current handshake message with headers */
2755 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2756 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002757 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002758 msg->next = NULL;
2759
2760 /* Append to the current flight */
2761 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002762 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002763 else
2764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002766 while( cur->next != NULL )
2767 cur = cur->next;
2768 cur->next = msg;
2769 }
2770
2771 return( 0 );
2772}
2773
2774/*
2775 * Free the current flight of handshake messages
2776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002778{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 mbedtls_ssl_flight_item *cur = flight;
2780 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002781
2782 while( cur != NULL )
2783 {
2784 next = cur->next;
2785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786 mbedtls_free( cur->p );
2787 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002788
2789 cur = next;
2790 }
2791}
2792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2794static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002795#endif
2796
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002797/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002798 * Swap transform_out and out_ctr with the alternative ones
2799 */
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002800static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002801{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002803 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002804#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2805 int ret;
2806#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002807
2808 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002811 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002812 }
2813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002815
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002816 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002817 tmp_transform = ssl->transform_out;
2818 ssl->transform_out = ssl->handshake->alt_transform_out;
2819 ssl->handshake->alt_transform_out = tmp_transform;
2820
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002821 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002822 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2823 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2824 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002825
2826 /* Adjust to the newly activated transform */
2827 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002829 {
2830 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2831 ssl->transform_out->fixed_ivlen;
2832 }
2833 else
2834 ssl->out_msg = ssl->out_iv;
2835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2837 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2842 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002843 }
2844 }
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002845#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
2846
2847 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002848}
2849
2850/*
2851 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002852 *
2853 * Need to remember the current message in case flush_output returns
2854 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002855 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002858{
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002859 int ret;
2860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002866
2867 ssl->handshake->cur_msg = ssl->handshake->flight;
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002868 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
2869 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002872 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002873
2874 while( ssl->handshake->cur_msg != NULL )
2875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 mbedtls_ssl_flight_item *cur = ssl->handshake->cur_msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002877
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002878 /* Swap epochs before sending Finished: we can't do it after
2879 * sending ChangeCipherSpec, in case write returns WANT_READ.
2880 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2882 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002883 {
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002884 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
2885 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002886 }
2887
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002888 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002889 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002890 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002891
2892 ssl->handshake->cur_msg = cur->next;
2893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894 MBEDTLS_SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002899 return( ret );
2900 }
2901 }
2902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2904 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002905 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002908 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2909 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002912
2913 return( 0 );
2914}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002915
2916/*
2917 * To be called when the last message of an incoming flight is received.
2918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002920{
2921 /* We won't need to resend that one any more */
2922 ssl_flight_free( ssl->handshake->flight );
2923 ssl->handshake->flight = NULL;
2924 ssl->handshake->cur_msg = NULL;
2925
2926 /* The next incoming flight will start with this msg_seq */
2927 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2928
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002929 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002930 ssl_set_timer( ssl, 0 );
2931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2933 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002936 }
2937 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002939}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002940
2941/*
2942 * To be called when the last message of an outgoing flight is send.
2943 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002945{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002946 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002947 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2950 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002953 }
2954 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002956}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002958
Paul Bakker5121ce52009-01-03 21:22:43 +00002959/*
2960 * Record layer functions
2961 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002962
2963/*
2964 * Write current record.
2965 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2966 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002968{
Nicholas Wilsonf0021642016-04-13 11:51:05 +01002969 int ret, done = 0, out_msg_type;
Paul Bakker23986e52011-04-24 08:57:21 +00002970 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002975 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002976 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002978 {
2979 ; /* Skip special handshake treatment when resending */
2980 }
2981 else
2982#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002984 {
Nicholas Wilsonf0021642016-04-13 11:51:05 +01002985 out_msg_type = ssl->out_msg[0];
2986
2987 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002988 ssl->handshake == NULL )
2989 {
2990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2991 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2992 }
2993
Paul Bakker5121ce52009-01-03 21:22:43 +00002994 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2995 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2996 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2997
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002998 /*
2999 * DTLS has additional fields in the Handshake layer,
3000 * between the length field and the actual payload:
3001 * uint16 message_seq;
3002 * uint24 fragment_offset;
3003 * uint24 fragment_length;
3004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003006 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003007 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003008 /* Make room for the additional DTLS fields */
Hanno Becker9648f8b2017-09-18 10:55:54 +01003009 if( MBEDTLS_SSL_MAX_CONTENT_LEN - ssl->out_msglen < 8 )
3010 {
3011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3012 "size %u, maximum %u",
3013 (unsigned) ( ssl->in_hslen - 4 ),
3014 (unsigned) ( MBEDTLS_SSL_MAX_CONTENT_LEN - 12 ) ) );
3015 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3016 }
3017
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003018 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003019 ssl->out_msglen += 8;
3020 len += 8;
3021
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003022 /* Write message_seq and update it, except for HelloRequest */
Nicholas Wilsonf0021642016-04-13 11:51:05 +01003023 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003024 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003025 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3026 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3027 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003028 }
3029 else
3030 {
3031 ssl->out_msg[4] = 0;
3032 ssl->out_msg[5] = 0;
3033 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003034
3035 /* We don't fragment, so frag_offset = 0 and frag_len = len */
3036 memset( ssl->out_msg + 6, 0x00, 3 );
3037 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003038 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003040
Nicholas Wilsonf0021642016-04-13 11:51:05 +01003041 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003042 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 }
3044
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003045 /* Save handshake and CCS messages for resending */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003047 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003048 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049 ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING &&
3050 ( ssl->out_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ||
3051 ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003052 {
3053 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003056 return( ret );
3057 }
3058 }
3059#endif
3060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003062 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003064 {
3065 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003068 return( ret );
3069 }
3070
3071 len = ssl->out_msglen;
3072 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3076 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 ret = mbedtls_ssl_hw_record_write( ssl );
3081 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3084 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003085 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003086
3087 if( ret == 0 )
3088 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003089 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003091 if( !done )
3092 {
3093 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003095 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003096
3097 ssl->out_len[0] = (unsigned char)( len >> 8 );
3098 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003099
Paul Bakker48916f92012-09-16 19:57:18 +00003100 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003101 {
3102 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003105 return( ret );
3106 }
3107
3108 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003109 ssl->out_len[0] = (unsigned char)( len >> 8 );
3110 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003111 }
3112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003113 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00003114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00003116 "version = [%d:%d], msglen = %d",
3117 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003118 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3121 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003122 }
3123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003127 return( ret );
3128 }
3129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003131
3132 return( 0 );
3133}
3134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003136/*
3137 * Mark bits in bitmask (used for DTLS HS reassembly)
3138 */
3139static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3140{
3141 unsigned int start_bits, end_bits;
3142
3143 start_bits = 8 - ( offset % 8 );
3144 if( start_bits != 8 )
3145 {
3146 size_t first_byte_idx = offset / 8;
3147
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003148 /* Special case */
3149 if( len <= start_bits )
3150 {
3151 for( ; len != 0; len-- )
3152 mask[first_byte_idx] |= 1 << ( start_bits - len );
3153
3154 /* Avoid potential issues with offset or len becoming invalid */
3155 return;
3156 }
3157
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003158 offset += start_bits; /* Now offset % 8 == 0 */
3159 len -= start_bits;
3160
3161 for( ; start_bits != 0; start_bits-- )
3162 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3163 }
3164
3165 end_bits = len % 8;
3166 if( end_bits != 0 )
3167 {
3168 size_t last_byte_idx = ( offset + len ) / 8;
3169
3170 len -= end_bits; /* Now len % 8 == 0 */
3171
3172 for( ; end_bits != 0; end_bits-- )
3173 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3174 }
3175
3176 memset( mask + offset / 8, 0xFF, len / 8 );
3177}
3178
3179/*
3180 * Check that bitmask is full
3181 */
3182static int ssl_bitmask_check( unsigned char *mask, size_t len )
3183{
3184 size_t i;
3185
3186 for( i = 0; i < len / 8; i++ )
3187 if( mask[i] != 0xFF )
3188 return( -1 );
3189
3190 for( i = 0; i < len % 8; i++ )
3191 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3192 return( -1 );
3193
3194 return( 0 );
3195}
3196
3197/*
3198 * Reassemble fragmented DTLS handshake messages.
3199 *
3200 * Use a temporary buffer for reassembly, divided in two parts:
3201 * - the first holds the reassembled message (including handshake header),
3202 * - the second holds a bitmask indicating which parts of the message
3203 * (excluding headers) have been received so far.
3204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003206{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003207 unsigned char *msg, *bitmask;
3208 size_t frag_len, frag_off;
3209 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3210
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003211 if( ssl->handshake == NULL )
3212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3214 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003215 }
3216
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003217 /*
3218 * For first fragment, check size and allocate buffer
3219 */
3220 if( ssl->handshake->hs_msg == NULL )
3221 {
3222 size_t alloc_len;
3223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003225 msg_len ) );
3226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 if( ssl->in_hslen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3230 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003231 }
3232
3233 /* The bitmask needs one bit per byte of message excluding header */
3234 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3235
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003236 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003237 if( ssl->handshake->hs_msg == NULL )
3238 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003240 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003241 }
3242
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003243 /* Prepare final header: copy msg_type, length and message_seq,
3244 * then add standardised fragment_offset and fragment_length */
3245 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3246 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3247 memcpy( ssl->handshake->hs_msg + 9,
3248 ssl->handshake->hs_msg + 1, 3 );
3249 }
3250 else
3251 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003252 /* Make sure msg_type and length are consistent */
3253 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3256 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003257 }
3258 }
3259
3260 msg = ssl->handshake->hs_msg + 12;
3261 bitmask = msg + msg_len;
3262
3263 /*
3264 * Check and copy current fragment
3265 */
3266 frag_off = ( ssl->in_msg[6] << 16 ) |
3267 ( ssl->in_msg[7] << 8 ) |
3268 ssl->in_msg[8];
3269 frag_len = ( ssl->in_msg[9] << 16 ) |
3270 ( ssl->in_msg[10] << 8 ) |
3271 ssl->in_msg[11];
3272
3273 if( frag_off + frag_len > msg_len )
3274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003276 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003278 }
3279
3280 if( frag_len + 12 > ssl->in_msglen )
3281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003283 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003285 }
3286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003288 frag_off, frag_len ) );
3289
3290 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3291 ssl_bitmask_set( bitmask, frag_off, frag_len );
3292
3293 /*
3294 * Do we have the complete message by now?
3295 * If yes, finalize it, else ask to read the next record.
3296 */
3297 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003300 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003301 }
3302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003304
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003305 if( frag_len + 12 < ssl->in_msglen )
3306 {
3307 /*
3308 * We'got more handshake messages in the same record.
3309 * This case is not handled now because no know implementation does
3310 * that and it's hard to test, so we prefer to fail cleanly for now.
3311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3313 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003314 }
3315
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003316 if( ssl->in_left > ssl->next_record_offset )
3317 {
3318 /*
3319 * We've got more data in the buffer after the current record,
3320 * that we don't want to overwrite. Move it before writing the
3321 * reassembled message, and adjust in_left and next_record_offset.
3322 */
3323 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3324 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3325 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3326
3327 /* First compute and check new lengths */
3328 ssl->next_record_offset = new_remain - ssl->in_hdr;
3329 ssl->in_left = ssl->next_record_offset + remain_len;
3330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 if( ssl->in_left > MBEDTLS_SSL_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003332 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3335 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003336 }
3337
3338 memmove( new_remain, cur_remain, remain_len );
3339 }
3340
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003341 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3342
Hanno Beckerd82e0c02018-10-15 13:22:22 +01003343 mbedtls_zeroize( ssl->handshake->hs_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003345 ssl->handshake->hs_msg = NULL;
3346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003348 ssl->in_msg, ssl->in_hslen );
3349
3350 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003351}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003353
Simon Butcher99000142016-10-13 17:21:01 +01003354int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003355{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003359 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003361 }
3362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003364 ( ssl->in_msg[1] << 16 ) |
3365 ( ssl->in_msg[2] << 8 ) |
3366 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003369 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003370 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003373 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003374 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003375 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003376 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003377
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003378 /* ssl->handshake is NULL when receiving ClientHello for renego */
3379 if( ssl->handshake != NULL &&
3380 recv_msg_seq != ssl->handshake->in_msg_seq )
3381 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003382 /* Retransmit only on last message from previous flight, to avoid
3383 * too many retransmissions.
3384 * Besides, No sane server ever retransmits HelloVerifyRequest */
3385 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003389 "message_seq = %d, start_of_flight = %d",
3390 recv_msg_seq,
3391 ssl->handshake->in_flight_start_seq ) );
3392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003396 return( ret );
3397 }
3398 }
3399 else
3400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003402 "message_seq = %d, expected = %d",
3403 recv_msg_seq,
3404 ssl->handshake->in_msg_seq ) );
3405 }
3406
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003407 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003408 }
3409 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003410
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003411 /* Reassemble if current message is fragmented or reassembly is
3412 * already in progress */
3413 if( ssl->in_msglen < ssl->in_hslen ||
3414 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3415 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3416 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003419
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003420 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003423 return( ret );
3424 }
3425 }
3426 }
3427 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003429 /* With TLS we don't handle fragmentation (for now) */
3430 if( ssl->in_msglen < ssl->in_hslen )
3431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3433 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003434 }
3435
Simon Butcher99000142016-10-13 17:21:01 +01003436 return( 0 );
3437}
3438
3439void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3440{
3441
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003442 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3443 ssl->handshake != NULL )
3444 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003445 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003446 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003447
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003448 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003450 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003451 ssl->handshake != NULL )
3452 {
3453 ssl->handshake->in_msg_seq++;
3454 }
3455#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003456}
3457
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003458/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003459 * DTLS anti-replay: RFC 6347 4.1.2.6
3460 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003461 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3462 * Bit n is set iff record number in_window_top - n has been seen.
3463 *
3464 * Usually, in_window_top is the last record number seen and the lsb of
3465 * in_window is set. The only exception is the initial state (record number 0
3466 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3469static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003470{
3471 ssl->in_window_top = 0;
3472 ssl->in_window = 0;
3473}
3474
3475static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3476{
3477 return( ( (uint64_t) buf[0] << 40 ) |
3478 ( (uint64_t) buf[1] << 32 ) |
3479 ( (uint64_t) buf[2] << 24 ) |
3480 ( (uint64_t) buf[3] << 16 ) |
3481 ( (uint64_t) buf[4] << 8 ) |
3482 ( (uint64_t) buf[5] ) );
3483}
3484
3485/*
3486 * Return 0 if sequence number is acceptable, -1 otherwise
3487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003489{
3490 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3491 uint64_t bit;
3492
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003493 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003494 return( 0 );
3495
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003496 if( rec_seqnum > ssl->in_window_top )
3497 return( 0 );
3498
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003499 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003500
3501 if( bit >= 64 )
3502 return( -1 );
3503
3504 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3505 return( -1 );
3506
3507 return( 0 );
3508}
3509
3510/*
3511 * Update replay window on new validated record
3512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003514{
3515 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3516
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003517 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003518 return;
3519
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003520 if( rec_seqnum > ssl->in_window_top )
3521 {
3522 /* Update window_top and the contents of the window */
3523 uint64_t shift = rec_seqnum - ssl->in_window_top;
3524
3525 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003526 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003527 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003528 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003529 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003530 ssl->in_window |= 1;
3531 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003532
3533 ssl->in_window_top = rec_seqnum;
3534 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003535 else
3536 {
3537 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003538 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003539
3540 if( bit < 64 ) /* Always true, but be extra sure */
3541 ssl->in_window |= (uint64_t) 1 << bit;
3542 }
3543}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003545
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003546#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003547/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003548static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3549
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003550/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003551 * Without any SSL context, check if a datagram looks like a ClientHello with
3552 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003553 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003554 *
3555 * - if cookie is valid, return 0
3556 * - if ClientHello looks superficially valid but cookie is not,
3557 * fill obuf and set olen, then
3558 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3559 * - otherwise return a specific error code
3560 */
3561static int ssl_check_dtls_clihlo_cookie(
3562 mbedtls_ssl_cookie_write_t *f_cookie_write,
3563 mbedtls_ssl_cookie_check_t *f_cookie_check,
3564 void *p_cookie,
3565 const unsigned char *cli_id, size_t cli_id_len,
3566 const unsigned char *in, size_t in_len,
3567 unsigned char *obuf, size_t buf_len, size_t *olen )
3568{
3569 size_t sid_len, cookie_len;
3570 unsigned char *p;
3571
3572 if( f_cookie_write == NULL || f_cookie_check == NULL )
3573 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3574
3575 /*
3576 * Structure of ClientHello with record and handshake headers,
3577 * and expected values. We don't need to check a lot, more checks will be
3578 * done when actually parsing the ClientHello - skipping those checks
3579 * avoids code duplication and does not make cookie forging any easier.
3580 *
3581 * 0-0 ContentType type; copied, must be handshake
3582 * 1-2 ProtocolVersion version; copied
3583 * 3-4 uint16 epoch; copied, must be 0
3584 * 5-10 uint48 sequence_number; copied
3585 * 11-12 uint16 length; (ignored)
3586 *
3587 * 13-13 HandshakeType msg_type; (ignored)
3588 * 14-16 uint24 length; (ignored)
3589 * 17-18 uint16 message_seq; copied
3590 * 19-21 uint24 fragment_offset; copied, must be 0
3591 * 22-24 uint24 fragment_length; (ignored)
3592 *
3593 * 25-26 ProtocolVersion client_version; (ignored)
3594 * 27-58 Random random; (ignored)
3595 * 59-xx SessionID session_id; 1 byte len + sid_len content
3596 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3597 * ...
3598 *
3599 * Minimum length is 61 bytes.
3600 */
3601 if( in_len < 61 ||
3602 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3603 in[3] != 0 || in[4] != 0 ||
3604 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3605 {
3606 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3607 }
3608
3609 sid_len = in[59];
3610 if( sid_len > in_len - 61 )
3611 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3612
3613 cookie_len = in[60 + sid_len];
3614 if( cookie_len > in_len - 60 )
3615 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3616
3617 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3618 cli_id, cli_id_len ) == 0 )
3619 {
3620 /* Valid cookie */
3621 return( 0 );
3622 }
3623
3624 /*
3625 * If we get here, we've got an invalid cookie, let's prepare HVR.
3626 *
3627 * 0-0 ContentType type; copied
3628 * 1-2 ProtocolVersion version; copied
3629 * 3-4 uint16 epoch; copied
3630 * 5-10 uint48 sequence_number; copied
3631 * 11-12 uint16 length; olen - 13
3632 *
3633 * 13-13 HandshakeType msg_type; hello_verify_request
3634 * 14-16 uint24 length; olen - 25
3635 * 17-18 uint16 message_seq; copied
3636 * 19-21 uint24 fragment_offset; copied
3637 * 22-24 uint24 fragment_length; olen - 25
3638 *
3639 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3640 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3641 *
3642 * Minimum length is 28.
3643 */
3644 if( buf_len < 28 )
3645 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3646
3647 /* Copy most fields and adapt others */
3648 memcpy( obuf, in, 25 );
3649 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3650 obuf[25] = 0xfe;
3651 obuf[26] = 0xff;
3652
3653 /* Generate and write actual cookie */
3654 p = obuf + 28;
3655 if( f_cookie_write( p_cookie,
3656 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3657 {
3658 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3659 }
3660
3661 *olen = p - obuf;
3662
3663 /* Go back and fill length fields */
3664 obuf[27] = (unsigned char)( *olen - 28 );
3665
3666 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3667 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3668 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3669
3670 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3671 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3672
3673 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3674}
3675
3676/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003677 * Handle possible client reconnect with the same UDP quadruplet
3678 * (RFC 6347 Section 4.2.8).
3679 *
3680 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3681 * that looks like a ClientHello.
3682 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003683 * - if the input looks like a ClientHello without cookies,
3684 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003685 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003686 * - if the input looks like a ClientHello with a valid cookie,
3687 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003688 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003689 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003690 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003691 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003692 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3693 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003694 */
3695static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3696{
3697 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003698 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003699
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003700 /* Use out_msg as temporary buffer for writing out HelloVerifyRequest,
3701 * because the output buffer's already around. Don't use out_buf though,
3702 * as we don't want to overwrite out_ctr. */
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003703 ret = ssl_check_dtls_clihlo_cookie(
3704 ssl->conf->f_cookie_write,
3705 ssl->conf->f_cookie_check,
3706 ssl->conf->p_cookie,
3707 ssl->cli_id, ssl->cli_id_len,
3708 ssl->in_buf, ssl->in_left,
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003709 ssl->out_msg, MBEDTLS_SSL_MAX_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003710
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003711 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3712
3713 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003714 {
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003715 int send_ret;
3716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3717 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003718 ssl->out_msg, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003719 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003720 * If the error is permanent we'll catch it later,
3721 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003722 send_ret = ssl->f_send( ssl->p_bio, ssl->out_msg, len );
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003723 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3724 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003725
3726 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003727 }
3728
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003729 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003730 {
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003732 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3733 {
3734 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3735 return( ret );
3736 }
3737
3738 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003739 }
3740
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003741 return( ret );
3742}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003743#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003744
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003745/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003746 * ContentType type;
3747 * ProtocolVersion version;
3748 * uint16 epoch; // DTLS only
3749 * uint48 sequence_number; // DTLS only
3750 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003751 *
3752 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003753 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003754 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3755 *
3756 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003757 * 1. proceed with the record if this function returns 0
3758 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3759 * 3. return CLIENT_RECONNECT if this function return that value
3760 * 4. drop the whole datagram if this function returns anything else.
3761 * Point 2 is needed when the peer is resending, and we have already received
3762 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003765{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003766 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003768 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 +02003769
Paul Bakker5121ce52009-01-03 21:22:43 +00003770 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003771 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003772 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003775 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003776 ssl->in_msgtype,
3777 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003778
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003779 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3781 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3782 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3783 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003786
3787#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01003788 /* Silently ignore invalid DTLS records as recommended by RFC 6347
3789 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003790 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3791#endif /* MBEDTLS_SSL_PROTO_DTLS */
3792 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3793 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
3794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003796 }
3797
3798 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003799 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3802 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003803 }
3804
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003805 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3808 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003809 }
3810
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003811 /* Check length against the size of our buffer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003812 if( ssl->in_msglen > MBEDTLS_SSL_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003813 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3816 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003817 }
3818
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003819 /*
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003820 * DTLS-related tests.
3821 * Check epoch before checking length constraint because
3822 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3823 * message gets duplicated before the corresponding Finished message,
3824 * the second ChangeCipherSpec should be discarded because it belongs
3825 * to an old epoch, but not because its length is shorter than
3826 * the minimum record length for packets using the new record transform.
3827 * Note that these two kinds of failures are handled differently,
3828 * as an unexpected record is silently skipped but an invalid
3829 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003830 */
3831#if defined(MBEDTLS_SSL_PROTO_DTLS)
3832 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3833 {
3834 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3835
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003836 /* Check epoch (and sequence number) with DTLS */
3837 if( rec_epoch != ssl->in_epoch )
3838 {
3839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3840 "expected %d, received %d",
3841 ssl->in_epoch, rec_epoch ) );
3842
3843#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3844 /*
3845 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3846 * access the first byte of record content (handshake type), as we
3847 * have an active transform (possibly iv_len != 0), so use the
3848 * fact that the record header len is 13 instead.
3849 */
3850 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3851 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3852 rec_epoch == 0 &&
3853 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3854 ssl->in_left > 13 &&
3855 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3856 {
3857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3858 "from the same port" ) );
3859 return( ssl_handle_possible_reconnect( ssl ) );
3860 }
3861 else
3862#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3863 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3864 }
3865
3866#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3867 /* Replay detection only works for the current epoch */
3868 if( rec_epoch == ssl->in_epoch &&
3869 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3870 {
3871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3872 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3873 }
3874#endif
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003875
3876 /* Drop unexpected ChangeCipherSpec messages */
3877 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3878 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3879 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3880 {
3881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3882 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3883 }
3884
3885 /* Drop unexpected ApplicationData records,
3886 * except at the beginning of renegotiations */
3887 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
3888 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
3889#if defined(MBEDTLS_SSL_RENEGOTIATION)
3890 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
3891 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
3892#endif
3893 )
3894 {
3895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3896 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3897 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003898 }
3899#endif /* MBEDTLS_SSL_PROTO_DTLS */
3900
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003901
3902 /* Check length against bounds of the current transform and version */
3903 if( ssl->transform_in == NULL )
3904 {
3905 if( ssl->in_msglen < 1 ||
3906 ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
3907 {
3908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3909 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3910 }
3911 }
3912 else
3913 {
3914 if( ssl->in_msglen < ssl->transform_in->minlen )
3915 {
3916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3917 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3918 }
3919
3920#if defined(MBEDTLS_SSL_PROTO_SSL3)
3921 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3922 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_MAX_CONTENT_LEN )
3923 {
3924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3925 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3926 }
3927#endif
3928#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3929 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3930 /*
3931 * TLS encrypted messages can have up to 256 bytes of padding
3932 */
3933 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
3934 ssl->in_msglen > ssl->transform_in->minlen +
3935 MBEDTLS_SSL_MAX_CONTENT_LEN + 256 )
3936 {
3937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3938 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3939 }
3940#endif
3941 }
3942
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003943 return( 0 );
3944}
Paul Bakker5121ce52009-01-03 21:22:43 +00003945
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003946/*
3947 * If applicable, decrypt (and decompress) record content
3948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003950{
3951 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
3954 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3957 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961 ret = mbedtls_ssl_hw_record_read( ssl );
3962 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3965 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003966 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003967
3968 if( ret == 0 )
3969 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003970 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003972 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003973 {
3974 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003977 return( ret );
3978 }
3979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003980 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00003981 ssl->in_msg, ssl->in_msglen );
3982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983 if( ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3986 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003987 }
3988 }
3989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003991 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003993 {
3994 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003997 return( ret );
3998 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00003999 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004000#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004002#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004003 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004006 }
4007#endif
4008
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004009 return( 0 );
4010}
4011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004012static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004013
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004014/*
4015 * Read a record.
4016 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004017 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4018 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4019 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004022{
4023 int ret;
4024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004026
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004027 if( ssl->keep_current_message == 0 )
4028 {
4029 do {
Simon Butcher99000142016-10-13 17:21:01 +01004030
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004031 if( ( ret = mbedtls_ssl_read_record_layer( ssl ) ) != 0 )
4032 {
4033 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4034 return( ret );
4035 }
4036
4037 ret = mbedtls_ssl_handle_message_type( ssl );
4038
4039 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret );
4040
4041 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004042 {
4043 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4044 return( ret );
4045 }
4046
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004047 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4048 {
4049 mbedtls_ssl_update_handshake_status( ssl );
4050 }
Simon Butcher99000142016-10-13 17:21:01 +01004051 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004052 else
Simon Butcher99000142016-10-13 17:21:01 +01004053 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4055 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004056 }
4057
4058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4059
4060 return( 0 );
4061}
4062
4063int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4064{
4065 int ret;
4066
Hanno Becker4a810fb2017-05-24 16:27:30 +01004067 /*
4068 * Step A
4069 *
4070 * Consume last content-layer message and potentially
4071 * update in_msglen which keeps track of the contents'
4072 * consumption state.
4073 *
4074 * (1) Handshake messages:
4075 * Remove last handshake message, move content
4076 * and adapt in_msglen.
4077 *
4078 * (2) Alert messages:
4079 * Consume whole record content, in_msglen = 0.
4080 *
4081 * NOTE: This needs to be fixed, since like for
4082 * handshake messages it is allowed to have
4083 * multiple alerts witin a single record.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004084 * Internal reference IOTSSL-1321.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004085 *
4086 * (3) Change cipher spec:
4087 * Consume whole record content, in_msglen = 0.
4088 *
4089 * (4) Application data:
4090 * Don't do anything - the record layer provides
4091 * the application data as a stream transport
4092 * and consumes through mbedtls_ssl_read only.
4093 *
4094 */
4095
4096 /* Case (1): Handshake messages */
4097 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004098 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004099 /* Hard assertion to be sure that no application data
4100 * is in flight, as corrupting ssl->in_msglen during
4101 * ssl->in_offt != NULL is fatal. */
4102 if( ssl->in_offt != NULL )
4103 {
4104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4105 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4106 }
4107
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004108 /*
4109 * Get next Handshake message in the current record
4110 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004111
Hanno Becker4a810fb2017-05-24 16:27:30 +01004112 /* Notes:
4113 * (1) in_hslen is *NOT* necessarily the size of the
4114 * current handshake content: If DTLS handshake
4115 * fragmentation is used, that's the fragment
4116 * size instead. Using the total handshake message
4117 * size here is FAULTY and should be changed at
4118 * some point. Internal reference IOTSSL-1414.
4119 * (2) While it doesn't seem to cause problems, one
4120 * has to be very careful not to assume that in_hslen
4121 * is always <= in_msglen in a sensible communication.
4122 * Again, it's wrong for DTLS handshake fragmentation.
4123 * The following check is therefore mandatory, and
4124 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004125 * Additionally, ssl->in_hslen might be arbitrarily out of
4126 * bounds after handling a DTLS message with an unexpected
4127 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004128 */
4129 if( ssl->in_hslen < ssl->in_msglen )
4130 {
4131 ssl->in_msglen -= ssl->in_hslen;
4132 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4133 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004134
Hanno Becker4a810fb2017-05-24 16:27:30 +01004135 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4136 ssl->in_msg, ssl->in_msglen );
4137 }
4138 else
4139 {
4140 ssl->in_msglen = 0;
4141 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004142
Hanno Becker4a810fb2017-05-24 16:27:30 +01004143 ssl->in_hslen = 0;
4144 }
4145 /* Case (4): Application data */
4146 else if( ssl->in_offt != NULL )
4147 {
4148 return( 0 );
4149 }
4150 /* Everything else (CCS & Alerts) */
4151 else
4152 {
4153 ssl->in_msglen = 0;
4154 }
4155
4156 /*
4157 * Step B
4158 *
4159 * Fetch and decode new record if current one is fully consumed.
4160 *
4161 */
4162
4163 if( ssl->in_msglen > 0 )
4164 {
4165 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004166 return( 0 );
4167 }
4168
Hanno Becker4a810fb2017-05-24 16:27:30 +01004169 /* Need to fetch a new record */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004170
Simon Butcher99000142016-10-13 17:21:01 +01004171#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004172read_record_header:
Simon Butcher99000142016-10-13 17:21:01 +01004173#endif
4174
Hanno Becker4a810fb2017-05-24 16:27:30 +01004175 /* Current record either fully processed or to be discarded. */
4176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004177 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004180 return( ret );
4181 }
4182
4183 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004185#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004186 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4187 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004188 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004189 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4190 {
4191 /* Skip unexpected record (but not whole datagram) */
4192 ssl->next_record_offset = ssl->in_msglen
4193 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004194
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4196 "(header)" ) );
4197 }
4198 else
4199 {
4200 /* Skip invalid record and the rest of the datagram */
4201 ssl->next_record_offset = 0;
4202 ssl->in_left = 0;
4203
4204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4205 "(header)" ) );
4206 }
4207
4208 /* Get next record */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004209 goto read_record_header;
4210 }
4211#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004212 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004213 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004214
4215 /*
4216 * Read and optionally decrypt the message contents
4217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4219 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004221 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004222 return( ret );
4223 }
4224
4225 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004226#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004227 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004229 else
4230#endif
4231 ssl->in_left = 0;
4232
4233 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004235#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004236 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004237 {
4238 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4240 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004241 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004242 /* Except when waiting for Finished as a bad mac here
4243 * probably means something went wrong in the handshake
4244 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4245 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4246 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4247 {
4248#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4249 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4250 {
4251 mbedtls_ssl_send_alert_message( ssl,
4252 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4253 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4254 }
4255#endif
4256 return( ret );
4257 }
4258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004260 if( ssl->conf->badmac_limit != 0 &&
4261 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4264 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004265 }
4266#endif
4267
Hanno Becker4a810fb2017-05-24 16:27:30 +01004268 /* As above, invalid records cause
4269 * dismissal of the whole datagram. */
4270
4271 ssl->next_record_offset = 0;
4272 ssl->in_left = 0;
4273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004275 goto read_record_header;
4276 }
4277
4278 return( ret );
4279 }
4280 else
4281#endif
4282 {
4283 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4285 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287 mbedtls_ssl_send_alert_message( ssl,
4288 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4289 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004290 }
4291#endif
4292 return( ret );
4293 }
4294 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004295
4296 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004297 * When we sent the last flight of the handshake, we MUST respond to a
4298 * retransmit of the peer's previous flight with a retransmit. (In
4299 * practice, only the Finished message will make it, other messages
4300 * including CCS use the old transform so they're dropped as invalid.)
4301 *
4302 * If the record we received is not a handshake message, however, it
4303 * means the peer received our last flight so we can clean up
4304 * handshake info.
4305 *
4306 * This check needs to be done before prepare_handshake() due to an edge
4307 * case: if the client immediately requests renegotiation, this
4308 * finishes the current handshake first, avoiding the new ClientHello
4309 * being mistaken for an ancient message in the current handshake.
4310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004312 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004313 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004316 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4317 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004321 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004324 return( ret );
4325 }
4326
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01004327 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004328 }
4329 else
4330 {
4331 ssl_handshake_wrapup_free_hs_transform( ssl );
4332 }
4333 }
4334#endif
4335
Simon Butcher99000142016-10-13 17:21:01 +01004336 return( 0 );
4337}
4338
4339int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4340{
4341 int ret;
4342
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004343 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004344 * Handle particular types of records
4345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004347 {
Simon Butcher99000142016-10-13 17:21:01 +01004348 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4349 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004350 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004351 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004352 }
4353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004355 {
Angus Gratton8946b0d2018-06-20 15:43:50 +10004356 if( ssl->in_msglen != 2 )
4357 {
4358 /* Note: Standard allows for more than one 2 byte alert
4359 to be packed in a single message, but Mbed TLS doesn't
4360 currently support this. */
4361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4362 ssl->in_msglen ) );
4363 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4364 }
4365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004367 ssl->in_msg[0], ssl->in_msg[1] ) );
4368
4369 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004370 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004372 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004375 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004376 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004377 }
4378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004379 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4380 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4383 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004384 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004385
4386#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4387 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4388 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4389 {
4390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4391 /* Will be handled when trying to parse ServerHello */
4392 return( 0 );
4393 }
4394#endif
4395
4396#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4397 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4398 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4399 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4400 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4401 {
4402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4403 /* Will be handled in mbedtls_ssl_parse_certificate() */
4404 return( 0 );
4405 }
4406#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4407
4408 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004409 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004410 }
4411
Paul Bakker5121ce52009-01-03 21:22:43 +00004412 return( 0 );
4413}
4414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004415int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004416{
4417 int ret;
4418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004419 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4420 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4421 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004422 {
4423 return( ret );
4424 }
4425
4426 return( 0 );
4427}
4428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004429int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004430 unsigned char level,
4431 unsigned char message )
4432{
4433 int ret;
4434
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004435 if( ssl == NULL || ssl->conf == NULL )
4436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004439 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004441 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004442 ssl->out_msglen = 2;
4443 ssl->out_msg[0] = level;
4444 ssl->out_msg[1] = message;
4445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004446 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004449 return( ret );
4450 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004452
4453 return( 0 );
4454}
4455
Paul Bakker5121ce52009-01-03 21:22:43 +00004456/*
4457 * Handshake functions
4458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4460 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4461 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4462 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4463 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4464 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4465 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004466/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004468{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4474 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004475 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4476 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004479 ssl->state++;
4480 return( 0 );
4481 }
4482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4484 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004485}
4486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004487int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004488{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004489 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4494 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004495 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4496 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004499 ssl->state++;
4500 return( 0 );
4501 }
4502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4504 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004505}
Gilles Peskinef9828522017-05-03 12:28:43 +02004506
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004507#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004508/* Some certificate support -> implement write and parse */
4509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004511{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004513 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004514 const mbedtls_x509_crt *crt;
4515 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004519 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4520 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004521 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4522 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004525 ssl->state++;
4526 return( 0 );
4527 }
4528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004529#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004530 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004531 {
4532 if( ssl->client_auth == 0 )
4533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004535 ssl->state++;
4536 return( 0 );
4537 }
4538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004540 /*
4541 * If using SSLv3 and got no cert, send an Alert message
4542 * (otherwise an empty Certificate message will be sent).
4543 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4545 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004546 {
4547 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4549 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4550 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004553 goto write_msg;
4554 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004556 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004557#endif /* MBEDTLS_SSL_CLI_C */
4558#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004559 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4564 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004565 }
4566 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004567#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004569 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004570
4571 /*
4572 * 0 . 0 handshake type
4573 * 1 . 3 handshake length
4574 * 4 . 6 length of all certs
4575 * 7 . 9 length of cert. 1
4576 * 10 . n-1 peer certificate
4577 * n . n+2 length of cert. 2
4578 * n+3 . ... upper level cert, etc.
4579 */
4580 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004581 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004582
Paul Bakker29087132010-03-21 21:03:34 +00004583 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004584 {
4585 n = crt->raw.len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586 if( n > MBEDTLS_SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
4589 i + 3 + n, MBEDTLS_SSL_MAX_CONTENT_LEN ) );
4590 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004591 }
4592
4593 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4594 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4595 ssl->out_msg[i + 2] = (unsigned char)( n );
4596
4597 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4598 i += n; crt = crt->next;
4599 }
4600
4601 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4602 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4603 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4604
4605 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004606 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4607 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004608
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004609#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004610write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004611#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004612
4613 ssl->state++;
4614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004615 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004617 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004618 return( ret );
4619 }
4620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004622
Paul Bakkered27a042013-04-18 22:46:23 +02004623 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004624}
4625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004626int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004627{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004628 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004629 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004630 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004631 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004632 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004636 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4637 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004638 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4639 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004642 ssl->state++;
4643 return( 0 );
4644 }
4645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004646#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004647 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004648 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4649 {
4650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4651 ssl->state++;
4652 return( 0 );
4653 }
4654
4655#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4656 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4657 authmode = ssl->handshake->sni_authmode;
4658#endif
4659
4660 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4661 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004662 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004663 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004665 ssl->state++;
4666 return( 0 );
4667 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004668#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004670 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004671 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004672 /* mbedtls_ssl_read_record may have sent an alert already. We
4673 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004674 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004675 return( ret );
4676 }
4677
4678 ssl->state++;
4679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004680#if defined(MBEDTLS_SSL_SRV_C)
4681#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004682 /*
4683 * Check if the client sent an empty certificate
4684 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004685 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004686 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004687 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004688 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004689 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4690 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4691 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004694
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004695 /* The client was asked for a certificate but didn't send
4696 one. The client should know what's going on, so we
4697 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004698 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004699 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004700 return( 0 );
4701 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004702 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004703 }
4704 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004705#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004707#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4708 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004709 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004710 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4713 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4714 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4715 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004718
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004719 /* The client was asked for a certificate but didn't send
4720 one. The client should know what's going on, so we
4721 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004722 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004723 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004724 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004725 else
4726 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004727 }
4728 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004729#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4730 MBEDTLS_SSL_PROTO_TLS1_2 */
4731#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004733 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004736 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4737 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004738 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004739 }
4740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004741 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4742 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004745 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4746 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004747 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004748 }
4749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004750 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004751
Paul Bakker5121ce52009-01-03 21:22:43 +00004752 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004753 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004754 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004755 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004756
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004757 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004758 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004761 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4762 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004764 }
4765
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004766 /* In case we tried to reuse a session but it failed */
4767 if( ssl->session_negotiate->peer_cert != NULL )
4768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004769 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4770 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004771 }
4772
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004773 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004774 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004775 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004777 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004778 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4779 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004780 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004781 }
4782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004783 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004784
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004785 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004786
4787 while( i < ssl->in_hslen )
4788 {
Philippe Antoine33e5c322018-07-09 10:39:02 +02004789 if ( i + 3 > ssl->in_hslen ) {
4790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4791 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4792 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4793 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
4794 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004795 if( ssl->in_msg[i] != 0 )
4796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004798 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4799 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004800 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004801 }
4802
4803 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4804 | (unsigned int) ssl->in_msg[i + 2];
4805 i += 3;
4806
4807 if( n < 128 || i + n > ssl->in_hslen )
4808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004810 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4811 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004813 }
4814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004816 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004817 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004818 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004819 case 0: /*ok*/
4820 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
4821 /* Ignore certificate with an unknown algorithm: maybe a
4822 prior certificate was already trusted. */
4823 break;
4824
4825 case MBEDTLS_ERR_X509_ALLOC_FAILED:
4826 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
4827 goto crt_parse_der_failed;
4828
4829 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
4830 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4831 goto crt_parse_der_failed;
4832
4833 default:
4834 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4835 crt_parse_der_failed:
4836 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004838 return( ret );
4839 }
4840
4841 i += n;
4842 }
4843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004844 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004845
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004846 /*
4847 * On client, make sure the server cert doesn't change during renego to
4848 * avoid "triple handshake" attack: https://secure-resumption.com/
4849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004851 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004853 {
4854 if( ssl->session->peer_cert == NULL )
4855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004857 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4858 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004859 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004860 }
4861
4862 if( ssl->session->peer_cert->raw.len !=
4863 ssl->session_negotiate->peer_cert->raw.len ||
4864 memcmp( ssl->session->peer_cert->raw.p,
4865 ssl->session_negotiate->peer_cert->raw.p,
4866 ssl->session->peer_cert->raw.len ) != 0 )
4867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004869 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4870 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004871 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004872 }
4873 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004874#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004875
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004876 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004877 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004878 mbedtls_x509_crt *ca_chain;
4879 mbedtls_x509_crl *ca_crl;
4880
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004881#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004882 if( ssl->handshake->sni_ca_chain != NULL )
4883 {
4884 ca_chain = ssl->handshake->sni_ca_chain;
4885 ca_crl = ssl->handshake->sni_ca_crl;
4886 }
4887 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004888#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004889 {
4890 ca_chain = ssl->conf->ca_chain;
4891 ca_crl = ssl->conf->ca_crl;
4892 }
4893
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004894 /*
4895 * Main check: verify certificate
4896 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004897 ret = mbedtls_x509_crt_verify_with_profile(
4898 ssl->session_negotiate->peer_cert,
4899 ca_chain, ca_crl,
4900 ssl->conf->cert_profile,
4901 ssl->hostname,
4902 &ssl->session_negotiate->verify_result,
4903 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004904
4905 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004907 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004908 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004909
4910 /*
4911 * Secondary checks: always done, but change 'ret' only if it was 0
4912 */
4913
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004914#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004916 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004917
4918 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004919 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004920 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004921 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004922 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
4923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004925 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004926 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004927 }
4928 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004929#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004931 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004932 ciphersuite_info,
4933 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004934 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004937 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004938 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004939 }
4940
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004941 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
4942 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
4943 * with details encoded in the verification flags. All other kinds
4944 * of error codes, including those from the user provided f_vrfy
4945 * functions, are treated as fatal and lead to a failure of
4946 * ssl_parse_certificate even if verification was optional. */
4947 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
4948 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
4949 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
4950 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004951 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004952 }
4953
4954 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
4955 {
4956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
4957 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
4958 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004959
4960 if( ret != 0 )
4961 {
4962 /* The certificate may have been rejected for several reasons.
4963 Pick one and send the corresponding alert. Which alert to send
4964 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004965 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
4966 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
4967 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
4968 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4969 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
4970 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4971 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
4972 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4973 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
4974 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4975 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
4976 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4977 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
4978 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4979 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
4980 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
4981 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
4982 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
4983 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
4984 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02004985 else
4986 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004987 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4988 alert );
4989 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004990
Hanno Beckere6706e62017-05-15 16:05:15 +01004991#if defined(MBEDTLS_DEBUG_C)
4992 if( ssl->session_negotiate->verify_result != 0 )
4993 {
4994 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
4995 ssl->session_negotiate->verify_result ) );
4996 }
4997 else
4998 {
4999 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5000 }
5001#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005002 }
5003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005005
5006 return( ret );
5007}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005008#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5009 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5010 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5011 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5012 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5013 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5014 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005016int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005017{
5018 int ret;
5019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005022 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005023 ssl->out_msglen = 1;
5024 ssl->out_msg[0] = 1;
5025
Paul Bakker5121ce52009-01-03 21:22:43 +00005026 ssl->state++;
5027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005028 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005031 return( ret );
5032 }
5033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005035
5036 return( 0 );
5037}
5038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005039int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005040{
5041 int ret;
5042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005045 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005047 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005048 return( ret );
5049 }
5050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005051 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005054 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5055 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005057 }
5058
5059 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005062 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5063 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005064 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005065 }
5066
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005067 /*
5068 * Switch to our negotiated transform and session parameters for inbound
5069 * data.
5070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005072 ssl->transform_in = ssl->transform_negotiate;
5073 ssl->session_in = ssl->session_negotiate;
5074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005075#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005076 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005078#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005079 ssl_dtls_replay_reset( ssl );
5080#endif
5081
5082 /* Increment epoch */
5083 if( ++ssl->in_epoch == 0 )
5084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005086 /* This is highly unlikely to happen for legitimate reasons, so
5087 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005088 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005089 }
5090 }
5091 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005092#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005093 memset( ssl->in_ctr, 0, 8 );
5094
5095 /*
5096 * Set the in_msg pointer to the correct location based on IV length
5097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005099 {
5100 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
5101 ssl->transform_negotiate->fixed_ivlen;
5102 }
5103 else
5104 ssl->in_msg = ssl->in_iv;
5105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005106#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5107 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005111 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005112 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5113 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005114 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005115 }
5116 }
5117#endif
5118
Paul Bakker5121ce52009-01-03 21:22:43 +00005119 ssl->state++;
5120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005122
5123 return( 0 );
5124}
5125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5127 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005128{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005129 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005131#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5132 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5133 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005134 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005135 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005136#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5138#if defined(MBEDTLS_SHA512_C)
5139 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005140 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5141 else
5142#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005143#if defined(MBEDTLS_SHA256_C)
5144 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005145 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005146 else
5147#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005148#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005151 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005152 }
Paul Bakker380da532012-04-18 16:10:25 +00005153}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005155void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005157#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5158 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005159 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5160 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005161#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5163#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005164 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005165#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005166#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005167 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005168#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005169#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005170}
5171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005172static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005173 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005174{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005175#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5176 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005177 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5178 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005179#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005180#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5181#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005182 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005183#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005184#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005185 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005187#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005188}
5189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005190#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5191 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5192static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005193 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005194{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005195 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5196 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005197}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005198#endif
Paul Bakker380da532012-04-18 16:10:25 +00005199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5201#if defined(MBEDTLS_SHA256_C)
5202static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005203 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005204{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005205 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005206}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005207#endif
Paul Bakker380da532012-04-18 16:10:25 +00005208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209#if defined(MBEDTLS_SHA512_C)
5210static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005211 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005212{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005213 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005214}
Paul Bakker769075d2012-11-24 11:26:46 +01005215#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005216#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005219static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005221{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005222 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005223 mbedtls_md5_context md5;
5224 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005225
Paul Bakker5121ce52009-01-03 21:22:43 +00005226 unsigned char padbuf[48];
5227 unsigned char md5sum[16];
5228 unsigned char sha1sum[20];
5229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005230 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005231 if( !session )
5232 session = ssl->session;
5233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005235
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005236 mbedtls_md5_init( &md5 );
5237 mbedtls_sha1_init( &sha1 );
5238
5239 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5240 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005241
5242 /*
5243 * SSLv3:
5244 * hash =
5245 * MD5( master + pad2 +
5246 * MD5( handshake + sender + master + pad1 ) )
5247 * + SHA1( master + pad2 +
5248 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005249 */
5250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005251#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005252 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5253 md5.state, sizeof( md5.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#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005257 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5258 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005259#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005261 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005262 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005263
Paul Bakker1ef83d62012-04-11 12:09:53 +00005264 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005265
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005266 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5267 mbedtls_md5_update_ret( &md5, session->master, 48 );
5268 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5269 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005270
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005271 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5272 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5273 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5274 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005275
Paul Bakker1ef83d62012-04-11 12:09:53 +00005276 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005277
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005278 mbedtls_md5_starts_ret( &md5 );
5279 mbedtls_md5_update_ret( &md5, session->master, 48 );
5280 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5281 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5282 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005283
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005284 mbedtls_sha1_starts_ret( &sha1 );
5285 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5286 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5287 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5288 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005290 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005291
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005292 mbedtls_md5_free( &md5 );
5293 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
5296 mbedtls_zeroize( md5sum, sizeof( md5sum ) );
5297 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005301#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005304static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005306{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005307 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005308 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005309 mbedtls_md5_context md5;
5310 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005311 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005314 if( !session )
5315 session = ssl->session;
5316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005318
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005319 mbedtls_md5_init( &md5 );
5320 mbedtls_sha1_init( &sha1 );
5321
5322 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5323 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005324
Paul Bakker1ef83d62012-04-11 12:09:53 +00005325 /*
5326 * TLSv1:
5327 * hash = PRF( master, finished_label,
5328 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5329 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005332 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5333 md5.state, sizeof( md5.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#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005337 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5338 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005339#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005342 ? "client finished"
5343 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005344
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005345 mbedtls_md5_finish_ret( &md5, padbuf );
5346 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005347
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005348 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005349 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005351 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005352
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005353 mbedtls_md5_free( &md5 );
5354 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005356 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005359}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005360#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005362#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5363#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005364static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005365 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005366{
5367 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005368 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005369 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005370 unsigned char padbuf[32];
5371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005372 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005373 if( !session )
5374 session = ssl->session;
5375
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005376 mbedtls_sha256_init( &sha256 );
5377
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005379
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005380 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005381
5382 /*
5383 * TLSv1.2:
5384 * hash = PRF( master, finished_label,
5385 * Hash( handshake ) )[0.11]
5386 */
5387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388#if !defined(MBEDTLS_SHA256_ALT)
5389 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005390 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005391#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005394 ? "client finished"
5395 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005396
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005397 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005398
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005399 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005400 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005403
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005404 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005409}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005412#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005413static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005414 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005415{
5416 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005417 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005418 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005419 unsigned char padbuf[48];
5420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005422 if( !session )
5423 session = ssl->session;
5424
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005425 mbedtls_sha512_init( &sha512 );
5426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005428
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005429 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005430
5431 /*
5432 * TLSv1.2:
5433 * hash = PRF( master, finished_label,
5434 * Hash( handshake ) )[0.11]
5435 */
5436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005438 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5439 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005440#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005442 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005443 ? "client finished"
5444 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005445
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005446 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005447
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005448 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005449 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005452
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005453 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005458}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005459#endif /* MBEDTLS_SHA512_C */
5460#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005463{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005465
5466 /*
5467 * Free our handshake params
5468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 mbedtls_ssl_handshake_free( ssl->handshake );
5470 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005471 ssl->handshake = NULL;
5472
5473 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005474 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005475 */
5476 if( ssl->transform )
5477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478 mbedtls_ssl_transform_free( ssl->transform );
5479 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005480 }
5481 ssl->transform = ssl->transform_negotiate;
5482 ssl->transform_negotiate = NULL;
5483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005485}
5486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005488{
5489 int resume = ssl->handshake->resume;
5490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005493#if defined(MBEDTLS_SSL_RENEGOTIATION)
5494 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005496 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005497 ssl->renego_records_seen = 0;
5498 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005499#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005500
5501 /*
5502 * Free the previous session and switch in the current one
5503 */
Paul Bakker0a597072012-09-25 21:55:46 +00005504 if( ssl->session )
5505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005507 /* RFC 7366 3.1: keep the EtM state */
5508 ssl->session_negotiate->encrypt_then_mac =
5509 ssl->session->encrypt_then_mac;
5510#endif
5511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512 mbedtls_ssl_session_free( ssl->session );
5513 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005514 }
5515 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005516 ssl->session_negotiate = NULL;
5517
Paul Bakker0a597072012-09-25 21:55:46 +00005518 /*
5519 * Add cache entry
5520 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005521 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005522 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005523 resume == 0 )
5524 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005525 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005527 }
Paul Bakker0a597072012-09-25 21:55:46 +00005528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005530 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005531 ssl->handshake->flight != NULL )
5532 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005533 /* Cancel handshake timer */
5534 ssl_set_timer( ssl, 0 );
5535
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005536 /* Keep last flight around in case we need to resend it:
5537 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005539 }
5540 else
5541#endif
5542 ssl_handshake_wrapup_free_hs_transform( ssl );
5543
Paul Bakker48916f92012-09-16 19:57:18 +00005544 ssl->state++;
5545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005547}
5548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005550{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005551 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005554
Paul Bakker92be97b2013-01-02 17:30:03 +01005555 /*
5556 * Set the out_msg pointer to the correct location based on IV length
5557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005559 {
5560 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5561 ssl->transform_negotiate->fixed_ivlen;
5562 }
5563 else
5564 ssl->out_msg = ssl->out_iv;
5565
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005566 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005567
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005568 /*
5569 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5570 * may define some other value. Currently (early 2016), no defined
5571 * ciphersuite does this (and this is unlikely to change as activity has
5572 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005576#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005577 ssl->verify_data_len = hash_len;
5578 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005579#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005580
Paul Bakker5121ce52009-01-03 21:22:43 +00005581 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005582 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5583 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005584
5585 /*
5586 * In case of session resuming, invert the client and server
5587 * ChangeCipherSpec messages order.
5588 */
Paul Bakker0a597072012-09-25 21:55:46 +00005589 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005591#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005592 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005594#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005596 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005597 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005598#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005599 }
5600 else
5601 ssl->state++;
5602
Paul Bakker48916f92012-09-16 19:57:18 +00005603 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005604 * Switch to our negotiated transform and session parameters for outbound
5605 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005610 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005611 {
5612 unsigned char i;
5613
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005614 /* Remember current epoch settings for resending */
5615 ssl->handshake->alt_transform_out = ssl->transform_out;
5616 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5617
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005618 /* Set sequence_number to zero */
5619 memset( ssl->out_ctr + 2, 0, 6 );
5620
5621 /* Increment epoch */
5622 for( i = 2; i > 0; i-- )
5623 if( ++ssl->out_ctr[i - 1] != 0 )
5624 break;
5625
5626 /* The loop goes to its end iff the counter is wrapping */
5627 if( i == 0 )
5628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5630 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005631 }
5632 }
5633 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005634#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005635 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005636
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005637 ssl->transform_out = ssl->transform_negotiate;
5638 ssl->session_out = ssl->session_negotiate;
5639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5641 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5646 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005647 }
5648 }
5649#endif
5650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005651#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005652 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005653 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005654#endif
5655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005656 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005659 return( ret );
5660 }
5661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005663
5664 return( 0 );
5665}
5666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005667#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005668#define SSL_MAX_HASH_LEN 36
5669#else
5670#define SSL_MAX_HASH_LEN 12
5671#endif
5672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005674{
Paul Bakker23986e52011-04-24 08:57:21 +00005675 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005676 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005677 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005680
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005681 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005686 return( ret );
5687 }
5688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005692 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5693 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005695 }
5696
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005697 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005698#if defined(MBEDTLS_SSL_PROTO_SSL3)
5699 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005700 hash_len = 36;
5701 else
5702#endif
5703 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005705 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5706 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005709 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5710 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005711 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005712 }
5713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005715 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005718 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5719 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005720 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005721 }
5722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005723#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005724 ssl->verify_data_len = hash_len;
5725 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005726#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005727
Paul Bakker0a597072012-09-25 21:55:46 +00005728 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005731 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005733#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005735 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005736 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005737#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005738 }
5739 else
5740 ssl->state++;
5741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005742#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005743 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005744 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005745#endif
5746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005748
5749 return( 0 );
5750}
5751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005752static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005753{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005756#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5757 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5758 mbedtls_md5_init( &handshake->fin_md5 );
5759 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005760 mbedtls_md5_starts_ret( &handshake->fin_md5 );
5761 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005762#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5764#if defined(MBEDTLS_SHA256_C)
5765 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005766 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005767#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768#if defined(MBEDTLS_SHA512_C)
5769 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005770 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005771#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005772#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005773
5774 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005775
5776#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5777 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5778 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5779#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005781#if defined(MBEDTLS_DHM_C)
5782 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005783#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784#if defined(MBEDTLS_ECDH_C)
5785 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005786#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02005787#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005788 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02005789#if defined(MBEDTLS_SSL_CLI_C)
5790 handshake->ecjpake_cache = NULL;
5791 handshake->ecjpake_cache_len = 0;
5792#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005793#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005794
5795#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5796 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5797#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005798}
5799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005801{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5805 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807 mbedtls_md_init( &transform->md_ctx_enc );
5808 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005809}
5810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005812{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005813 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005814}
5815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005816static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005817{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005818 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005819 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005821 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005822 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005823 if( ssl->handshake )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 mbedtls_ssl_handshake_free( ssl->handshake );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005825
5826 /*
5827 * Either the pointers are now NULL or cleared properly and can be freed.
5828 * Now allocate missing structures.
5829 */
5830 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005831 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005832 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005833 }
Paul Bakker48916f92012-09-16 19:57:18 +00005834
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005835 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005836 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005837 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005838 }
Paul Bakker48916f92012-09-16 19:57:18 +00005839
Paul Bakker82788fb2014-10-20 13:59:19 +02005840 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005841 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005842 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005843 }
Paul Bakker48916f92012-09-16 19:57:18 +00005844
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005845 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005846 if( ssl->handshake == NULL ||
5847 ssl->transform_negotiate == NULL ||
5848 ssl->session_negotiate == NULL )
5849 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005852 mbedtls_free( ssl->handshake );
5853 mbedtls_free( ssl->transform_negotiate );
5854 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005855
5856 ssl->handshake = NULL;
5857 ssl->transform_negotiate = NULL;
5858 ssl->session_negotiate = NULL;
5859
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005860 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005861 }
5862
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005863 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005865 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005866 ssl_handshake_params_init( ssl->handshake );
5867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005869 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5870 {
5871 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005872
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005873 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5874 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5875 else
5876 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005877
5878 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005879 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005880#endif
5881
Paul Bakker48916f92012-09-16 19:57:18 +00005882 return( 0 );
5883}
5884
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005885#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005886/* Dummy cookie callbacks for defaults */
5887static int ssl_cookie_write_dummy( void *ctx,
5888 unsigned char **p, unsigned char *end,
5889 const unsigned char *cli_id, size_t cli_id_len )
5890{
5891 ((void) ctx);
5892 ((void) p);
5893 ((void) end);
5894 ((void) cli_id);
5895 ((void) cli_id_len);
5896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005897 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005898}
5899
5900static int ssl_cookie_check_dummy( void *ctx,
5901 const unsigned char *cookie, size_t cookie_len,
5902 const unsigned char *cli_id, size_t cli_id_len )
5903{
5904 ((void) ctx);
5905 ((void) cookie);
5906 ((void) cookie_len);
5907 ((void) cli_id);
5908 ((void) cli_id_len);
5909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005910 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005911}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005912#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005913
Paul Bakker5121ce52009-01-03 21:22:43 +00005914/*
5915 * Initialize an SSL context
5916 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005917void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
5918{
5919 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
5920}
5921
5922/*
5923 * Setup an SSL context
5924 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005925int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02005926 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00005927{
Paul Bakker48916f92012-09-16 19:57:18 +00005928 int ret;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005929 const size_t len = MBEDTLS_SSL_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005930
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005931 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00005932
5933 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005934 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00005935 */
k-stachowiakc2eddee2018-07-09 10:39:20 +02005936 ssl->in_buf = NULL;
5937 ssl->out_buf = NULL;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005938 if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL ||
5939 ( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005940 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", len ) );
k-stachowiak2c161142018-07-31 16:52:32 +02005942 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiakc2eddee2018-07-09 10:39:20 +02005943 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005944 }
5945
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005946#if defined(MBEDTLS_SSL_PROTO_DTLS)
5947 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5948 {
5949 ssl->out_hdr = ssl->out_buf;
5950 ssl->out_ctr = ssl->out_buf + 3;
5951 ssl->out_len = ssl->out_buf + 11;
5952 ssl->out_iv = ssl->out_buf + 13;
5953 ssl->out_msg = ssl->out_buf + 13;
5954
5955 ssl->in_hdr = ssl->in_buf;
5956 ssl->in_ctr = ssl->in_buf + 3;
5957 ssl->in_len = ssl->in_buf + 11;
5958 ssl->in_iv = ssl->in_buf + 13;
5959 ssl->in_msg = ssl->in_buf + 13;
5960 }
5961 else
5962#endif
5963 {
5964 ssl->out_ctr = ssl->out_buf;
5965 ssl->out_hdr = ssl->out_buf + 8;
5966 ssl->out_len = ssl->out_buf + 11;
5967 ssl->out_iv = ssl->out_buf + 13;
5968 ssl->out_msg = ssl->out_buf + 13;
5969
5970 ssl->in_ctr = ssl->in_buf;
5971 ssl->in_hdr = ssl->in_buf + 8;
5972 ssl->in_len = ssl->in_buf + 11;
5973 ssl->in_iv = ssl->in_buf + 13;
5974 ssl->in_msg = ssl->in_buf + 13;
5975 }
5976
Paul Bakker48916f92012-09-16 19:57:18 +00005977 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiakc2eddee2018-07-09 10:39:20 +02005978 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005979
5980 return( 0 );
k-stachowiakc2eddee2018-07-09 10:39:20 +02005981
5982error:
5983 mbedtls_free( ssl->in_buf );
5984 mbedtls_free( ssl->out_buf );
5985
5986 ssl->conf = NULL;
5987
5988 ssl->in_buf = NULL;
5989 ssl->out_buf = NULL;
5990
5991 ssl->in_hdr = NULL;
5992 ssl->in_ctr = NULL;
5993 ssl->in_len = NULL;
5994 ssl->in_iv = NULL;
5995 ssl->in_msg = NULL;
5996
5997 ssl->out_hdr = NULL;
5998 ssl->out_ctr = NULL;
5999 ssl->out_len = NULL;
6000 ssl->out_iv = NULL;
6001 ssl->out_msg = NULL;
6002
k-stachowiak2c161142018-07-31 16:52:32 +02006003 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006004}
6005
6006/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006007 * Reset an initialized and used SSL context for re-use while retaining
6008 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006009 *
6010 * If partial is non-zero, keep data in the input buffer and client ID.
6011 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006012 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006013static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006014{
Paul Bakker48916f92012-09-16 19:57:18 +00006015 int ret;
6016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006017 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006018
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006019 /* Cancel any possibly running timer */
6020 ssl_set_timer( ssl, 0 );
6021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022#if defined(MBEDTLS_SSL_RENEGOTIATION)
6023 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006024 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006025
6026 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6028 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006029#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006031
Paul Bakker7eb013f2011-10-06 12:37:39 +00006032 ssl->in_offt = NULL;
6033
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006034 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006035 ssl->in_msgtype = 0;
6036 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006037 if( partial == 0 )
6038 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006040 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006041 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006044 ssl_dtls_replay_reset( ssl );
6045#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006046
6047 ssl->in_hslen = 0;
6048 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006049
6050 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006051
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006052 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006053 ssl->out_msgtype = 0;
6054 ssl->out_msglen = 0;
6055 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6057 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006058 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006059#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006060
Paul Bakker48916f92012-09-16 19:57:18 +00006061 ssl->transform_in = NULL;
6062 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006063
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006064 ssl->session_in = NULL;
6065 ssl->session_out = NULL;
6066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 memset( ssl->out_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006068
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006069 if( partial == 0 )
6070 memset( ssl->in_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00006071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6073 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6076 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6079 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006080 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006081 }
6082#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006083
Paul Bakker48916f92012-09-16 19:57:18 +00006084 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006086 mbedtls_ssl_transform_free( ssl->transform );
6087 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006088 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006089 }
Paul Bakker48916f92012-09-16 19:57:18 +00006090
Paul Bakkerc0463502013-02-14 11:19:38 +01006091 if( ssl->session )
6092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093 mbedtls_ssl_session_free( ssl->session );
6094 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006095 ssl->session = NULL;
6096 }
6097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006098#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006099 ssl->alpn_chosen = NULL;
6100#endif
6101
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006102#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006103 if( partial == 0 )
6104 {
6105 mbedtls_free( ssl->cli_id );
6106 ssl->cli_id = NULL;
6107 ssl->cli_id_len = 0;
6108 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006109#endif
6110
Paul Bakker48916f92012-09-16 19:57:18 +00006111 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6112 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006113
6114 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006115}
6116
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006117/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006118 * Reset an initialized and used SSL context for re-use while retaining
6119 * all application-set variables, function pointers and data.
6120 */
6121int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6122{
6123 return( ssl_session_reset_int( ssl, 0 ) );
6124}
6125
6126/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006127 * SSL set accessors
6128 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006129void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006130{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006131 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006132}
6133
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006134void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006135{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006136 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006137}
6138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006140void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006141{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006142 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006143}
6144#endif
6145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006146#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006147void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006148{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006149 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006150}
6151#endif
6152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006153#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006154void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006155{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006156 conf->hs_timeout_min = min;
6157 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006158}
6159#endif
6160
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006161void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006162{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006163 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006164}
6165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006166#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006167void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006168 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006169 void *p_vrfy )
6170{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006171 conf->f_vrfy = f_vrfy;
6172 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006173}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006175
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006176void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006177 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006178 void *p_rng )
6179{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006180 conf->f_rng = f_rng;
6181 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006182}
6183
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006184void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006185 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006186 void *p_dbg )
6187{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006188 conf->f_dbg = f_dbg;
6189 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006190}
6191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006192void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006193 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006194 mbedtls_ssl_send_t *f_send,
6195 mbedtls_ssl_recv_t *f_recv,
6196 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006197{
6198 ssl->p_bio = p_bio;
6199 ssl->f_send = f_send;
6200 ssl->f_recv = f_recv;
6201 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006202}
6203
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006204void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006205{
6206 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006207}
6208
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006209void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6210 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006211 mbedtls_ssl_set_timer_t *f_set_timer,
6212 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006213{
6214 ssl->p_timer = p_timer;
6215 ssl->f_set_timer = f_set_timer;
6216 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006217
6218 /* Make sure we start with no timer running */
6219 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006220}
6221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006223void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006224 void *p_cache,
6225 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6226 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006227{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006228 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006229 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006230 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006231}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234#if defined(MBEDTLS_SSL_CLI_C)
6235int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006236{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006237 int ret;
6238
6239 if( ssl == NULL ||
6240 session == NULL ||
6241 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006242 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006245 }
6246
6247 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6248 return( ret );
6249
Paul Bakker0a597072012-09-25 21:55:46 +00006250 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006251
6252 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006255
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006256void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006257 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006258{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006259 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6260 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6261 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6262 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006263}
6264
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006265void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006266 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006267 int major, int minor )
6268{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006270 return;
6271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006272 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006273 return;
6274
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006275 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006276}
6277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006278#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006279void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006280 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006281{
6282 conf->cert_profile = profile;
6283}
6284
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006285/* Append a new keycert entry to a (possibly empty) list */
6286static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6287 mbedtls_x509_crt *cert,
6288 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006289{
niisatoa35dbf12018-06-25 19:05:48 +09006290 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006291
niisatoa35dbf12018-06-25 19:05:48 +09006292 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6293 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006294 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006295
niisatoa35dbf12018-06-25 19:05:48 +09006296 new_cert->cert = cert;
6297 new_cert->key = key;
6298 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006299
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006300 /* Update head is the list was null, else add to the end */
6301 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006302 {
niisatoa35dbf12018-06-25 19:05:48 +09006303 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006304 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006305 else
6306 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006307 mbedtls_ssl_key_cert *cur = *head;
6308 while( cur->next != NULL )
6309 cur = cur->next;
niisatoa35dbf12018-06-25 19:05:48 +09006310 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006311 }
6312
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006313 return( 0 );
6314}
6315
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006316int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006317 mbedtls_x509_crt *own_cert,
6318 mbedtls_pk_context *pk_key )
6319{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006320 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006321}
6322
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006323void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006324 mbedtls_x509_crt *ca_chain,
6325 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006326{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006327 conf->ca_chain = ca_chain;
6328 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006329}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006331
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006332#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6333int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6334 mbedtls_x509_crt *own_cert,
6335 mbedtls_pk_context *pk_key )
6336{
6337 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6338 own_cert, pk_key ) );
6339}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006340
6341void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6342 mbedtls_x509_crt *ca_chain,
6343 mbedtls_x509_crl *ca_crl )
6344{
6345 ssl->handshake->sni_ca_chain = ca_chain;
6346 ssl->handshake->sni_ca_crl = ca_crl;
6347}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006348
6349void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6350 int authmode )
6351{
6352 ssl->handshake->sni_authmode = authmode;
6353}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006354#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6355
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006356#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006357/*
6358 * Set EC J-PAKE password for current handshake
6359 */
6360int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6361 const unsigned char *pw,
6362 size_t pw_len )
6363{
6364 mbedtls_ecjpake_role role;
6365
Janos Follath8eb64132016-06-03 15:40:57 +01006366 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006367 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6368
6369 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6370 role = MBEDTLS_ECJPAKE_SERVER;
6371 else
6372 role = MBEDTLS_ECJPAKE_CLIENT;
6373
6374 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6375 role,
6376 MBEDTLS_MD_SHA256,
6377 MBEDTLS_ECP_DP_SECP256R1,
6378 pw, pw_len ) );
6379}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006380#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006382#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006383int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006384 const unsigned char *psk, size_t psk_len,
6385 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006386{
Paul Bakker6db455e2013-09-18 17:29:31 +02006387 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006388 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006390 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6391 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006392
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006393 /* Identity len will be encoded on two bytes */
6394 if( ( psk_identity_len >> 16 ) != 0 ||
6395 psk_identity_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
6396 {
6397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6398 }
6399
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006400 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006401 {
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006402 mbedtls_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006403
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006404 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006405 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006406 conf->psk_len = 0;
6407 }
6408 if( conf->psk_identity != NULL )
6409 {
6410 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006411 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006412 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006413 }
6414
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006415 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6416 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006417 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006418 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006419 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006420 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006421 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006422 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006423 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006424
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006425 conf->psk_len = psk_len;
6426 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006427
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006428 memcpy( conf->psk, psk, conf->psk_len );
6429 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006430
6431 return( 0 );
6432}
6433
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006434int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6435 const unsigned char *psk, size_t psk_len )
6436{
6437 if( psk == NULL || ssl->handshake == NULL )
6438 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6439
6440 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6441 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6442
6443 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006444 {
6445 mbedtls_zeroize( ssl->handshake->psk, ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006446 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006447 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006448 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006449
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006450 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006451 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006452
6453 ssl->handshake->psk_len = psk_len;
6454 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6455
6456 return( 0 );
6457}
6458
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006459void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006460 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006461 size_t),
6462 void *p_psk )
6463{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006464 conf->f_psk = f_psk;
6465 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006466}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006468
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006469#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006470
6471#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006472int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006473{
6474 int ret;
6475
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006476 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6477 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6478 {
6479 mbedtls_mpi_free( &conf->dhm_P );
6480 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006481 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006482 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006483
6484 return( 0 );
6485}
Hanno Becker470a8c42017-10-04 15:28:46 +01006486#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006487
Hanno Beckera90658f2017-10-04 15:29:08 +01006488int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6489 const unsigned char *dhm_P, size_t P_len,
6490 const unsigned char *dhm_G, size_t G_len )
6491{
6492 int ret;
6493
6494 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6495 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6496 {
6497 mbedtls_mpi_free( &conf->dhm_P );
6498 mbedtls_mpi_free( &conf->dhm_G );
6499 return( ret );
6500 }
6501
6502 return( 0 );
6503}
Paul Bakker5121ce52009-01-03 21:22:43 +00006504
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006505int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006506{
6507 int ret;
6508
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006509 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6510 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6511 {
6512 mbedtls_mpi_free( &conf->dhm_P );
6513 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006514 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006515 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006516
6517 return( 0 );
6518}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006519#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006520
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006521#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6522/*
6523 * Set the minimum length for Diffie-Hellman parameters
6524 */
6525void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6526 unsigned int bitlen )
6527{
6528 conf->dhm_min_bitlen = bitlen;
6529}
6530#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6531
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006532#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006533/*
6534 * Set allowed/preferred hashes for handshake signatures
6535 */
6536void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6537 const int *hashes )
6538{
6539 conf->sig_hashes = hashes;
6540}
Hanno Becker947194e2017-04-07 13:25:49 +01006541#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006542
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006543#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006544/*
6545 * Set the allowed elliptic curves
6546 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006547void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006548 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006549{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006550 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006551}
Hanno Becker947194e2017-04-07 13:25:49 +01006552#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006553
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006554#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006556{
Hanno Becker947194e2017-04-07 13:25:49 +01006557 /* Initialize to suppress unnecessary compiler warning */
6558 size_t hostname_len = 0;
6559
6560 /* Check if new hostname is valid before
6561 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006562 if( hostname != NULL )
6563 {
6564 hostname_len = strlen( hostname );
6565
6566 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6567 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6568 }
6569
6570 /* Now it's clear that we will overwrite the old hostname,
6571 * so we can free it safely */
6572
6573 if( ssl->hostname != NULL )
6574 {
6575 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
6576 mbedtls_free( ssl->hostname );
6577 }
6578
6579 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006580
Paul Bakker5121ce52009-01-03 21:22:43 +00006581 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006582 {
6583 ssl->hostname = NULL;
6584 }
6585 else
6586 {
6587 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006588 if( ssl->hostname == NULL )
6589 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006590
Hanno Becker947194e2017-04-07 13:25:49 +01006591 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006592
Hanno Becker947194e2017-04-07 13:25:49 +01006593 ssl->hostname[hostname_len] = '\0';
6594 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006595
6596 return( 0 );
6597}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006598#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006599
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006600#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006601void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006603 const unsigned char *, size_t),
6604 void *p_sni )
6605{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006606 conf->f_sni = f_sni;
6607 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006608}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006609#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006612int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006613{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006614 size_t cur_len, tot_len;
6615 const char **p;
6616
6617 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006618 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6619 * MUST NOT be truncated."
6620 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006621 */
6622 tot_len = 0;
6623 for( p = protos; *p != NULL; p++ )
6624 {
6625 cur_len = strlen( *p );
6626 tot_len += cur_len;
6627
Ronald Crona32236c2020-04-23 16:41:44 +02006628 if( ( cur_len == 0 ) ||
6629 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
6630 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006632 }
6633
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006634 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006635
6636 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006637}
6638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006640{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006641 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006642}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006644
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006645void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006646{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006647 conf->max_major_ver = major;
6648 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006649}
6650
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006651void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006652{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006653 conf->min_major_ver = major;
6654 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006655}
6656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006658void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006659{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006660 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006661}
6662#endif
6663
Janos Follath088ce432017-04-10 12:42:31 +01006664#if defined(MBEDTLS_SSL_SRV_C)
6665void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6666 char cert_req_ca_list )
6667{
6668 conf->cert_req_ca_list = cert_req_ca_list;
6669}
6670#endif
6671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006673void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006674{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006675 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006676}
6677#endif
6678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006680void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006681{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006682 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006683}
6684#endif
6685
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006686#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006687void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006688{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006689 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006690}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006691#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006693#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006694int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006695{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
6697 mfl_code_to_length[mfl_code] > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006700 }
6701
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006702 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006703
6704 return( 0 );
6705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006708#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006709void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006710{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006711 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006712}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006716void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006717{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006718 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006719}
6720#endif
6721
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006722void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006723{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006724 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006725}
6726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006728void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006729{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006730 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006731}
6732
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006733void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006734{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006735 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006736}
6737
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006738void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006739 const unsigned char period[8] )
6740{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006741 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006742}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006746#if defined(MBEDTLS_SSL_CLI_C)
6747void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006748{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006749 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006750}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006751#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006752
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006753#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006754void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6755 mbedtls_ssl_ticket_write_t *f_ticket_write,
6756 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6757 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006758{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006759 conf->f_ticket_write = f_ticket_write;
6760 conf->f_ticket_parse = f_ticket_parse;
6761 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006762}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006763#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006765
Robert Cragie4feb7ae2015-10-02 13:33:37 +01006766#if defined(MBEDTLS_SSL_EXPORT_KEYS)
6767void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
6768 mbedtls_ssl_export_keys_t *f_export_keys,
6769 void *p_export_keys )
6770{
6771 conf->f_export_keys = f_export_keys;
6772 conf->p_export_keys = p_export_keys;
6773}
6774#endif
6775
Paul Bakker5121ce52009-01-03 21:22:43 +00006776/*
6777 * SSL get accessors
6778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006780{
6781 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6782}
6783
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006784uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006785{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006786 if( ssl->session != NULL )
6787 return( ssl->session->verify_result );
6788
6789 if( ssl->session_negotiate != NULL )
6790 return( ssl->session_negotiate->verify_result );
6791
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006792 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006793}
6794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006796{
Paul Bakker926c8e42013-03-06 10:23:34 +01006797 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006798 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006801}
6802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006804{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006806 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006807 {
6808 switch( ssl->minor_ver )
6809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006811 return( "DTLSv1.0" );
6812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006814 return( "DTLSv1.2" );
6815
6816 default:
6817 return( "unknown (DTLS)" );
6818 }
6819 }
6820#endif
6821
Paul Bakker43ca69c2011-01-15 17:35:19 +00006822 switch( ssl->minor_ver )
6823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006824 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006825 return( "SSLv3.0" );
6826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006828 return( "TLSv1.0" );
6829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006831 return( "TLSv1.1" );
6832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006833 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00006834 return( "TLSv1.2" );
6835
Paul Bakker43ca69c2011-01-15 17:35:19 +00006836 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006837 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00006838 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00006839}
6840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006841int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006842{
Hanno Becker12f7ede2018-08-17 15:28:19 +01006843 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006844 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006845 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006846
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006847 if( transform == NULL )
6848 return( (int) mbedtls_ssl_hdr_len( ssl ) );
6849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850#if defined(MBEDTLS_ZLIB_SUPPORT)
6851 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
6852 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006853#endif
6854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857 case MBEDTLS_MODE_GCM:
6858 case MBEDTLS_MODE_CCM:
6859 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006860 transform_expansion = transform->minlen;
6861 break;
6862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863 case MBEDTLS_MODE_CBC:
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006864
6865 block_size = mbedtls_cipher_get_block_size(
6866 &transform->cipher_ctx_enc );
6867
Hanno Becker12f7ede2018-08-17 15:28:19 +01006868 /* Expansion due to the addition of the MAC. */
6869 transform_expansion += transform->maclen;
6870
6871 /* Expansion due to the addition of CBC padding;
6872 * Theoretically up to 256 bytes, but we never use
6873 * more than the block size of the underlying cipher. */
6874 transform_expansion += block_size;
6875
6876 /* For TLS 1.1 or higher, an explicit IV is added
6877 * after the record header. */
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006878#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
6879 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker12f7ede2018-08-17 15:28:19 +01006880 transform_expansion += block_size;
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006881#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker12f7ede2018-08-17 15:28:19 +01006882
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006883 break;
6884
6885 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006887 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006888 }
6889
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006890 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006891}
6892
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02006893#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
6894size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
6895{
6896 size_t max_len;
6897
6898 /*
6899 * Assume mfl_code is correct since it was checked when set
6900 */
6901 max_len = mfl_code_to_length[ssl->conf->mfl_code];
6902
6903 /*
6904 * Check if a smaller max length was negotiated
6905 */
6906 if( ssl->session_out != NULL &&
6907 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6908 {
6909 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6910 }
6911
6912 return max_len;
6913}
6914#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
6915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916#if defined(MBEDTLS_X509_CRT_PARSE_C)
6917const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00006918{
6919 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006920 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006921
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006922 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006923}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00006925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006926#if defined(MBEDTLS_SSL_CLI_C)
6927int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006928{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006929 if( ssl == NULL ||
6930 dst == NULL ||
6931 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006932 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006934 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006935 }
6936
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006937 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006938}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006940
Paul Bakker5121ce52009-01-03 21:22:43 +00006941/*
Paul Bakker1961b702013-01-25 14:49:24 +01006942 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00006943 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006944int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006945{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006947
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006948 if( ssl == NULL || ssl->conf == NULL )
6949 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006952 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006954#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006956 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006957 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006958#endif
6959
Paul Bakker1961b702013-01-25 14:49:24 +01006960 return( ret );
6961}
6962
6963/*
6964 * Perform the SSL handshake
6965 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01006967{
6968 int ret = 0;
6969
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006970 if( ssl == NULL || ssl->conf == NULL )
6971 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01006974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01006976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01006978
6979 if( ret != 0 )
6980 break;
6981 }
6982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006984
6985 return( ret );
6986}
6987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988#if defined(MBEDTLS_SSL_RENEGOTIATION)
6989#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006990/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006991 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00006992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006994{
6995 int ret;
6996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006998
6999 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7001 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007006 return( ret );
7007 }
7008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007010
7011 return( 0 );
7012}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007014
7015/*
7016 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017 * - any side: calling mbedtls_ssl_renegotiate(),
7018 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7019 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007020 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007021 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007025{
7026 int ret;
7027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007029
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007030 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7031 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007032
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007033 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7034 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007036 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007038 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007039 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007040 ssl->handshake->out_msg_seq = 1;
7041 else
7042 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007043 }
7044#endif
7045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007046 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7047 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007052 return( ret );
7053 }
7054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007056
7057 return( 0 );
7058}
7059
7060/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007061 * Renegotiate current connection on client,
7062 * or request renegotiation on server
7063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007065{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007067
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007068 if( ssl == NULL || ssl->conf == NULL )
7069 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007072 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007073 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7076 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007079
7080 /* Did we already try/start sending HelloRequest? */
7081 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007083
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007084 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007085 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007089 /*
7090 * On client, either start the renegotiation process or,
7091 * if already in progress, continue the handshake
7092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7096 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007097
7098 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007101 return( ret );
7102 }
7103 }
7104 else
7105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007109 return( ret );
7110 }
7111 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007113
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007114 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007115}
7116
7117/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007118 * Check record counters and renegotiate if they're above the limit.
7119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007121{
Andres AG2196c7f2016-12-15 17:01:16 +00007122 size_t ep_len = ssl_ep_len( ssl );
7123 int in_ctr_cmp;
7124 int out_ctr_cmp;
7125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7127 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007128 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007129 {
7130 return( 0 );
7131 }
7132
Andres AG2196c7f2016-12-15 17:01:16 +00007133 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7134 ssl->conf->renego_period + ep_len, 8 - ep_len );
7135 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
7136 ssl->conf->renego_period + ep_len, 8 - ep_len );
7137
7138 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007139 {
7140 return( 0 );
7141 }
7142
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007147
7148/*
7149 * Receive application data decrypted from the SSL layer
7150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007152{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007153 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007154 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007155
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007156 if( ssl == NULL || ssl->conf == NULL )
7157 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007162 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007165 return( ret );
7166
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007167 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007171 return( ret );
7172 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007173 }
7174#endif
7175
Hanno Becker4a810fb2017-05-24 16:27:30 +01007176 /*
7177 * Check if renegotiation is necessary and/or handshake is
7178 * in process. If yes, perform/continue, and fall through
7179 * if an unexpected packet is received while the client
7180 * is waiting for the ServerHello.
7181 *
7182 * (There is no equivalent to the last condition on
7183 * the server-side as it is not treated as within
7184 * a handshake while waiting for the ClientHello
7185 * after a renegotiation request.)
7186 */
7187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007189 ret = ssl_check_ctr_renegotiate( ssl );
7190 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7191 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007194 return( ret );
7195 }
7196#endif
7197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007201 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7202 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007205 return( ret );
7206 }
7207 }
7208
7209 if( ssl->in_offt == NULL )
7210 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007211 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007212 if( ssl->f_get_timer != NULL &&
7213 ssl->f_get_timer( ssl->p_timer ) == -1 )
7214 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007215 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007216 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007217
Hanno Becker4a810fb2017-05-24 16:27:30 +01007218 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007219 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007220 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7221 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007222
Hanno Becker4a810fb2017-05-24 16:27:30 +01007223 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7224 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007225 }
7226
7227 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007229 {
7230 /*
7231 * OpenSSL sends empty messages to randomize the IV
7232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007233 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007236 return( 0 );
7237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007239 return( ret );
7240 }
7241 }
7242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007246
Hanno Becker4a810fb2017-05-24 16:27:30 +01007247 /*
7248 * - For client-side, expect SERVER_HELLO_REQUEST.
7249 * - For server-side, expect CLIENT_HELLO.
7250 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7251 */
7252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007254 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007256 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007259
7260 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007262 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007263 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007264#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007266 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007267#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007268
Hanno Becker4a810fb2017-05-24 16:27:30 +01007269#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007270 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007274
7275 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007277 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007278 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007279#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007281 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007282#endif /* MBEDTLS_SSL_SRV_C */
7283
Hanno Becker21df7f92017-10-17 11:03:26 +01007284#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007285 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007286 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7287 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7288 ssl->conf->allow_legacy_renegotiation ==
7289 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7290 {
7291 /*
7292 * Accept renegotiation request
7293 */
Paul Bakker48916f92012-09-16 19:57:18 +00007294
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007295 /* DTLS clients need to know renego is server-initiated */
7296#if defined(MBEDTLS_SSL_PROTO_DTLS)
7297 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7298 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7299 {
7300 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7301 }
7302#endif
7303 ret = ssl_start_renegotiation( ssl );
7304 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7305 ret != 0 )
7306 {
7307 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7308 return( ret );
7309 }
7310 }
7311 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007312#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007313 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007314 /*
7315 * Refuse renegotiation
7316 */
7317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320#if defined(MBEDTLS_SSL_PROTO_SSL3)
7321 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007322 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007323 /* SSLv3 does not have a "no_renegotiation" warning, so
7324 we send a fatal alert and abort the connection. */
7325 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7326 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7327 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007328 }
7329 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7331#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7332 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7333 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007335 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7336 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7337 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007338 {
7339 return( ret );
7340 }
Paul Bakker48916f92012-09-16 19:57:18 +00007341 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007342 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7344 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7347 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007348 }
Paul Bakker48916f92012-09-16 19:57:18 +00007349 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007350
Hanno Becker4a810fb2017-05-24 16:27:30 +01007351 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00007352 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007353#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007355 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007356 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007357 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007358 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007361 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007363 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007364 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007365 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7369 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007372 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007373 }
7374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7378 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007379 }
7380
7381 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007382
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007383 /* We're going to return something now, cancel timer,
7384 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007386 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007387
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007388#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007389 /* If we requested renego but received AppData, resend HelloRequest.
7390 * Do it now, after setting in_offt, to avoid taking this branch
7391 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007393 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007394 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007395 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007396 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007399 return( ret );
7400 }
7401 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007403#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007404 }
7405
7406 n = ( len < ssl->in_msglen )
7407 ? len : ssl->in_msglen;
7408
7409 memcpy( buf, ssl->in_offt, n );
7410 ssl->in_msglen -= n;
7411
gabor-mezei-armef738752020-07-15 10:55:00 +02007412 /* Zeroising the plaintext buffer to erase unused application data
7413 from the memory. */
7414 mbedtls_zeroize( ssl->in_offt, n );
7415
Paul Bakker5121ce52009-01-03 21:22:43 +00007416 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007417 {
7418 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007419 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007420 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007422 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007423 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007424 /* more data available */
7425 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007426 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007429
Paul Bakker23986e52011-04-24 08:57:21 +00007430 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007431}
7432
7433/*
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007434 * Send application data to be encrypted by the SSL layer, taking care of max
7435 * fragment length and buffer size.
7436 *
7437 * According to RFC 5246 Section 6.2.1:
7438 *
7439 * Zero-length fragments of Application data MAY be sent as they are
7440 * potentially useful as a traffic analysis countermeasure.
7441 *
7442 * Therefore, it is possible that the input message length is 0 and the
7443 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007444 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007445static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007446 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007447{
Paul Bakker23986e52011-04-24 08:57:21 +00007448 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007450 size_t max_len = mbedtls_ssl_get_max_frag_len( ssl );
Florin0b7b83f2017-07-22 09:01:44 +02007451#else
7452 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
7453#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007454 if( len > max_len )
7455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007456#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007457 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007460 "maximum fragment length: %d > %d",
7461 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007463 }
7464 else
7465#endif
7466 len = max_len;
7467 }
Paul Bakker887bd502011-06-08 13:10:54 +00007468
Paul Bakker5121ce52009-01-03 21:22:43 +00007469 if( ssl->out_left != 0 )
7470 {
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007471 /*
7472 * The user has previously tried to send the data and
7473 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7474 * written. In this case, we expect the high-level write function
7475 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007480 return( ret );
7481 }
7482 }
Paul Bakker887bd502011-06-08 13:10:54 +00007483 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007484 {
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007485 /*
7486 * The user is trying to send a message the first time, so we need to
7487 * copy the data into the internal buffers and setup the data structure
7488 * to keep track of partial writes
7489 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007490 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007492 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007494 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007497 return( ret );
7498 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007499 }
7500
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007501 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007502}
7503
7504/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007505 * Write application data, doing 1/n-1 splitting if necessary.
7506 *
7507 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007508 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01007509 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007512static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007513 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007514{
7515 int ret;
7516
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007517 if( ssl->conf->cbc_record_splitting ==
7518 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007519 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7521 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7522 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007523 {
7524 return( ssl_write_real( ssl, buf, len ) );
7525 }
7526
7527 if( ssl->split_done == 0 )
7528 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007529 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007530 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007531 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007532 }
7533
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007534 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7535 return( ret );
7536 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007537
7538 return( ret + 1 );
7539}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007541
7542/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007543 * Write application data (public-facing wrapper)
7544 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007545int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007546{
7547 int ret;
7548
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007550
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007551 if( ssl == NULL || ssl->conf == NULL )
7552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7553
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007554#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007555 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7556 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007557 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007558 return( ret );
7559 }
7560#endif
7561
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007562 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007563 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007564 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007565 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007566 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007567 return( ret );
7568 }
7569 }
7570
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007571#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007572 ret = ssl_write_split( ssl, buf, len );
7573#else
7574 ret = ssl_write_real( ssl, buf, len );
7575#endif
7576
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007578
7579 return( ret );
7580}
7581
7582/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007583 * Notify the peer that the connection is being closed
7584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007585int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007586{
7587 int ret;
7588
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007589 if( ssl == NULL || ssl->conf == NULL )
7590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007593
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007594 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007595 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007597 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7600 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7601 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007604 return( ret );
7605 }
7606 }
7607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007609
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007610 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007611}
7612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00007614{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007615 if( transform == NULL )
7616 return;
7617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00007619 deflateEnd( &transform->ctx_deflate );
7620 inflateEnd( &transform->ctx_inflate );
7621#endif
7622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623 mbedtls_cipher_free( &transform->cipher_ctx_enc );
7624 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02007625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626 mbedtls_md_free( &transform->md_ctx_enc );
7627 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02007628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629 mbedtls_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007630}
7631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632#if defined(MBEDTLS_X509_CRT_PARSE_C)
7633static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007636
7637 while( cur != NULL )
7638 {
7639 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007641 cur = next;
7642 }
7643}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake )
Paul Bakker48916f92012-09-16 19:57:18 +00007647{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007648 if( handshake == NULL )
7649 return;
7650
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007651#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7652 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7653 mbedtls_md5_free( &handshake->fin_md5 );
7654 mbedtls_sha1_free( &handshake->fin_sha1 );
7655#endif
7656#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7657#if defined(MBEDTLS_SHA256_C)
7658 mbedtls_sha256_free( &handshake->fin_sha256 );
7659#endif
7660#if defined(MBEDTLS_SHA512_C)
7661 mbedtls_sha512_free( &handshake->fin_sha512 );
7662#endif
7663#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665#if defined(MBEDTLS_DHM_C)
7666 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007667#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668#if defined(MBEDTLS_ECDH_C)
7669 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007670#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007671#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007672 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007673#if defined(MBEDTLS_SSL_CLI_C)
7674 mbedtls_free( handshake->ecjpake_cache );
7675 handshake->ecjpake_cache = NULL;
7676 handshake->ecjpake_cache_len = 0;
7677#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007678#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02007679
Janos Follath4ae5c292016-02-10 11:27:43 +00007680#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
7681 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02007682 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007683 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007684#endif
7685
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007686#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7687 if( handshake->psk != NULL )
7688 {
7689 mbedtls_zeroize( handshake->psk, handshake->psk_len );
7690 mbedtls_free( handshake->psk );
7691 }
7692#endif
7693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7695 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007696 /*
7697 * Free only the linked list wrapper, not the keys themselves
7698 * since the belong to the SNI callback
7699 */
7700 if( handshake->sni_key_cert != NULL )
7701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007703
7704 while( cur != NULL )
7705 {
7706 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007708 cur = next;
7709 }
7710 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713#if defined(MBEDTLS_SSL_PROTO_DTLS)
7714 mbedtls_free( handshake->verify_cookie );
7715 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007716 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007717#endif
7718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719 mbedtls_zeroize( handshake, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007720}
7721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007723{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007724 if( session == NULL )
7725 return;
7726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007728 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007730 mbedtls_x509_crt_free( session->peer_cert );
7731 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007732 }
Paul Bakkered27a042013-04-18 22:46:23 +02007733#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007734
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007735#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007737#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739 mbedtls_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007740}
7741
Paul Bakker5121ce52009-01-03 21:22:43 +00007742/*
7743 * Free an SSL context
7744 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007746{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007747 if( ssl == NULL )
7748 return;
7749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007751
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007752 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754 mbedtls_zeroize( ssl->out_buf, MBEDTLS_SSL_BUFFER_LEN );
7755 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007756 }
7757
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007758 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760 mbedtls_zeroize( ssl->in_buf, MBEDTLS_SSL_BUFFER_LEN );
7761 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007762 }
7763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02007765 if( ssl->compress_buf != NULL )
7766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767 mbedtls_zeroize( ssl->compress_buf, MBEDTLS_SSL_BUFFER_LEN );
7768 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02007769 }
7770#endif
7771
Paul Bakker48916f92012-09-16 19:57:18 +00007772 if( ssl->transform )
7773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774 mbedtls_ssl_transform_free( ssl->transform );
7775 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007776 }
7777
7778 if( ssl->handshake )
7779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007780 mbedtls_ssl_handshake_free( ssl->handshake );
7781 mbedtls_ssl_transform_free( ssl->transform_negotiate );
7782 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784 mbedtls_free( ssl->handshake );
7785 mbedtls_free( ssl->transform_negotiate );
7786 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007787 }
7788
Paul Bakkerc0463502013-02-14 11:19:38 +01007789 if( ssl->session )
7790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007791 mbedtls_ssl_session_free( ssl->session );
7792 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007793 }
7794
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02007795#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02007796 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007797 {
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007798 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00007800 }
Paul Bakker0be444a2013-08-27 21:55:01 +02007801#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7804 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
7807 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00007808 }
7809#endif
7810
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007811#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007813#endif
7814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00007816
Paul Bakker86f04f42013-02-14 11:20:09 +01007817 /* Actually clear after last debug message */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818 mbedtls_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007819}
7820
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007821/*
7822 * Initialze mbedtls_ssl_config
7823 */
7824void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
7825{
7826 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
7827}
7828
Simon Butcherc97b6972015-12-27 23:48:17 +00007829#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007830static int ssl_preset_default_hashes[] = {
7831#if defined(MBEDTLS_SHA512_C)
7832 MBEDTLS_MD_SHA512,
7833 MBEDTLS_MD_SHA384,
7834#endif
7835#if defined(MBEDTLS_SHA256_C)
7836 MBEDTLS_MD_SHA256,
7837 MBEDTLS_MD_SHA224,
7838#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02007839#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007840 MBEDTLS_MD_SHA1,
7841#endif
7842 MBEDTLS_MD_NONE
7843};
Simon Butcherc97b6972015-12-27 23:48:17 +00007844#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007845
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007846static int ssl_preset_suiteb_ciphersuites[] = {
7847 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
7848 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
7849 0
7850};
7851
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007852#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007853static int ssl_preset_suiteb_hashes[] = {
7854 MBEDTLS_MD_SHA256,
7855 MBEDTLS_MD_SHA384,
7856 MBEDTLS_MD_NONE
7857};
7858#endif
7859
7860#if defined(MBEDTLS_ECP_C)
7861static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007862#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007863 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007864#endif
7865#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007866 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007867#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007868 MBEDTLS_ECP_DP_NONE
7869};
7870#endif
7871
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007872/*
Tillmann Karras588ad502015-09-25 04:27:22 +02007873 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007874 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007875int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007876 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007877{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007878#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007879 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007880#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007881
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02007882 /* Use the functions here so that they are covered in tests,
7883 * but otherwise access member directly for efficiency */
7884 mbedtls_ssl_conf_endpoint( conf, endpoint );
7885 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007886
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007887 /*
7888 * Things that are common to all presets
7889 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007890#if defined(MBEDTLS_SSL_CLI_C)
7891 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7892 {
7893 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
7894#if defined(MBEDTLS_SSL_SESSION_TICKETS)
7895 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
7896#endif
7897 }
7898#endif
7899
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007900#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007901 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007902#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007903
7904#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7905 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
7906#endif
7907
7908#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7909 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
7910#endif
7911
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007912#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7913 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7914#endif
7915
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007916#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007917 conf->f_cookie_write = ssl_cookie_write_dummy;
7918 conf->f_cookie_check = ssl_cookie_check_dummy;
7919#endif
7920
7921#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7922 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7923#endif
7924
Janos Follath088ce432017-04-10 12:42:31 +01007925#if defined(MBEDTLS_SSL_SRV_C)
7926 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
7927#endif
7928
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007929#if defined(MBEDTLS_SSL_PROTO_DTLS)
7930 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7931 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7932#endif
7933
7934#if defined(MBEDTLS_SSL_RENEGOTIATION)
7935 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00007936 memset( conf->renego_period, 0x00, 2 );
7937 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007938#endif
7939
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007940#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
7941 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7942 {
Hanno Becker00d0a682017-10-04 13:14:29 +01007943 const unsigned char dhm_p[] =
7944 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
7945 const unsigned char dhm_g[] =
7946 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
7947
Hanno Beckera90658f2017-10-04 15:29:08 +01007948 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
7949 dhm_p, sizeof( dhm_p ),
7950 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007951 {
7952 return( ret );
7953 }
7954 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007955#endif
7956
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007957 /*
7958 * Preset-specific defaults
7959 */
7960 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007961 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007962 /*
7963 * NSA Suite B
7964 */
7965 case MBEDTLS_SSL_PRESET_SUITEB:
7966 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7967 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7968 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7969 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7970
7971 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7972 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7973 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7974 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7975 ssl_preset_suiteb_ciphersuites;
7976
7977#if defined(MBEDTLS_X509_CRT_PARSE_C)
7978 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007979#endif
7980
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007981#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007982 conf->sig_hashes = ssl_preset_suiteb_hashes;
7983#endif
7984
7985#if defined(MBEDTLS_ECP_C)
7986 conf->curve_list = ssl_preset_suiteb_curves;
7987#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007988 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007989
7990 /*
7991 * Default
7992 */
7993 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03007994 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
7995 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
7996 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7997 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7998 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
7999 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8000 MBEDTLS_SSL_MIN_MINOR_VERSION :
8001 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008002 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8003 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8004
8005#if defined(MBEDTLS_SSL_PROTO_DTLS)
8006 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8007 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8008#endif
8009
8010 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8011 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8012 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8013 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8014 mbedtls_ssl_list_ciphersuites();
8015
8016#if defined(MBEDTLS_X509_CRT_PARSE_C)
8017 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8018#endif
8019
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008020#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008021 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008022#endif
8023
8024#if defined(MBEDTLS_ECP_C)
8025 conf->curve_list = mbedtls_ecp_grp_id_list();
8026#endif
8027
8028#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8029 conf->dhm_min_bitlen = 1024;
8030#endif
8031 }
8032
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008033 return( 0 );
8034}
8035
8036/*
8037 * Free mbedtls_ssl_config
8038 */
8039void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8040{
8041#if defined(MBEDTLS_DHM_C)
8042 mbedtls_mpi_free( &conf->dhm_P );
8043 mbedtls_mpi_free( &conf->dhm_G );
8044#endif
8045
8046#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8047 if( conf->psk != NULL )
8048 {
8049 mbedtls_zeroize( conf->psk, conf->psk_len );
8050 mbedtls_zeroize( conf->psk_identity, conf->psk_identity_len );
8051 mbedtls_free( conf->psk );
8052 mbedtls_free( conf->psk_identity );
8053 conf->psk_len = 0;
8054 conf->psk_identity_len = 0;
8055 }
8056#endif
8057
8058#if defined(MBEDTLS_X509_CRT_PARSE_C)
8059 ssl_key_cert_free( conf->key_cert );
8060#endif
8061
8062 mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
8063}
8064
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008065#if defined(MBEDTLS_PK_C) && \
8066 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008067/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008068 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008070unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008072#if defined(MBEDTLS_RSA_C)
8073 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8074 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008075#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076#if defined(MBEDTLS_ECDSA_C)
8077 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8078 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008079#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008081}
8082
Hanno Becker7e5437a2017-04-28 17:15:26 +01008083unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8084{
8085 switch( type ) {
8086 case MBEDTLS_PK_RSA:
8087 return( MBEDTLS_SSL_SIG_RSA );
8088 case MBEDTLS_PK_ECDSA:
8089 case MBEDTLS_PK_ECKEY:
8090 return( MBEDTLS_SSL_SIG_ECDSA );
8091 default:
8092 return( MBEDTLS_SSL_SIG_ANON );
8093 }
8094}
8095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008097{
8098 switch( sig )
8099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100#if defined(MBEDTLS_RSA_C)
8101 case MBEDTLS_SSL_SIG_RSA:
8102 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008103#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008104#if defined(MBEDTLS_ECDSA_C)
8105 case MBEDTLS_SSL_SIG_ECDSA:
8106 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008107#endif
8108 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008109 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008110 }
8111}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008112#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008113
Hanno Becker7e5437a2017-04-28 17:15:26 +01008114#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8115 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8116
8117/* Find an entry in a signature-hash set matching a given hash algorithm. */
8118mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8119 mbedtls_pk_type_t sig_alg )
8120{
8121 switch( sig_alg )
8122 {
8123 case MBEDTLS_PK_RSA:
8124 return( set->rsa );
8125 case MBEDTLS_PK_ECDSA:
8126 return( set->ecdsa );
8127 default:
8128 return( MBEDTLS_MD_NONE );
8129 }
8130}
8131
8132/* Add a signature-hash-pair to a signature-hash set */
8133void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8134 mbedtls_pk_type_t sig_alg,
8135 mbedtls_md_type_t md_alg )
8136{
8137 switch( sig_alg )
8138 {
8139 case MBEDTLS_PK_RSA:
8140 if( set->rsa == MBEDTLS_MD_NONE )
8141 set->rsa = md_alg;
8142 break;
8143
8144 case MBEDTLS_PK_ECDSA:
8145 if( set->ecdsa == MBEDTLS_MD_NONE )
8146 set->ecdsa = md_alg;
8147 break;
8148
8149 default:
8150 break;
8151 }
8152}
8153
8154/* Allow exactly one hash algorithm for each signature. */
8155void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8156 mbedtls_md_type_t md_alg )
8157{
8158 set->rsa = md_alg;
8159 set->ecdsa = md_alg;
8160}
8161
8162#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8163 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8164
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008165/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008166 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008168mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008169{
8170 switch( hash )
8171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172#if defined(MBEDTLS_MD5_C)
8173 case MBEDTLS_SSL_HASH_MD5:
8174 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176#if defined(MBEDTLS_SHA1_C)
8177 case MBEDTLS_SSL_HASH_SHA1:
8178 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008179#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180#if defined(MBEDTLS_SHA256_C)
8181 case MBEDTLS_SSL_HASH_SHA224:
8182 return( MBEDTLS_MD_SHA224 );
8183 case MBEDTLS_SSL_HASH_SHA256:
8184 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008185#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186#if defined(MBEDTLS_SHA512_C)
8187 case MBEDTLS_SSL_HASH_SHA384:
8188 return( MBEDTLS_MD_SHA384 );
8189 case MBEDTLS_SSL_HASH_SHA512:
8190 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008191#endif
8192 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008193 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008194 }
8195}
8196
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008197/*
8198 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8199 */
8200unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8201{
8202 switch( md )
8203 {
8204#if defined(MBEDTLS_MD5_C)
8205 case MBEDTLS_MD_MD5:
8206 return( MBEDTLS_SSL_HASH_MD5 );
8207#endif
8208#if defined(MBEDTLS_SHA1_C)
8209 case MBEDTLS_MD_SHA1:
8210 return( MBEDTLS_SSL_HASH_SHA1 );
8211#endif
8212#if defined(MBEDTLS_SHA256_C)
8213 case MBEDTLS_MD_SHA224:
8214 return( MBEDTLS_SSL_HASH_SHA224 );
8215 case MBEDTLS_MD_SHA256:
8216 return( MBEDTLS_SSL_HASH_SHA256 );
8217#endif
8218#if defined(MBEDTLS_SHA512_C)
8219 case MBEDTLS_MD_SHA384:
8220 return( MBEDTLS_SSL_HASH_SHA384 );
8221 case MBEDTLS_MD_SHA512:
8222 return( MBEDTLS_SSL_HASH_SHA512 );
8223#endif
8224 default:
8225 return( MBEDTLS_SSL_HASH_NONE );
8226 }
8227}
8228
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008229#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008230/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008231 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008232 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008233 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008234int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008237
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008238 if( ssl->conf->curve_list == NULL )
8239 return( -1 );
8240
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008241 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008242 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008243 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008244
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008245 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008246}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008247#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008248
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008249#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008250/*
8251 * Check if a hash proposed by the peer is in our list.
8252 * Return 0 if we're willing to use it, -1 otherwise.
8253 */
8254int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8255 mbedtls_md_type_t md )
8256{
8257 const int *cur;
8258
8259 if( ssl->conf->sig_hashes == NULL )
8260 return( -1 );
8261
8262 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8263 if( *cur == (int) md )
8264 return( 0 );
8265
8266 return( -1 );
8267}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008268#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008270#if defined(MBEDTLS_X509_CRT_PARSE_C)
8271int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8272 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008273 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008274 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008275{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008276 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008277#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008278 int usage = 0;
8279#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008281 const char *ext_oid;
8282 size_t ext_len;
8283#endif
8284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8286 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008287 ((void) cert);
8288 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008289 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008290#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8293 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008294 {
8295 /* Server part of the key exchange */
8296 switch( ciphersuite->key_exchange )
8297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008298 case MBEDTLS_KEY_EXCHANGE_RSA:
8299 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008300 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008301 break;
8302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8304 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8305 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8306 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008307 break;
8308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008309 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8310 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008311 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008312 break;
8313
8314 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008315 case MBEDTLS_KEY_EXCHANGE_NONE:
8316 case MBEDTLS_KEY_EXCHANGE_PSK:
8317 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8318 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008319 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008320 usage = 0;
8321 }
8322 }
8323 else
8324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008325 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8326 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008327 }
8328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008329 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008330 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008331 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008332 ret = -1;
8333 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008334#else
8335 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008338#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8339 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8342 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008343 }
8344 else
8345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8347 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008348 }
8349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008351 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008352 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008353 ret = -1;
8354 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008356
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008357 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008358}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008360
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008361/*
8362 * Convert version numbers to/from wire format
8363 * and, for DTLS, to/from TLS equivalent.
8364 *
8365 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008366 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008367 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8368 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008371 unsigned char ver[2] )
8372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373#if defined(MBEDTLS_SSL_PROTO_DTLS)
8374 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008377 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8378
8379 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8380 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8381 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008382 else
8383#else
8384 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008385#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008386 {
8387 ver[0] = (unsigned char) major;
8388 ver[1] = (unsigned char) minor;
8389 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008390}
8391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008392void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008393 const unsigned char ver[2] )
8394{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008395#if defined(MBEDTLS_SSL_PROTO_DTLS)
8396 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008397 {
8398 *major = 255 - ver[0] + 2;
8399 *minor = 255 - ver[1] + 1;
8400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008401 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008402 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8403 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008404 else
8405#else
8406 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008407#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008408 {
8409 *major = ver[0];
8410 *minor = ver[1];
8411 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008412}
8413
Simon Butcher99000142016-10-13 17:21:01 +01008414int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8415{
8416#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8417 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8418 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8419
8420 switch( md )
8421 {
8422#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8423#if defined(MBEDTLS_MD5_C)
8424 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008425 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008426#endif
8427#if defined(MBEDTLS_SHA1_C)
8428 case MBEDTLS_SSL_HASH_SHA1:
8429 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8430 break;
8431#endif
8432#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8433#if defined(MBEDTLS_SHA512_C)
8434 case MBEDTLS_SSL_HASH_SHA384:
8435 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8436 break;
8437#endif
8438#if defined(MBEDTLS_SHA256_C)
8439 case MBEDTLS_SSL_HASH_SHA256:
8440 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8441 break;
8442#endif
8443 default:
8444 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8445 }
8446
8447 return 0;
8448#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8449 (void) ssl;
8450 (void) md;
8451
8452 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8453#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8454}
8455
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008456#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8457 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8458int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8459 unsigned char *output,
8460 unsigned char *data, size_t data_len )
8461{
8462 int ret = 0;
8463 mbedtls_md5_context mbedtls_md5;
8464 mbedtls_sha1_context mbedtls_sha1;
8465
8466 mbedtls_md5_init( &mbedtls_md5 );
8467 mbedtls_sha1_init( &mbedtls_sha1 );
8468
8469 /*
8470 * digitally-signed struct {
8471 * opaque md5_hash[16];
8472 * opaque sha_hash[20];
8473 * };
8474 *
8475 * md5_hash
8476 * MD5(ClientHello.random + ServerHello.random
8477 * + ServerParams);
8478 * sha_hash
8479 * SHA(ClientHello.random + ServerHello.random
8480 * + ServerParams);
8481 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008482 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008483 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008485 goto exit;
8486 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008487 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008488 ssl->handshake->randbytes, 64 ) ) != 0 )
8489 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008490 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008491 goto exit;
8492 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008493 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008494 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008495 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008496 goto exit;
8497 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008498 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008499 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008500 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008501 goto exit;
8502 }
8503
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008504 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008505 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008507 goto exit;
8508 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008509 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008510 ssl->handshake->randbytes, 64 ) ) != 0 )
8511 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008512 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008513 goto exit;
8514 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008515 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008516 data_len ) ) != 0 )
8517 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008518 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008519 goto exit;
8520 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008521 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008522 output + 16 ) ) != 0 )
8523 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008525 goto exit;
8526 }
8527
8528exit:
8529 mbedtls_md5_free( &mbedtls_md5 );
8530 mbedtls_sha1_free( &mbedtls_sha1 );
8531
8532 if( ret != 0 )
8533 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8534 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8535
8536 return( ret );
8537
8538}
8539#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
8540 MBEDTLS_SSL_PROTO_TLS1_1 */
8541
8542#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8543 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8544int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8545 unsigned char *output,
8546 unsigned char *data, size_t data_len,
8547 mbedtls_md_type_t md_alg )
8548{
8549 int ret = 0;
8550 mbedtls_md_context_t ctx;
8551 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8552
8553 mbedtls_md_init( &ctx );
8554
8555 /*
8556 * digitally-signed struct {
8557 * opaque client_random[32];
8558 * opaque server_random[32];
8559 * ServerDHParams params;
8560 * };
8561 */
8562 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8563 {
8564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8565 goto exit;
8566 }
8567 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8568 {
8569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8570 goto exit;
8571 }
8572 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8573 {
8574 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8575 goto exit;
8576 }
8577 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8578 {
8579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8580 goto exit;
8581 }
8582 if( ( ret = mbedtls_md_finish( &ctx, output ) ) != 0 )
8583 {
8584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8585 goto exit;
8586 }
8587
8588exit:
8589 mbedtls_md_free( &ctx );
8590
8591 if( ret != 0 )
8592 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8593 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8594
8595 return( ret );
8596}
8597#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
8598 MBEDTLS_SSL_PROTO_TLS1_2 */
8599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600#endif /* MBEDTLS_SSL_TLS_C */