blob: c2e5bde99a26c69cacf1c2b4824e91d2910a956c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48/*
49 * The SSL 3.0 specification was drafted by Netscape in 1996,
50 * and became an IETF standard in 1999.
51 *
52 * http://wp.netscape.com/eng/ssl3/
53 * http://www.ietf.org/rfc/rfc2246.txt
54 * http://www.ietf.org/rfc/rfc4346.txt
55 */
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020059#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020061#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000064
SimonBd5800b72016-04-26 07:43:27 +010065#if defined(MBEDTLS_PLATFORM_C)
66#include "mbedtls/platform.h"
67#else
68#include <stdlib.h>
69#define mbedtls_calloc calloc
70#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010071#endif
72
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/debug.h"
74#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020075#include "mbedtls/ssl_internal.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020076
Rich Evans00ab4702015-02-06 13:43:58 +000077#include <string.h>
78
Janos Follath23bdca02016-10-07 14:47:14 +010079#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020081#endif
82
Paul Bakker34617722014-06-13 17:20:13 +020083/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020085 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
86}
87
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010088/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010090{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020092 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010093 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010094#else
95 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010096#endif
97 return( 0 );
98}
99
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200100/*
101 * Start a timer.
102 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200106 if( ssl->f_set_timer == NULL )
107 return;
108
109 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
110 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200111}
112
113/*
114 * Return -1 is timer is expired, 0 if it isn't.
115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200117{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200118 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200119 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200120
121 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200122 {
123 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200124 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200125 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200126
127 return( 0 );
128}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200129
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200130#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200131/*
132 * Double the retransmit timeout value, within the allowed range,
133 * returning -1 if the maximum value has already been reached.
134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200136{
137 uint32_t new_timeout;
138
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200139 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200140 return( -1 );
141
142 new_timeout = 2 * ssl->handshake->retransmit_timeout;
143
144 /* Avoid arithmetic overflow and range overflow */
145 if( new_timeout < ssl->handshake->retransmit_timeout ||
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 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200148 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200149 }
150
151 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200153 ssl->handshake->retransmit_timeout ) );
154
155 return( 0 );
156}
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200159{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200160 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200162 ssl->handshake->retransmit_timeout ) );
163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200167/*
168 * Convert max_fragment_length codes to length.
169 * RFC 6066 says:
170 * enum{
171 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
172 * } MaxFragmentLength;
173 * and we add 0 -> extension unused
174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175static unsigned int mfl_code_to_length[MBEDTLS_SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200176{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 MBEDTLS_SSL_MAX_CONTENT_LEN, /* MBEDTLS_SSL_MAX_FRAG_LEN_NONE */
178 512, /* MBEDTLS_SSL_MAX_FRAG_LEN_512 */
179 1024, /* MBEDTLS_SSL_MAX_FRAG_LEN_1024 */
180 2048, /* MBEDTLS_SSL_MAX_FRAG_LEN_2048 */
181 4096, /* MBEDTLS_SSL_MAX_FRAG_LEN_4096 */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200182};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200184
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200185#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_ssl_session_free( dst );
189 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200192 if( src->peer_cert != NULL )
193 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200194 int ret;
195
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200196 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200197 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200198 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200203 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200206 dst->peer_cert = NULL;
207 return( ret );
208 }
209 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200211
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200212#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200213 if( src->ticket != NULL )
214 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200215 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200216 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200217 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200218
219 memcpy( dst->ticket, src->ticket, src->ticket_len );
220 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200221#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200222
223 return( 0 );
224}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200225#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
228int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200229 const unsigned char *key_enc, const unsigned char *key_dec,
230 size_t keylen,
231 const unsigned char *iv_enc, const unsigned char *iv_dec,
232 size_t ivlen,
233 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200234 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
236int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
237int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
238int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
239int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
240#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000241
Paul Bakker5121ce52009-01-03 21:22:43 +0000242/*
243 * Key material generation
244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200246static int ssl3_prf( const unsigned char *secret, size_t slen,
247 const char *label,
248 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000249 unsigned char *dstbuf, size_t dlen )
250{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100251 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000252 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200253 mbedtls_md5_context md5;
254 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000255 unsigned char padding[16];
256 unsigned char sha1sum[20];
257 ((void)label);
258
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200259 mbedtls_md5_init( &md5 );
260 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200261
Paul Bakker5f70b252012-09-13 14:23:06 +0000262 /*
263 * SSLv3:
264 * block =
265 * MD5( secret + SHA1( 'A' + secret + random ) ) +
266 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
267 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
268 * ...
269 */
270 for( i = 0; i < dlen / 16; i++ )
271 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200272 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000273
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100274 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 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, padding, 1 + i ) ) != 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, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100279 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100280 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100281 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100282 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100283 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000284
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100285 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 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, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100288 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100289 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100290 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100291 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100292 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000293 }
294
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100295exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200296 mbedtls_md5_free( &md5 );
297 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_zeroize( padding, sizeof( padding ) );
300 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000301
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100302 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000303}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200307static int tls1_prf( const unsigned char *secret, size_t slen,
308 const char *label,
309 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000310 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000311{
Paul Bakker23986e52011-04-24 08:57:21 +0000312 size_t nb, hs;
313 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200314 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 unsigned char tmp[128];
316 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 const mbedtls_md_info_t *md_info;
318 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100319 int ret;
320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
323 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
326 hs = ( slen + 1 ) / 2;
327 S1 = secret;
328 S2 = secret + slen - hs;
329
330 nb = strlen( label );
331 memcpy( tmp + 20, label, nb );
332 memcpy( tmp + 20 + nb, random, rlen );
333 nb += rlen;
334
335 /*
336 * First compute P_md5(secret,label+random)[0..dlen]
337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
339 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100342 return( ret );
343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
345 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
346 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
348 for( i = 0; i < dlen; i += 16 )
349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_md_hmac_reset ( &md_ctx );
351 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
352 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_md_hmac_reset ( &md_ctx );
355 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
356 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000357
358 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
359
360 for( j = 0; j < k; j++ )
361 dstbuf[i + j] = h_i[j];
362 }
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100365
Paul Bakker5121ce52009-01-03 21:22:43 +0000366 /*
367 * XOR out with P_sha1(secret,label+random)[0..dlen]
368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
370 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100373 return( ret );
374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
376 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
377 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000378
379 for( i = 0; i < dlen; i += 20 )
380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_md_hmac_reset ( &md_ctx );
382 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
383 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_md_hmac_reset ( &md_ctx );
386 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
387 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000388
389 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
390
391 for( j = 0; j < k; j++ )
392 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
393 }
394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_zeroize( tmp, sizeof( tmp ) );
398 mbedtls_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
400 return( 0 );
401}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
405static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100406 const unsigned char *secret, size_t slen,
407 const char *label,
408 const unsigned char *random, size_t rlen,
409 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000410{
411 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100412 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
415 const mbedtls_md_info_t *md_info;
416 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100417 int ret;
418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
422 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100425
426 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000428
429 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100430 memcpy( tmp + md_len, label, nb );
431 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000432 nb += rlen;
433
434 /*
435 * Compute P_<hash>(secret, label + random)[0..dlen]
436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100438 return( ret );
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
441 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
442 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100443
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100444 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_md_hmac_reset ( &md_ctx );
447 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
448 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_md_hmac_reset ( &md_ctx );
451 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
452 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000453
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100454 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000455
456 for( j = 0; j < k; j++ )
457 dstbuf[i + j] = h_i[j];
458 }
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_zeroize( tmp, sizeof( tmp ) );
463 mbedtls_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000464
465 return( 0 );
466}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100469static int tls_prf_sha256( const unsigned char *secret, size_t slen,
470 const char *label,
471 const unsigned char *random, size_t rlen,
472 unsigned char *dstbuf, size_t dlen )
473{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100475 label, random, rlen, dstbuf, dlen ) );
476}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200480static int tls_prf_sha384( const unsigned char *secret, size_t slen,
481 const char *label,
482 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000483 unsigned char *dstbuf, size_t dlen )
484{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100486 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488#endif /* MBEDTLS_SHA512_C */
489#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
494 defined(MBEDTLS_SSL_PROTO_TLS1_1)
495static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200496#endif
Paul Bakker380da532012-04-18 16:10:25 +0000497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#if defined(MBEDTLS_SSL_PROTO_SSL3)
499static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
500static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200501#endif
502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
504static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
505static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200506#endif
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
509#if defined(MBEDTLS_SHA256_C)
510static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
511static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
512static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200513#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#if defined(MBEDTLS_SHA512_C)
516static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
517static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
518static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100519#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000523{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200524 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000525 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 unsigned char keyblk[256];
527 unsigned char *key1;
528 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100529 unsigned char *mac_enc;
530 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000531 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200532 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 const mbedtls_cipher_info_t *cipher_info;
534 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_ssl_session *session = ssl->session_negotiate;
537 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
538 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100543 if( cipher_info == NULL )
544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100546 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100548 }
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100551 if( md_info == NULL )
552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100554 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100556 }
557
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000559 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561#if defined(MBEDTLS_SSL_PROTO_SSL3)
562 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000563 {
Paul Bakker48916f92012-09-16 19:57:18 +0000564 handshake->tls_prf = ssl3_prf;
565 handshake->calc_verify = ssl_calc_verify_ssl;
566 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000567 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200568 else
569#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
571 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000572 {
Paul Bakker48916f92012-09-16 19:57:18 +0000573 handshake->tls_prf = tls1_prf;
574 handshake->calc_verify = ssl_calc_verify_tls;
575 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000576 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200577 else
578#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
580#if defined(MBEDTLS_SHA512_C)
581 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
582 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000583 {
Paul Bakker48916f92012-09-16 19:57:18 +0000584 handshake->tls_prf = tls_prf_sha384;
585 handshake->calc_verify = ssl_calc_verify_tls_sha384;
586 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000587 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000588 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200589#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#if defined(MBEDTLS_SHA256_C)
591 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000592 {
Paul Bakker48916f92012-09-16 19:57:18 +0000593 handshake->tls_prf = tls_prf_sha256;
594 handshake->calc_verify = ssl_calc_verify_tls_sha256;
595 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000596 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200597 else
598#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
602 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200603 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000604
605 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 * SSLv3:
607 * master =
608 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
609 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
610 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200611 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200612 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 * master = PRF( premaster, "master secret", randbytes )[0..47]
614 */
Paul Bakker0a597072012-09-25 21:55:46 +0000615 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000618 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
621 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200622 {
623 unsigned char session_hash[48];
624 size_t hash_len;
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200627
628 ssl->handshake->calc_verify( ssl, session_hash );
629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
631 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200634 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200636 {
637 hash_len = 48;
638 }
639 else
640#endif
641 hash_len = 32;
642 }
643 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200645 hash_len = 36;
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200648
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100649 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
650 "extended master secret",
651 session_hash, hash_len,
652 session->master, 48 );
653 if( ret != 0 )
654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100656 return( ret );
657 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200658
659 }
660 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200661#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100662 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
663 "master secret",
664 handshake->randbytes, 64,
665 session->master, 48 );
666 if( ret != 0 )
667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100669 return( ret );
670 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 }
674 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000676
677 /*
678 * Swap the client and server random values.
679 */
Paul Bakker48916f92012-09-16 19:57:18 +0000680 memcpy( tmp, handshake->randbytes, 64 );
681 memcpy( handshake->randbytes, tmp + 32, 32 );
682 memcpy( handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000684
685 /*
686 * SSLv3:
687 * key block =
688 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
689 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
690 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
691 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
692 * ...
693 *
694 * TLSv1:
695 * key block = PRF( master, "key expansion", randbytes )
696 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100697 ret = handshake->tls_prf( session->master, 48, "key expansion",
698 handshake->randbytes, 64, keyblk, 256 );
699 if( ret != 0 )
700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100702 return( ret );
703 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
706 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
707 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
708 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
709 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000712
713 /*
714 * Determine the appropriate key, IV and MAC length.
715 */
Paul Bakker68884e32013-01-07 18:20:04 +0100716
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200717 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
720 cipher_info->mode == MBEDTLS_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200722 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000723 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200724
Paul Bakker68884e32013-01-07 18:20:04 +0100725 transform->ivlen = 12;
726 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200727
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200728 /* Minimum length is expicit IV + tag */
729 transform->minlen = transform->ivlen - transform->fixed_ivlen
730 + ( transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100732 }
733 else
734 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200735 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
737 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200740 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100741 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200743 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000744 mac_key_len = mbedtls_md_get_size( md_info );
745 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200748 /*
749 * If HMAC is to be truncated, we shall keep the leftmost bytes,
750 * (rfc 6066 page 13 or rfc 2104 section 4),
751 * so we only need to adjust the length here.
752 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000756
757#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
758 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000759 * HMAC implementation which also truncates the key
760 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000761 mac_key_len = transform->maclen;
762#endif
763 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200765
766 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100767 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000768
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200769 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200771 transform->minlen = transform->maclen;
772 else
Paul Bakker68884e32013-01-07 18:20:04 +0100773 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200774 /*
775 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100776 * 1. if EtM is in use: one block plus MAC
777 * otherwise: * first multiple of blocklen greater than maclen
778 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
781 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100782 {
783 transform->minlen = transform->maclen
784 + cipher_info->block_size;
785 }
786 else
787#endif
788 {
789 transform->minlen = transform->maclen
790 + cipher_info->block_size
791 - transform->maclen % cipher_info->block_size;
792 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
795 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
796 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200797 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100798 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200799#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
801 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
802 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200803 {
804 transform->minlen += transform->ivlen;
805 }
806 else
807#endif
808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
810 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200811 }
Paul Bakker68884e32013-01-07 18:20:04 +0100812 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000813 }
814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000816 transform->keylen, transform->minlen, transform->ivlen,
817 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000818
819 /*
820 * Finally setup the cipher contexts, IVs and MAC secrets.
821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200823 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000824 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000825 key1 = keyblk + mac_key_len * 2;
826 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000827
Paul Bakker68884e32013-01-07 18:20:04 +0100828 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000829 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000830
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000831 /*
832 * This is not used in TLS v1.1.
833 */
Paul Bakker48916f92012-09-16 19:57:18 +0000834 iv_copy_len = ( transform->fixed_ivlen ) ?
835 transform->fixed_ivlen : transform->ivlen;
836 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
837 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000838 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000839 }
840 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#endif /* MBEDTLS_SSL_CLI_C */
842#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200843 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000844 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000845 key1 = keyblk + mac_key_len * 2 + transform->keylen;
846 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000847
Hanno Becker81c7b182017-11-09 18:39:33 +0000848 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100849 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000850
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000851 /*
852 * This is not used in TLS v1.1.
853 */
Paul Bakker48916f92012-09-16 19:57:18 +0000854 iv_copy_len = ( transform->fixed_ivlen ) ?
855 transform->fixed_ivlen : transform->ivlen;
856 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
857 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000858 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000859 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100860 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
864 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100865 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_SSL_PROTO_SSL3)
868 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100869 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000870 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
873 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100874 }
875
Hanno Becker81c7b182017-11-09 18:39:33 +0000876 memcpy( transform->mac_enc, mac_enc, mac_key_len );
877 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100878 }
879 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880#endif /* MBEDTLS_SSL_PROTO_SSL3 */
881#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
882 defined(MBEDTLS_SSL_PROTO_TLS1_2)
883 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100884 {
Gilles Peskine21701302018-03-19 19:06:08 +0100885 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
886 For AEAD-based ciphersuites, there is nothing to do here. */
887 if( mac_key_len != 0 )
888 {
889 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
890 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
891 }
Paul Bakker68884e32013-01-07 18:20:04 +0100892 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200893 else
894#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
897 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200898 }
Paul Bakker68884e32013-01-07 18:20:04 +0100899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
901 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100906 transform->iv_enc, transform->iv_dec,
907 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100908 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +0000909 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
912 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000913 }
914 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000916
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200917#if defined(MBEDTLS_SSL_EXPORT_KEYS)
918 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100919 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200920 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
921 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +0000922 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100923 iv_copy_len );
924 }
925#endif
926
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200927 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200928 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200930 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200931 return( ret );
932 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200933
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200934 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200935 cipher_info ) ) != 0 )
936 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200937 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200938 return( ret );
939 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200942 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200946 return( ret );
947 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200950 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200954 return( ret );
955 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957#if defined(MBEDTLS_CIPHER_MODE_CBC)
958 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
961 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200964 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200965 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
968 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200971 return( ret );
972 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 mbedtls_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +0000979 // Initialize compression
980 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000982 {
Paul Bakker16770332013-10-11 09:59:44 +0200983 if( ssl->compress_buf == NULL )
984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200986 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +0200987 if( ssl->compress_buf == NULL )
988 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 MBEDTLS_SSL_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200991 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +0200992 }
993 }
994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000996
Paul Bakker48916f92012-09-16 19:57:18 +0000997 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
998 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000999
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001000 if( deflateInit( &transform->ctx_deflate,
1001 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001002 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1005 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001006 }
1007 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001011
1012 return( 0 );
1013}
1014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#if defined(MBEDTLS_SSL_PROTO_SSL3)
1016void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001017{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001018 mbedtls_md5_context md5;
1019 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001020 unsigned char pad_1[48];
1021 unsigned char pad_2[48];
1022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001025 mbedtls_md5_init( &md5 );
1026 mbedtls_sha1_init( &sha1 );
1027
1028 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1029 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
Paul Bakker380da532012-04-18 16:10:25 +00001031 memset( pad_1, 0x36, 48 );
1032 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001033
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001034 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1035 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1036 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001038 mbedtls_md5_starts_ret( &md5 );
1039 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1040 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1041 mbedtls_md5_update_ret( &md5, hash, 16 );
1042 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001043
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001044 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1045 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1046 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001047
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001048 mbedtls_sha1_starts_ret( &sha1 );
1049 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1050 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1051 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1052 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001056
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001057 mbedtls_md5_free( &md5 );
1058 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001059
Paul Bakker380da532012-04-18 16:10:25 +00001060 return;
1061}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1065void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001066{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001067 mbedtls_md5_context md5;
1068 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001071
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001072 mbedtls_md5_init( &md5 );
1073 mbedtls_sha1_init( &sha1 );
1074
1075 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1076 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001077
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001078 mbedtls_md5_finish_ret( &md5, hash );
1079 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001083
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001084 mbedtls_md5_free( &md5 );
1085 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001086
Paul Bakker380da532012-04-18 16:10:25 +00001087 return;
1088}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1092#if defined(MBEDTLS_SHA256_C)
1093void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001094{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001095 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001096
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001097 mbedtls_sha256_init( &sha256 );
1098
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001100
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001101 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001102 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001106
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001107 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001108
Paul Bakker380da532012-04-18 16:10:25 +00001109 return;
1110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113#if defined(MBEDTLS_SHA512_C)
1114void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001115{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001116 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001117
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001118 mbedtls_sha512_init( &sha512 );
1119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001121
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001122 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001123 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001127
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001128 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001129
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 return;
1131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#endif /* MBEDTLS_SHA512_C */
1133#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1136int 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 +02001137{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001138 unsigned char *p = ssl->handshake->premaster;
1139 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001140 const unsigned char *psk = ssl->conf->psk;
1141 size_t psk_len = ssl->conf->psk_len;
1142
1143 /* If the psk callback was called, use its result */
1144 if( ssl->handshake->psk != NULL )
1145 {
1146 psk = ssl->handshake->psk;
1147 psk_len = ssl->handshake->psk_len;
1148 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001149
1150 /*
1151 * PMS = struct {
1152 * opaque other_secret<0..2^16-1>;
1153 * opaque psk<0..2^16-1>;
1154 * };
1155 * with "other_secret" depending on the particular key exchange
1156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1158 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001159 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001160 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001162
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001163 *(p++) = (unsigned char)( psk_len >> 8 );
1164 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001165
1166 if( end < p || (size_t)( end - p ) < psk_len )
1167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1168
1169 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001170 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001171 }
1172 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1174#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1175 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001176 {
1177 /*
1178 * other_secret already set by the ClientKeyExchange message,
1179 * and is 48 bytes long
1180 */
Philippe Antoine33e5c322018-07-09 10:39:02 +02001181 if( end - p < 2 )
1182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1183
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001184 *p++ = 0;
1185 *p++ = 48;
1186 p += 48;
1187 }
1188 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1190#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1191 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001192 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001193 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001194 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001195
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001196 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001198 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001199 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001202 return( ret );
1203 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001204 *(p++) = (unsigned char)( len >> 8 );
1205 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001206 p += len;
1207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001209 }
1210 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1212#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1213 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001214 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001215 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001216 size_t zlen;
1217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001219 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001220 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001223 return( ret );
1224 }
1225
1226 *(p++) = (unsigned char)( zlen >> 8 );
1227 *(p++) = (unsigned char)( zlen );
1228 p += zlen;
1229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001231 }
1232 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1236 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001237 }
1238
1239 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001240 if( end - p < 2 )
1241 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001242
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001243 *(p++) = (unsigned char)( psk_len >> 8 );
1244 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001245
1246 if( end < p || (size_t)( end - p ) < psk_len )
1247 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1248
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001249 memcpy( p, psk, psk_len );
1250 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001251
1252 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1253
1254 return( 0 );
1255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001259/*
1260 * SSLv3.0 MAC functions
1261 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001262#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001263static void ssl_mac( mbedtls_md_context_t *md_ctx,
1264 const unsigned char *secret,
1265 const unsigned char *buf, size_t len,
1266 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001267 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001268{
1269 unsigned char header[11];
1270 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001271 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1273 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001274
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001275 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001277 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001278 else
Paul Bakker68884e32013-01-07 18:20:04 +01001279 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001280
1281 memcpy( header, ctr, 8 );
1282 header[ 8] = (unsigned char) type;
1283 header[ 9] = (unsigned char)( len >> 8 );
1284 header[10] = (unsigned char)( len );
1285
Paul Bakker68884e32013-01-07 18:20:04 +01001286 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 mbedtls_md_starts( md_ctx );
1288 mbedtls_md_update( md_ctx, secret, md_size );
1289 mbedtls_md_update( md_ctx, padding, padlen );
1290 mbedtls_md_update( md_ctx, header, 11 );
1291 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001292 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Paul Bakker68884e32013-01-07 18:20:04 +01001294 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 mbedtls_md_starts( md_ctx );
1296 mbedtls_md_update( md_ctx, secret, md_size );
1297 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001298 mbedtls_md_update( md_ctx, out, md_size );
1299 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001304 defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001305#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001306#endif
1307
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02001308/* The function below is only used in the Lucky 13 counter-measure in
1309 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1310#if defined(SSL_SOME_MODES_USE_MAC) && \
1311 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1312 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1313 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1314/* This function makes sure every byte in the memory region is accessed
1315 * (in ascending addresses order) */
1316static void ssl_read_memory( unsigned char *p, size_t len )
1317{
1318 unsigned char acc = 0;
1319 volatile unsigned char force;
1320
1321 for( ; len != 0; p++, len-- )
1322 acc ^= *p;
1323
1324 force = acc;
1325 (void) force;
1326}
1327#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1328
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001329/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001330 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001333{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001335 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001338
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001339 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1342 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001343 }
1344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001348 ssl->out_msg, ssl->out_msglen );
1349
Hanno Beckerd33f1ca2017-09-18 10:55:31 +01001350 if( ssl->out_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
1351 {
Hanno Becker184f6752017-10-04 13:47:33 +01001352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1353 (unsigned) ssl->out_msglen,
Hanno Beckerd33f1ca2017-09-18 10:55:31 +01001354 MBEDTLS_SSL_MAX_CONTENT_LEN ) );
1355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1356 }
1357
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001359 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001360 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001361#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 if( mode == MBEDTLS_MODE_STREAM ||
1363 ( mode == MBEDTLS_MODE_CBC
1364#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1365 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001366#endif
1367 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369#if defined(MBEDTLS_SSL_PROTO_SSL3)
1370 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001371 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001372 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001373
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001374 ssl_mac( &ssl->transform_out->md_ctx_enc,
1375 ssl->transform_out->mac_enc,
1376 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001377 ssl->out_ctr, ssl->out_msgtype,
1378 mac );
1379
1380 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001381 }
1382 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001383#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1385 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1386 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001387 {
Hanno Becker992b6872017-11-09 18:57:39 +00001388 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1391 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1392 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1393 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001394 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001395 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001397
1398 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001399 }
1400 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001401#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1404 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001405 }
1406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001408 ssl->out_msg + ssl->out_msglen,
1409 ssl->transform_out->maclen );
1410
1411 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001412 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001413 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001414#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001416 /*
1417 * Encrypt
1418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1420 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001422 int ret;
1423 size_t olen = 0;
1424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 "including %d bytes of padding",
1427 ssl->out_msglen, 0 ) );
1428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001430 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001431 ssl->transform_out->ivlen,
1432 ssl->out_msg, ssl->out_msglen,
1433 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001436 return( ret );
1437 }
1438
1439 if( ssl->out_msglen != olen )
1440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1442 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001443 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 }
Paul Bakker68884e32013-01-07 18:20:04 +01001445 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1447#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1448 if( mode == MBEDTLS_MODE_GCM ||
1449 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001450 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001451 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001452 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001453 unsigned char *enc_msg;
1454 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001455 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001457
Paul Bakkerca4ab492012-04-18 14:23:57 +00001458 memcpy( add_data, ssl->out_ctr, 8 );
1459 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001461 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001462 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1463 add_data[12] = ssl->out_msglen & 0xFF;
1464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakkerca4ab492012-04-18 14:23:57 +00001466 add_data, 13 );
1467
Paul Bakker68884e32013-01-07 18:20:04 +01001468 /*
1469 * Generate IV
1470 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001471 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1472 {
1473 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1475 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001476 }
1477
1478 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1479 ssl->out_ctr, 8 );
1480 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001483 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001484
Paul Bakker68884e32013-01-07 18:20:04 +01001485 /*
1486 * Fix pointer positions and message length with added IV
1487 */
1488 enc_msg = ssl->out_msg;
1489 enc_msglen = ssl->out_msglen;
1490 ssl->out_msglen += ssl->transform_out->ivlen -
1491 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker68884e32013-01-07 18:20:04 +01001494 "including %d bytes of padding",
1495 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001496
Paul Bakker68884e32013-01-07 18:20:04 +01001497 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001498 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 if( ( ret = mbedtls_cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001501 ssl->transform_out->iv_enc,
1502 ssl->transform_out->ivlen,
1503 add_data, 13,
1504 enc_msg, enc_msglen,
1505 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001506 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001509 return( ret );
1510 }
1511
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001512 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1515 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001516 }
1517
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001518 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001519 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001522 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001523 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001525#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001527 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001528 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001529 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001530 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001531
Paul Bakker48916f92012-09-16 19:57:18 +00001532 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1533 ssl->transform_out->ivlen;
1534 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 padlen = 0;
1536
1537 for( i = 0; i <= padlen; i++ )
1538 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1539
1540 ssl->out_msglen += padlen + 1;
1541
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001542 enc_msglen = ssl->out_msglen;
1543 enc_msg = ssl->out_msg;
1544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001546 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001547 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1548 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001551 {
1552 /*
1553 * Generate IV
1554 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001555 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001556 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001557 if( ret != 0 )
1558 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001559
Paul Bakker92be97b2013-01-02 17:30:03 +01001560 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001561 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001562
1563 /*
1564 * Fix pointer positions and message length with added IV
1565 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001566 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001567 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001568 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001569 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001573 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001574 ssl->out_msglen, ssl->transform_out->ivlen,
1575 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001578 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001579 ssl->transform_out->ivlen,
1580 enc_msg, enc_msglen,
1581 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001584 return( ret );
1585 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001586
Paul Bakkercca5b812013-08-31 17:40:26 +02001587 if( enc_msglen != olen )
1588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1590 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001591 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1594 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001595 {
1596 /*
1597 * Save IV in SSL3 and TLS1
1598 */
1599 memcpy( ssl->transform_out->iv_enc,
1600 ssl->transform_out->cipher_ctx_enc.iv,
1601 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001603#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001606 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001607 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001608 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1609
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001610 /*
1611 * MAC(MAC_write_key, seq_num +
1612 * TLSCipherText.type +
1613 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001614 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001615 * IV + // except for TLS 1.0
1616 * ENC(content + padding + padding_length));
1617 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001618 unsigned char pseudo_hdr[13];
1619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001621
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001622 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1623 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001624 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1625 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1630 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001631 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001632 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001634
Hanno Becker3d8c9072018-01-05 16:24:22 +00001635 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1636 ssl->transform_out->maclen );
1637
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001638 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001639 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001640 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001642 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001643 else
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001644#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001648 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001649
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001650 /* Make extra sure authentication was performed, exactly once */
1651 if( auth_done != 1 )
1652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1654 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001655 }
1656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
1659 return( 0 );
1660}
1661
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001662#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
1663 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1664 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1665 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1666/*
1667 * Compute HMAC of variable-length data with constant flow.
1668 */
1669int mbedtls_ssl_cf_hmac(
1670 mbedtls_md_context_t *ctx,
1671 const unsigned char *add_data, size_t add_data_len,
1672 const unsigned char *data, size_t data_len_secret,
1673 size_t min_data_len, size_t max_data_len,
1674 unsigned char *output )
1675{
1676 /* WORK IN PROGRESS - THIS IS NOT CONSTANT FLOW AT ALL */
1677 (void) min_data_len;
1678 (void) max_data_len;
1679 mbedtls_md_hmac_update( ctx, add_data, add_data_len );
1680 mbedtls_md_hmac_update( ctx, data, data_len_secret );
1681 mbedtls_md_hmac_finish( ctx, output );
1682 mbedtls_md_hmac_reset( ctx );
1683
1684 return( 0 );
1685}
1686#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC && TLS 1.0-1.2 */
1687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001689{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001690 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001692 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001693#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001694 size_t padlen = 0, correct = 1;
1695#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001698
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001699 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001703 }
1704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001706
Paul Bakker48916f92012-09-16 19:57:18 +00001707 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001710 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001712 }
1713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1715 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001716 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001717 int ret;
1718 size_t olen = 0;
1719
Paul Bakker68884e32013-01-07 18:20:04 +01001720 padlen = 0;
1721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001723 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001724 ssl->transform_in->ivlen,
1725 ssl->in_msg, ssl->in_msglen,
1726 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001729 return( ret );
1730 }
1731
1732 if( ssl->in_msglen != olen )
1733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1735 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001736 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001737 }
Paul Bakker68884e32013-01-07 18:20:04 +01001738 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1740#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1741 if( mode == MBEDTLS_MODE_GCM ||
1742 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001743 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001744 int ret;
1745 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001746 unsigned char *dec_msg;
1747 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001748 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001749 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001751 size_t explicit_iv_len = ssl->transform_in->ivlen -
1752 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001753
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001754 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001757 "+ taglen (%d)", ssl->in_msglen,
1758 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001760 }
1761 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1762
Paul Bakker68884e32013-01-07 18:20:04 +01001763 dec_msg = ssl->in_msg;
1764 dec_msg_result = ssl->in_msg;
1765 ssl->in_msglen = dec_msglen;
1766
1767 memcpy( add_data, ssl->in_ctr, 8 );
1768 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001770 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001771 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1772 add_data[12] = ssl->in_msglen & 0xFF;
1773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakker68884e32013-01-07 18:20:04 +01001775 add_data, 13 );
1776
1777 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1778 ssl->in_iv,
1779 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
Paul Bakker68884e32013-01-07 18:20:04 +01001782 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001784
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001785 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001786 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001789 ssl->transform_in->iv_dec,
1790 ssl->transform_in->ivlen,
1791 add_data, 13,
1792 dec_msg, dec_msglen,
1793 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001794 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1799 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001800
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001801 return( ret );
1802 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001803 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001804
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001805 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1808 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001809 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001810 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001811 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001813#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001815 {
Paul Bakker45829992013-01-03 14:52:21 +01001816 /*
1817 * Decrypt and check the padding
1818 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001819 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001820 unsigned char *dec_msg;
1821 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001822 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001823 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001824 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001825
Paul Bakker5121ce52009-01-03 21:22:43 +00001826 /*
Paul Bakker45829992013-01-03 14:52:21 +01001827 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1830 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001831 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001832#endif
Paul Bakker45829992013-01-03 14:52:21 +01001833
1834 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1835 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001838 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1839 ssl->transform_in->ivlen,
1840 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001842 }
1843
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001844 dec_msglen = ssl->in_msglen;
1845 dec_msg = ssl->in_msg;
1846 dec_msg_result = ssl->in_msg;
1847
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001848 /*
1849 * Authenticate before decrypt if enabled
1850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1852 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001853 {
Hanno Becker992b6872017-11-09 18:57:39 +00001854 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001855 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001858
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001859 dec_msglen -= ssl->transform_in->maclen;
1860 ssl->in_msglen -= ssl->transform_in->maclen;
1861
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001862 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1863 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1864 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1865 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1870 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001871 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001872 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001876 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001877 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001878 ssl->transform_in->maclen );
1879
Hanno Becker992b6872017-11-09 18:57:39 +00001880 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1881 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001886 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001887 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001888 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001890
1891 /*
1892 * Check length sanity
1893 */
1894 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001897 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001899 }
1900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001902 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001903 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001904 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001906 {
Paul Bakker48916f92012-09-16 19:57:18 +00001907 dec_msglen -= ssl->transform_in->ivlen;
1908 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001909
Paul Bakker48916f92012-09-16 19:57:18 +00001910 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001911 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001912 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001916 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001917 ssl->transform_in->ivlen,
1918 dec_msg, dec_msglen,
1919 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001922 return( ret );
1923 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001924
Paul Bakkercca5b812013-08-31 17:40:26 +02001925 if( dec_msglen != olen )
1926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1928 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001929 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1932 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001933 {
1934 /*
1935 * Save IV in SSL3 and TLS1
1936 */
1937 memcpy( ssl->transform_in->iv_dec,
1938 ssl->transform_in->cipher_ctx_dec.iv,
1939 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001940 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001941#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001942
1943 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001944
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001945 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001946 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948#if defined(MBEDTLS_SSL_DEBUG_ALL)
1949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01001950 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001951#endif
Paul Bakker45829992013-01-03 14:52:21 +01001952 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001953 correct = 0;
1954 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956#if defined(MBEDTLS_SSL_PROTO_SSL3)
1957 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 {
Paul Bakker48916f92012-09-16 19:57:18 +00001959 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961#if defined(MBEDTLS_SSL_DEBUG_ALL)
1962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001964 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001965#endif
Paul Bakker45829992013-01-03 14:52:21 +01001966 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 }
1968 }
1969 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1971#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1972 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1973 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 {
1975 /*
Paul Bakker45829992013-01-03 14:52:21 +01001976 * TLSv1+: always check the padding up to the first failure
1977 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001978 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001979 size_t pad_count = 0, real_count = 1;
Angus Gratton1ba8e912018-06-19 15:57:50 +10001980 size_t padding_idx = ssl->in_msglen - padlen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001981
Paul Bakker956c9e02013-12-19 14:42:28 +01001982 /*
1983 * Padding is guaranteed to be incorrect if:
Angus Gratton1ba8e912018-06-19 15:57:50 +10001984 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001985 *
Angus Gratton1ba8e912018-06-19 15:57:50 +10001986 * 2. padding_idx > MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02001987 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001988 *
1989 * In both cases we reset padding_idx to a safe value (0) to
1990 * prevent out-of-buffer reads.
1991 */
Angus Gratton1ba8e912018-06-19 15:57:50 +10001992 correct &= ( padlen <= ssl->in_msglen );
1993 correct &= ( padding_idx <= MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02001994 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001995
1996 padding_idx *= correct;
1997
Angus Gratton1ba8e912018-06-19 15:57:50 +10001998 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001999 {
Angus Gratton1ba8e912018-06-19 15:57:50 +10002000 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002001 pad_count += real_count *
2002 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2003 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002004
2005 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002008 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002010#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002011 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002013 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2015 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2018 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002019 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002020
2021 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002023 else
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02002024#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2027 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002028 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002029
Manuel Pégourié-Gonnard7c344322018-07-10 11:15:36 +02002030#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard7c344322018-07-10 11:15:36 +02002033#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002034
2035 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002036 * Authenticate if not done yet.
2037 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002039#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002040 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002041 {
Hanno Becker992b6872017-11-09 18:57:39 +00002042 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002043
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002044 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002045
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002046 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2047 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049#if defined(MBEDTLS_SSL_PROTO_SSL3)
2050 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002051 {
2052 ssl_mac( &ssl->transform_in->md_ctx_dec,
2053 ssl->transform_in->mac_dec,
2054 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002055 ssl->in_ctr, ssl->in_msgtype,
2056 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002057 }
2058 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2060#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2061 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2062 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002063 {
2064 /*
2065 * Process MAC and always update for padlen afterwards to make
Gilles Peskinebb07ca02018-06-06 17:23:31 +02002066 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002067 *
2068 * Known timing attacks:
2069 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2070 *
Gilles Peskinebb07ca02018-06-06 17:23:31 +02002071 * To compensate for different timings for the MAC calculation
2072 * depending on how much padding was removed (which is determined
2073 * by padlen), process extra_run more blocks through the hash
2074 * function.
2075 *
2076 * The formula in the paper is
2077 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2078 * where L1 is the size of the header plus the decrypted message
2079 * plus CBC padding and L2 is the size of the header plus the
2080 * decrypted message. This is for an underlying hash function
2081 * with 64-byte blocks.
2082 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2083 * correctly. We round down instead of up, so -56 is the correct
2084 * value for our calculations instead of -55.
2085 *
2086 * Repeat the formula rather than defining a block_size variable.
2087 * This avoids requiring division by a variable at runtime
2088 * (which would be marginally less efficient and would require
2089 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002090 */
2091 size_t j, extra_run = 0;
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002092
2093 /*
2094 * The next two sizes are the minimum and maximum values of
2095 * in_msglen over all padlen values.
2096 *
2097 * They're independent of padlen, since we previously did
2098 * in_msglen -= padlen.
2099 *
2100 * Note that max_len + maclen is never more than the buffer
2101 * length, as we previously did in_msglen -= maclen too.
2102 */
2103 const size_t max_len = ssl->in_msglen + padlen;
2104 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2105
Gilles Peskinebb07ca02018-06-06 17:23:31 +02002106 switch( ssl->transform_in->ciphersuite_info->mac )
2107 {
2108#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2109 defined(MBEDTLS_SHA256_C)
2110 case MBEDTLS_MD_MD5:
2111 case MBEDTLS_MD_SHA1:
2112 case MBEDTLS_MD_SHA256:
2113 /* 8 bytes of message size, 64-byte compression blocks */
2114 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2115 ( 13 + ssl->in_msglen + 8 ) / 64;
2116 break;
2117#endif
2118#if defined(MBEDTLS_SHA512_C)
2119 case MBEDTLS_MD_SHA384:
2120 /* 16 bytes of message size, 128-byte compression blocks */
2121 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2122 ( 13 + ssl->in_msglen + 16 ) / 128;
2123 break;
2124#endif
2125 default:
2126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2127 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2128 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002129
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002130 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2133 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2134 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2135 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002136 ssl->in_msglen );
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002137 /* Make sure we access everything even when padlen > 0. This
2138 * makes the synchronisation requirements for just-in-time
2139 * Prime+Probe attacks much tighter and hopefully impractical. */
2140 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002141 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002142
Manuel Pégourié-Gonnard83527972020-06-18 11:30:40 +02002143 /* Dummy calls to compression function.
2144 * Call mbedtls_md_process at least once due to cache attacks
2145 * that observe whether md_process() was called of not.
2146 * Respect the usual start-(process|update)-finish sequence for
2147 * the sake of hardware accelerators that might require it. */
2148 mbedtls_md_starts( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002149 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Manuel Pégourié-Gonnard83527972020-06-18 11:30:40 +02002151 {
2152 /* The switch statement above already checks that we're using
2153 * one of MD-5, SHA-1, SHA-256 or SHA-384. */
2154 unsigned char tmp[384 / 8];
2155 mbedtls_md_finish( &ssl->transform_in->md_ctx_dec, tmp );
2156 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002159
2160 /* Make sure we access all the memory that could contain the MAC,
2161 * before we check it in the next code block. This makes the
2162 * synchronisation requirements for just-in-time Prime+Probe
2163 * attacks much tighter and hopefully impractical. */
2164 ssl_read_memory( ssl->in_msg + min_len,
2165 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002166 }
2167 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2169 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2172 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002173 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002174
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002175#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002176 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2177 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2178 ssl->transform_in->maclen );
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002179#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002180
Hanno Becker992b6872017-11-09 18:57:39 +00002181 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2182 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184#if defined(MBEDTLS_SSL_DEBUG_ALL)
2185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002186#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002187 correct = 0;
2188 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002189 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002190 }
Hanno Beckerca31b472018-10-17 14:43:14 +01002191
2192 /*
2193 * Finally check the correct flag
2194 */
2195 if( correct == 0 )
2196 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002197#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002198
2199 /* Make extra sure authentication was performed, exactly once */
2200 if( auth_done != 1 )
2201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2203 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002204 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002205
2206 if( ssl->in_msglen == 0 )
2207 {
Angus Grattonb91cb6e2018-06-19 15:58:22 +10002208#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2209 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2210 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2211 {
2212 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2214 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2215 }
2216#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2217
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 ssl->nb_zero++;
2219
2220 /*
2221 * Three or more empty messages may be a DoS attack
2222 * (excessive CPU consumption).
2223 */
2224 if( ssl->nb_zero > 3 )
2225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002227 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 }
2230 }
2231 else
2232 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002235 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002236 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002237 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002238 }
2239 else
2240#endif
2241 {
2242 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2243 if( ++ssl->in_ctr[i - 1] != 0 )
2244 break;
2245
2246 /* The loop goes to its end iff the counter is wrapping */
2247 if( i == ssl_ep_len( ssl ) )
2248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2250 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002251 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002252 }
2253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002255
2256 return( 0 );
2257}
2258
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002259#undef MAC_NONE
2260#undef MAC_PLAINTEXT
2261#undef MAC_CIPHERTEXT
2262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002264/*
2265 * Compression/decompression functions
2266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002268{
2269 int ret;
2270 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002271 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002272 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002273 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002276
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002277 if( len_pre == 0 )
2278 return( 0 );
2279
Paul Bakker2770fbd2012-07-03 13:30:23 +00002280 memcpy( msg_pre, ssl->out_msg, len_pre );
2281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002283 ssl->out_msglen ) );
2284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002286 ssl->out_msg, ssl->out_msglen );
2287
Paul Bakker48916f92012-09-16 19:57:18 +00002288 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2289 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2290 ssl->transform_out->ctx_deflate.next_out = msg_post;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002291 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002292
Paul Bakker48916f92012-09-16 19:57:18 +00002293 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002294 if( ret != Z_OK )
2295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2297 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002298 }
2299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 ssl->out_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002301 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002304 ssl->out_msglen ) );
2305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002307 ssl->out_msg, ssl->out_msglen );
2308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002310
2311 return( 0 );
2312}
2313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002315{
2316 int ret;
2317 unsigned char *msg_post = ssl->in_msg;
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002318 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002319 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002320 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002323
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002324 if( len_pre == 0 )
2325 return( 0 );
2326
Paul Bakker2770fbd2012-07-03 13:30:23 +00002327 memcpy( msg_pre, ssl->in_msg, len_pre );
2328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002330 ssl->in_msglen ) );
2331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002333 ssl->in_msg, ssl->in_msglen );
2334
Paul Bakker48916f92012-09-16 19:57:18 +00002335 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2336 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2337 ssl->transform_in->ctx_inflate.next_out = msg_post;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002338 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002339 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002340
Paul Bakker48916f92012-09-16 19:57:18 +00002341 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002342 if( ret != Z_OK )
2343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2345 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002346 }
2347
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002348 ssl->in_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002349 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002352 ssl->in_msglen ) );
2353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002355 ssl->in_msg, ssl->in_msglen );
2356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002358
2359 return( 0 );
2360}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2364static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366#if defined(MBEDTLS_SSL_PROTO_DTLS)
2367static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002368{
2369 /* If renegotiation is not enforced, retransmit until we would reach max
2370 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002371 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002372 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002373 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002374 unsigned char doublings = 1;
2375
2376 while( ratio != 0 )
2377 {
2378 ++doublings;
2379 ratio >>= 1;
2380 }
2381
2382 if( ++ssl->renego_records_seen > doublings )
2383 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002385 return( 0 );
2386 }
2387 }
2388
2389 return( ssl_write_hello_request( ssl ) );
2390}
2391#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002393
Paul Bakker5121ce52009-01-03 21:22:43 +00002394/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002395 * Fill the input message buffer by appending data to it.
2396 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002397 *
2398 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2399 * available (from this read and/or a previous one). Otherwise, an error code
2400 * is returned (possibly EOF or WANT_READ).
2401 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002402 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2403 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2404 * since we always read a whole datagram at once.
2405 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002406 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002407 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002410{
Paul Bakker23986e52011-04-24 08:57:21 +00002411 int ret;
2412 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002416 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002419 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002421 }
2422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 if( nb_want > MBEDTLS_SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2426 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002427 }
2428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002430 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002432 uint32_t timeout;
2433
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002434 /* Just to be sure */
2435 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2436 {
2437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2438 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2440 }
2441
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002442 /*
2443 * The point is, we need to always read a full datagram at once, so we
2444 * sometimes read more then requested, and handle the additional data.
2445 * It could be the rest of the current record (while fetching the
2446 * header) and/or some other records in the same datagram.
2447 */
2448
2449 /*
2450 * Move to the next record in the already read datagram if applicable
2451 */
2452 if( ssl->next_record_offset != 0 )
2453 {
2454 if( ssl->in_left < ssl->next_record_offset )
2455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2457 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002458 }
2459
2460 ssl->in_left -= ssl->next_record_offset;
2461
2462 if( ssl->in_left != 0 )
2463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002465 ssl->next_record_offset ) );
2466 memmove( ssl->in_hdr,
2467 ssl->in_hdr + ssl->next_record_offset,
2468 ssl->in_left );
2469 }
2470
2471 ssl->next_record_offset = 0;
2472 }
2473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002476
2477 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002478 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002479 */
2480 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002483 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002484 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002485
2486 /*
Antonin Décimo8fd91562019-01-23 15:24:37 +01002487 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002488 * are not at the beginning of a new record, the caller did something
2489 * wrong.
2490 */
2491 if( ssl->in_left != 0 )
2492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2494 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002495 }
2496
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002497 /*
2498 * Don't even try to read if time's out already.
2499 * This avoids by-passing the timer when repeatedly receiving messages
2500 * that will end up being dropped.
2501 */
2502 if( ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002503 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002504 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 len = MBEDTLS_SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002509 timeout = ssl->handshake->retransmit_timeout;
2510 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002511 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002514
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002515 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002516 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2517 timeout );
2518 else
2519 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002522
2523 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002525 }
2526
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002527 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002530 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002533 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002534 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002537 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002538 }
2539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002543 return( ret );
2544 }
2545
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002546 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002547 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002549 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002551 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002552 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002555 return( ret );
2556 }
2557
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002558 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002559 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002561 }
2562
Paul Bakker5121ce52009-01-03 21:22:43 +00002563 if( ret < 0 )
2564 return( ret );
2565
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002566 ssl->in_left = ret;
2567 }
2568 else
2569#endif
2570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002572 ssl->in_left, nb_want ) );
2573
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002574 while( ssl->in_left < nb_want )
2575 {
2576 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002577
2578 if( ssl_check_timer( ssl ) != 0 )
2579 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2580 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002581 {
2582 if( ssl->f_recv_timeout != NULL )
2583 {
2584 ret = ssl->f_recv_timeout( ssl->p_bio,
2585 ssl->in_hdr + ssl->in_left, len,
2586 ssl->conf->read_timeout );
2587 }
2588 else
2589 {
2590 ret = ssl->f_recv( ssl->p_bio,
2591 ssl->in_hdr + ssl->in_left, len );
2592 }
2593 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002596 ssl->in_left, nb_want ) );
2597 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002598
2599 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002601
2602 if( ret < 0 )
2603 return( ret );
2604
mohammad160344a6a682018-03-28 23:45:33 -07002605 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad1603b11af862018-03-19 07:18:13 -07002606 {
Jaeden Amerob5f53b12018-04-03 12:09:45 +01002607 MBEDTLS_SSL_DEBUG_MSG( 1,
2608 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160329ed80f2018-04-02 07:34:26 -07002609 ret, (unsigned long)len ) );
mohammad1603b11af862018-03-19 07:18:13 -07002610 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2611 }
2612
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002613 ssl->in_left += ret;
2614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 }
2616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618
2619 return( 0 );
2620}
2621
2622/*
2623 * Flush any data not yet written
2624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002626{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002627 int ret;
2628 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002632 if( ssl->f_send == NULL )
2633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002635 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002637 }
2638
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002639 /* Avoid incrementing counter if data is flushed */
2640 if( ssl->out_left == 0 )
2641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002643 return( 0 );
2644 }
2645
Paul Bakker5121ce52009-01-03 21:22:43 +00002646 while( ssl->out_left > 0 )
2647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2649 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651 buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) +
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002652 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002653 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
2657 if( ret <= 0 )
2658 return( ret );
2659
mohammad160344a6a682018-03-28 23:45:33 -07002660 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16036085c722018-02-22 04:29:04 -08002661 {
Jaeden Amerob5f53b12018-04-03 12:09:45 +01002662 MBEDTLS_SSL_DEBUG_MSG( 1,
2663 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160329ed80f2018-04-02 07:34:26 -07002664 ret, (unsigned long)ssl->out_left ) );
mohammad16036085c722018-02-22 04:29:04 -08002665 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2666 }
2667
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 ssl->out_left -= ret;
2669 }
2670
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002671 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002672 if( ++ssl->out_ctr[i - 1] != 0 )
2673 break;
2674
2675 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002676 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2679 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002680 }
2681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
2684 return( 0 );
2685}
2686
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002687/*
2688 * Functions to handle the DTLS retransmission state machine
2689 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002691/*
2692 * Append current handshake message to current outgoing flight
2693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002695{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 mbedtls_ssl_flight_item *msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002697
2698 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002699 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002700 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002703 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002704 }
2705
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002706 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002707 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002710 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002711 }
2712
2713 /* Copy current handshake message with headers */
2714 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2715 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002716 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002717 msg->next = NULL;
2718
2719 /* Append to the current flight */
2720 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002721 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002722 else
2723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002725 while( cur->next != NULL )
2726 cur = cur->next;
2727 cur->next = msg;
2728 }
2729
2730 return( 0 );
2731}
2732
2733/*
2734 * Free the current flight of handshake messages
2735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002737{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 mbedtls_ssl_flight_item *cur = flight;
2739 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002740
2741 while( cur != NULL )
2742 {
2743 next = cur->next;
2744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 mbedtls_free( cur->p );
2746 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002747
2748 cur = next;
2749 }
2750}
2751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2753static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002754#endif
2755
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002756/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002757 * Swap transform_out and out_ctr with the alternative ones
2758 */
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002759static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002760{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002762 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002763#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2764 int ret;
2765#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002766
2767 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002770 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002771 }
2772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002774
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002775 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002776 tmp_transform = ssl->transform_out;
2777 ssl->transform_out = ssl->handshake->alt_transform_out;
2778 ssl->handshake->alt_transform_out = tmp_transform;
2779
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002780 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002781 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2782 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2783 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002784
2785 /* Adjust to the newly activated transform */
2786 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002788 {
2789 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2790 ssl->transform_out->fixed_ivlen;
2791 }
2792 else
2793 ssl->out_msg = ssl->out_iv;
2794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2796 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2801 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002802 }
2803 }
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002804#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
2805
2806 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002807}
2808
2809/*
2810 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002811 *
2812 * Need to remember the current message in case flush_output returns
2813 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002814 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002817{
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002818 int ret;
2819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002825
2826 ssl->handshake->cur_msg = ssl->handshake->flight;
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002827 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
2828 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002831 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002832
2833 while( ssl->handshake->cur_msg != NULL )
2834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 mbedtls_ssl_flight_item *cur = ssl->handshake->cur_msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002836
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002837 /* Swap epochs before sending Finished: we can't do it after
2838 * sending ChangeCipherSpec, in case write returns WANT_READ.
2839 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2841 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002842 {
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002843 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
2844 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002845 }
2846
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002847 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002848 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002849 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002850
2851 ssl->handshake->cur_msg = cur->next;
2852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853 MBEDTLS_SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002858 return( ret );
2859 }
2860 }
2861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2863 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002864 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002867 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2868 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002871
2872 return( 0 );
2873}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002874
2875/*
2876 * To be called when the last message of an incoming flight is received.
2877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002879{
2880 /* We won't need to resend that one any more */
2881 ssl_flight_free( ssl->handshake->flight );
2882 ssl->handshake->flight = NULL;
2883 ssl->handshake->cur_msg = NULL;
2884
2885 /* The next incoming flight will start with this msg_seq */
2886 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2887
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002888 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002889 ssl_set_timer( ssl, 0 );
2890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2892 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002895 }
2896 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002898}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002899
2900/*
2901 * To be called when the last message of an outgoing flight is send.
2902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002904{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002905 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002906 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2909 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002912 }
2913 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002915}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002917
Paul Bakker5121ce52009-01-03 21:22:43 +00002918/*
2919 * Record layer functions
2920 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002921
2922/*
2923 * Write current record.
2924 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002927{
Nicholas Wilsonf0021642016-04-13 11:51:05 +01002928 int ret, done = 0, out_msg_type;
Paul Bakker23986e52011-04-24 08:57:21 +00002929 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002934 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002935 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002937 {
2938 ; /* Skip special handshake treatment when resending */
2939 }
2940 else
2941#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002943 {
Nicholas Wilsonf0021642016-04-13 11:51:05 +01002944 out_msg_type = ssl->out_msg[0];
2945
2946 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002947 ssl->handshake == NULL )
2948 {
2949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2950 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2951 }
2952
Paul Bakker5121ce52009-01-03 21:22:43 +00002953 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2954 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2955 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2956
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002957 /*
2958 * DTLS has additional fields in the Handshake layer,
2959 * between the length field and the actual payload:
2960 * uint16 message_seq;
2961 * uint24 fragment_offset;
2962 * uint24 fragment_length;
2963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002965 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002966 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002967 /* Make room for the additional DTLS fields */
Hanno Becker9648f8b2017-09-18 10:55:54 +01002968 if( MBEDTLS_SSL_MAX_CONTENT_LEN - ssl->out_msglen < 8 )
2969 {
2970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
2971 "size %u, maximum %u",
2972 (unsigned) ( ssl->in_hslen - 4 ),
2973 (unsigned) ( MBEDTLS_SSL_MAX_CONTENT_LEN - 12 ) ) );
2974 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2975 }
2976
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002977 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002978 ssl->out_msglen += 8;
2979 len += 8;
2980
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002981 /* Write message_seq and update it, except for HelloRequest */
Nicholas Wilsonf0021642016-04-13 11:51:05 +01002982 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002983 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002984 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2985 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2986 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002987 }
2988 else
2989 {
2990 ssl->out_msg[4] = 0;
2991 ssl->out_msg[5] = 0;
2992 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002993
2994 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2995 memset( ssl->out_msg + 6, 0x00, 3 );
2996 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002997 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002999
Nicholas Wilsonf0021642016-04-13 11:51:05 +01003000 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003001 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003002 }
3003
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003004 /* Save handshake and CCS messages for resending */
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é-Gonnardffa67be2014-09-19 11:18:57 +02003007 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008 ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING &&
3009 ( ssl->out_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ||
3010 ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003011 {
3012 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003015 return( ret );
3016 }
3017 }
3018#endif
3019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003020#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003021 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003023 {
3024 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003027 return( ret );
3028 }
3029
3030 len = ssl->out_msglen;
3031 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3035 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039 ret = mbedtls_ssl_hw_record_write( ssl );
3040 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3043 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003044 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003045
3046 if( ret == 0 )
3047 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003048 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003050 if( !done )
3051 {
3052 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003054 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003055
3056 ssl->out_len[0] = (unsigned char)( len >> 8 );
3057 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003058
Paul Bakker48916f92012-09-16 19:57:18 +00003059 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003060 {
3061 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003064 return( ret );
3065 }
3066
3067 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003068 ssl->out_len[0] = (unsigned char)( len >> 8 );
3069 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003070 }
3071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00003073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00003075 "version = [%d:%d], msglen = %d",
3076 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003077 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3080 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003081 }
3082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003086 return( ret );
3087 }
3088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003090
3091 return( 0 );
3092}
3093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003095/*
3096 * Mark bits in bitmask (used for DTLS HS reassembly)
3097 */
3098static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3099{
3100 unsigned int start_bits, end_bits;
3101
3102 start_bits = 8 - ( offset % 8 );
3103 if( start_bits != 8 )
3104 {
3105 size_t first_byte_idx = offset / 8;
3106
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003107 /* Special case */
3108 if( len <= start_bits )
3109 {
3110 for( ; len != 0; len-- )
3111 mask[first_byte_idx] |= 1 << ( start_bits - len );
3112
3113 /* Avoid potential issues with offset or len becoming invalid */
3114 return;
3115 }
3116
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003117 offset += start_bits; /* Now offset % 8 == 0 */
3118 len -= start_bits;
3119
3120 for( ; start_bits != 0; start_bits-- )
3121 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3122 }
3123
3124 end_bits = len % 8;
3125 if( end_bits != 0 )
3126 {
3127 size_t last_byte_idx = ( offset + len ) / 8;
3128
3129 len -= end_bits; /* Now len % 8 == 0 */
3130
3131 for( ; end_bits != 0; end_bits-- )
3132 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3133 }
3134
3135 memset( mask + offset / 8, 0xFF, len / 8 );
3136}
3137
3138/*
3139 * Check that bitmask is full
3140 */
3141static int ssl_bitmask_check( unsigned char *mask, size_t len )
3142{
3143 size_t i;
3144
3145 for( i = 0; i < len / 8; i++ )
3146 if( mask[i] != 0xFF )
3147 return( -1 );
3148
3149 for( i = 0; i < len % 8; i++ )
3150 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3151 return( -1 );
3152
3153 return( 0 );
3154}
3155
3156/*
3157 * Reassemble fragmented DTLS handshake messages.
3158 *
3159 * Use a temporary buffer for reassembly, divided in two parts:
3160 * - the first holds the reassembled message (including handshake header),
3161 * - the second holds a bitmask indicating which parts of the message
3162 * (excluding headers) have been received so far.
3163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003165{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003166 unsigned char *msg, *bitmask;
3167 size_t frag_len, frag_off;
3168 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3169
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003170 if( ssl->handshake == NULL )
3171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3173 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003174 }
3175
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003176 /*
3177 * For first fragment, check size and allocate buffer
3178 */
3179 if( ssl->handshake->hs_msg == NULL )
3180 {
3181 size_t alloc_len;
3182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003184 msg_len ) );
3185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186 if( ssl->in_hslen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3189 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003190 }
3191
3192 /* The bitmask needs one bit per byte of message excluding header */
3193 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3194
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003195 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003196 if( ssl->handshake->hs_msg == NULL )
3197 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003199 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003200 }
3201
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003202 /* Prepare final header: copy msg_type, length and message_seq,
3203 * then add standardised fragment_offset and fragment_length */
3204 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3205 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3206 memcpy( ssl->handshake->hs_msg + 9,
3207 ssl->handshake->hs_msg + 1, 3 );
3208 }
3209 else
3210 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003211 /* Make sure msg_type and length are consistent */
3212 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3215 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003216 }
3217 }
3218
3219 msg = ssl->handshake->hs_msg + 12;
3220 bitmask = msg + msg_len;
3221
3222 /*
3223 * Check and copy current fragment
3224 */
3225 frag_off = ( ssl->in_msg[6] << 16 ) |
3226 ( ssl->in_msg[7] << 8 ) |
3227 ssl->in_msg[8];
3228 frag_len = ( ssl->in_msg[9] << 16 ) |
3229 ( ssl->in_msg[10] << 8 ) |
3230 ssl->in_msg[11];
3231
3232 if( frag_off + frag_len > msg_len )
3233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003235 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003237 }
3238
3239 if( frag_len + 12 > ssl->in_msglen )
3240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003242 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003244 }
3245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003247 frag_off, frag_len ) );
3248
3249 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3250 ssl_bitmask_set( bitmask, frag_off, frag_len );
3251
3252 /*
3253 * Do we have the complete message by now?
3254 * If yes, finalize it, else ask to read the next record.
3255 */
3256 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003259 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003260 }
3261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003263
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003264 if( frag_len + 12 < ssl->in_msglen )
3265 {
3266 /*
3267 * We'got more handshake messages in the same record.
3268 * This case is not handled now because no know implementation does
3269 * that and it's hard to test, so we prefer to fail cleanly for now.
3270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3272 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003273 }
3274
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003275 if( ssl->in_left > ssl->next_record_offset )
3276 {
3277 /*
3278 * We've got more data in the buffer after the current record,
3279 * that we don't want to overwrite. Move it before writing the
3280 * reassembled message, and adjust in_left and next_record_offset.
3281 */
3282 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3283 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3284 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3285
3286 /* First compute and check new lengths */
3287 ssl->next_record_offset = new_remain - ssl->in_hdr;
3288 ssl->in_left = ssl->next_record_offset + remain_len;
3289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 if( ssl->in_left > MBEDTLS_SSL_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003291 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3294 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003295 }
3296
3297 memmove( new_remain, cur_remain, remain_len );
3298 }
3299
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003300 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3301
Hanno Beckerd82e0c02018-10-15 13:22:22 +01003302 mbedtls_zeroize( ssl->handshake->hs_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003304 ssl->handshake->hs_msg = NULL;
3305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003307 ssl->in_msg, ssl->in_hslen );
3308
3309 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003310}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003312
Simon Butcher99000142016-10-13 17:21:01 +01003313int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003314{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003318 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003320 }
3321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003323 ( ssl->in_msg[1] << 16 ) |
3324 ( ssl->in_msg[2] << 8 ) |
3325 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003328 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003329 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003332 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003333 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003334 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003335 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003336
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003337 /* ssl->handshake is NULL when receiving ClientHello for renego */
3338 if( ssl->handshake != NULL &&
3339 recv_msg_seq != ssl->handshake->in_msg_seq )
3340 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003341 /* Retransmit only on last message from previous flight, to avoid
3342 * too many retransmissions.
3343 * Besides, No sane server ever retransmits HelloVerifyRequest */
3344 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003348 "message_seq = %d, start_of_flight = %d",
3349 recv_msg_seq,
3350 ssl->handshake->in_flight_start_seq ) );
3351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003355 return( ret );
3356 }
3357 }
3358 else
3359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003361 "message_seq = %d, expected = %d",
3362 recv_msg_seq,
3363 ssl->handshake->in_msg_seq ) );
3364 }
3365
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003366 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003367 }
3368 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003369
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003370 /* Reassemble if current message is fragmented or reassembly is
3371 * already in progress */
3372 if( ssl->in_msglen < ssl->in_hslen ||
3373 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3374 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3375 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003378
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003379 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003382 return( ret );
3383 }
3384 }
3385 }
3386 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003388 /* With TLS we don't handle fragmentation (for now) */
3389 if( ssl->in_msglen < ssl->in_hslen )
3390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3392 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003393 }
3394
Simon Butcher99000142016-10-13 17:21:01 +01003395 return( 0 );
3396}
3397
3398void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3399{
3400
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003401 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3402 ssl->handshake != NULL )
3403 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003404 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003405 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003406
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003407 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003409 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003410 ssl->handshake != NULL )
3411 {
3412 ssl->handshake->in_msg_seq++;
3413 }
3414#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003415}
3416
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003417/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003418 * DTLS anti-replay: RFC 6347 4.1.2.6
3419 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003420 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3421 * Bit n is set iff record number in_window_top - n has been seen.
3422 *
3423 * Usually, in_window_top is the last record number seen and the lsb of
3424 * in_window is set. The only exception is the initial state (record number 0
3425 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3428static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003429{
3430 ssl->in_window_top = 0;
3431 ssl->in_window = 0;
3432}
3433
3434static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3435{
3436 return( ( (uint64_t) buf[0] << 40 ) |
3437 ( (uint64_t) buf[1] << 32 ) |
3438 ( (uint64_t) buf[2] << 24 ) |
3439 ( (uint64_t) buf[3] << 16 ) |
3440 ( (uint64_t) buf[4] << 8 ) |
3441 ( (uint64_t) buf[5] ) );
3442}
3443
3444/*
3445 * Return 0 if sequence number is acceptable, -1 otherwise
3446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003448{
3449 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3450 uint64_t bit;
3451
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003452 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003453 return( 0 );
3454
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003455 if( rec_seqnum > ssl->in_window_top )
3456 return( 0 );
3457
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003458 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003459
3460 if( bit >= 64 )
3461 return( -1 );
3462
3463 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3464 return( -1 );
3465
3466 return( 0 );
3467}
3468
3469/*
3470 * Update replay window on new validated record
3471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003473{
3474 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3475
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003476 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003477 return;
3478
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003479 if( rec_seqnum > ssl->in_window_top )
3480 {
3481 /* Update window_top and the contents of the window */
3482 uint64_t shift = rec_seqnum - ssl->in_window_top;
3483
3484 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003485 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003486 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003487 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003488 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003489 ssl->in_window |= 1;
3490 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003491
3492 ssl->in_window_top = rec_seqnum;
3493 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003494 else
3495 {
3496 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003497 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003498
3499 if( bit < 64 ) /* Always true, but be extra sure */
3500 ssl->in_window |= (uint64_t) 1 << bit;
3501 }
3502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003504
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003505#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003506/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003507static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3508
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003509/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003510 * Without any SSL context, check if a datagram looks like a ClientHello with
3511 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003512 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003513 *
3514 * - if cookie is valid, return 0
3515 * - if ClientHello looks superficially valid but cookie is not,
3516 * fill obuf and set olen, then
3517 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3518 * - otherwise return a specific error code
3519 */
3520static int ssl_check_dtls_clihlo_cookie(
3521 mbedtls_ssl_cookie_write_t *f_cookie_write,
3522 mbedtls_ssl_cookie_check_t *f_cookie_check,
3523 void *p_cookie,
3524 const unsigned char *cli_id, size_t cli_id_len,
3525 const unsigned char *in, size_t in_len,
3526 unsigned char *obuf, size_t buf_len, size_t *olen )
3527{
3528 size_t sid_len, cookie_len;
3529 unsigned char *p;
3530
3531 if( f_cookie_write == NULL || f_cookie_check == NULL )
3532 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3533
3534 /*
3535 * Structure of ClientHello with record and handshake headers,
3536 * and expected values. We don't need to check a lot, more checks will be
3537 * done when actually parsing the ClientHello - skipping those checks
3538 * avoids code duplication and does not make cookie forging any easier.
3539 *
3540 * 0-0 ContentType type; copied, must be handshake
3541 * 1-2 ProtocolVersion version; copied
3542 * 3-4 uint16 epoch; copied, must be 0
3543 * 5-10 uint48 sequence_number; copied
3544 * 11-12 uint16 length; (ignored)
3545 *
3546 * 13-13 HandshakeType msg_type; (ignored)
3547 * 14-16 uint24 length; (ignored)
3548 * 17-18 uint16 message_seq; copied
3549 * 19-21 uint24 fragment_offset; copied, must be 0
3550 * 22-24 uint24 fragment_length; (ignored)
3551 *
3552 * 25-26 ProtocolVersion client_version; (ignored)
3553 * 27-58 Random random; (ignored)
3554 * 59-xx SessionID session_id; 1 byte len + sid_len content
3555 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3556 * ...
3557 *
3558 * Minimum length is 61 bytes.
3559 */
3560 if( in_len < 61 ||
3561 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3562 in[3] != 0 || in[4] != 0 ||
3563 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3564 {
3565 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3566 }
3567
3568 sid_len = in[59];
3569 if( sid_len > in_len - 61 )
3570 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3571
3572 cookie_len = in[60 + sid_len];
3573 if( cookie_len > in_len - 60 )
3574 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3575
3576 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3577 cli_id, cli_id_len ) == 0 )
3578 {
3579 /* Valid cookie */
3580 return( 0 );
3581 }
3582
3583 /*
3584 * If we get here, we've got an invalid cookie, let's prepare HVR.
3585 *
3586 * 0-0 ContentType type; copied
3587 * 1-2 ProtocolVersion version; copied
3588 * 3-4 uint16 epoch; copied
3589 * 5-10 uint48 sequence_number; copied
3590 * 11-12 uint16 length; olen - 13
3591 *
3592 * 13-13 HandshakeType msg_type; hello_verify_request
3593 * 14-16 uint24 length; olen - 25
3594 * 17-18 uint16 message_seq; copied
3595 * 19-21 uint24 fragment_offset; copied
3596 * 22-24 uint24 fragment_length; olen - 25
3597 *
3598 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3599 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3600 *
3601 * Minimum length is 28.
3602 */
3603 if( buf_len < 28 )
3604 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3605
3606 /* Copy most fields and adapt others */
3607 memcpy( obuf, in, 25 );
3608 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3609 obuf[25] = 0xfe;
3610 obuf[26] = 0xff;
3611
3612 /* Generate and write actual cookie */
3613 p = obuf + 28;
3614 if( f_cookie_write( p_cookie,
3615 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3616 {
3617 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3618 }
3619
3620 *olen = p - obuf;
3621
3622 /* Go back and fill length fields */
3623 obuf[27] = (unsigned char)( *olen - 28 );
3624
3625 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3626 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3627 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3628
3629 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3630 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3631
3632 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3633}
3634
3635/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003636 * Handle possible client reconnect with the same UDP quadruplet
3637 * (RFC 6347 Section 4.2.8).
3638 *
3639 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3640 * that looks like a ClientHello.
3641 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003642 * - if the input looks like a ClientHello without cookies,
3643 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003644 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003645 * - if the input looks like a ClientHello with a valid cookie,
3646 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003647 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003648 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003649 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003650 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003651 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3652 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003653 */
3654static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3655{
3656 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003657 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003658
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003659 /* Use out_msg as temporary buffer for writing out HelloVerifyRequest,
3660 * because the output buffer's already around. Don't use out_buf though,
3661 * as we don't want to overwrite out_ctr. */
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003662 ret = ssl_check_dtls_clihlo_cookie(
3663 ssl->conf->f_cookie_write,
3664 ssl->conf->f_cookie_check,
3665 ssl->conf->p_cookie,
3666 ssl->cli_id, ssl->cli_id_len,
3667 ssl->in_buf, ssl->in_left,
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003668 ssl->out_msg, MBEDTLS_SSL_MAX_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003669
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003670 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3671
3672 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003673 {
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003674 int send_ret;
3675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3676 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003677 ssl->out_msg, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003678 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003679 * If the error is permanent we'll catch it later,
3680 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003681 send_ret = ssl->f_send( ssl->p_bio, ssl->out_msg, len );
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003682 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3683 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003684
3685 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003686 }
3687
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003688 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003689 {
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003691 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3692 {
3693 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3694 return( ret );
3695 }
3696
3697 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003698 }
3699
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003700 return( ret );
3701}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003702#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003703
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003704/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003705 * ContentType type;
3706 * ProtocolVersion version;
3707 * uint16 epoch; // DTLS only
3708 * uint48 sequence_number; // DTLS only
3709 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003710 *
3711 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003712 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003713 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3714 *
3715 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003716 * 1. proceed with the record if this function returns 0
3717 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3718 * 3. return CLIENT_RECONNECT if this function return that value
3719 * 4. drop the whole datagram if this function returns anything else.
3720 * Point 2 is needed when the peer is resending, and we have already received
3721 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003724{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003725 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 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 +02003728
Paul Bakker5121ce52009-01-03 21:22:43 +00003729 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003730 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003731 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003734 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003735 ssl->in_msgtype,
3736 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003737
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003738 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3740 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3741 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3742 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003745
3746#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01003747 /* Silently ignore invalid DTLS records as recommended by RFC 6347
3748 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003749 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3750#endif /* MBEDTLS_SSL_PROTO_DTLS */
3751 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3752 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
3753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003755 }
3756
3757 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003758 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3761 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003762 }
3763
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003764 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3767 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003768 }
3769
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003770 /* Check length against the size of our buffer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771 if( ssl->in_msglen > MBEDTLS_SSL_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003772 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3775 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003776 }
3777
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003778 /*
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003779 * DTLS-related tests.
3780 * Check epoch before checking length constraint because
3781 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3782 * message gets duplicated before the corresponding Finished message,
3783 * the second ChangeCipherSpec should be discarded because it belongs
3784 * to an old epoch, but not because its length is shorter than
3785 * the minimum record length for packets using the new record transform.
3786 * Note that these two kinds of failures are handled differently,
3787 * as an unexpected record is silently skipped but an invalid
3788 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003789 */
3790#if defined(MBEDTLS_SSL_PROTO_DTLS)
3791 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3792 {
3793 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3794
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003795 /* Check epoch (and sequence number) with DTLS */
3796 if( rec_epoch != ssl->in_epoch )
3797 {
3798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3799 "expected %d, received %d",
3800 ssl->in_epoch, rec_epoch ) );
3801
3802#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3803 /*
3804 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3805 * access the first byte of record content (handshake type), as we
3806 * have an active transform (possibly iv_len != 0), so use the
3807 * fact that the record header len is 13 instead.
3808 */
3809 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3810 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3811 rec_epoch == 0 &&
3812 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3813 ssl->in_left > 13 &&
3814 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3815 {
3816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3817 "from the same port" ) );
3818 return( ssl_handle_possible_reconnect( ssl ) );
3819 }
3820 else
3821#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3822 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3823 }
3824
3825#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3826 /* Replay detection only works for the current epoch */
3827 if( rec_epoch == ssl->in_epoch &&
3828 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3829 {
3830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3831 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3832 }
3833#endif
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003834
3835 /* Drop unexpected ChangeCipherSpec messages */
3836 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3837 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3838 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3839 {
3840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3841 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3842 }
3843
3844 /* Drop unexpected ApplicationData records,
3845 * except at the beginning of renegotiations */
3846 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
3847 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
3848#if defined(MBEDTLS_SSL_RENEGOTIATION)
3849 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
3850 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
3851#endif
3852 )
3853 {
3854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3855 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3856 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003857 }
3858#endif /* MBEDTLS_SSL_PROTO_DTLS */
3859
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003860
3861 /* Check length against bounds of the current transform and version */
3862 if( ssl->transform_in == NULL )
3863 {
3864 if( ssl->in_msglen < 1 ||
3865 ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
3866 {
3867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3868 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3869 }
3870 }
3871 else
3872 {
3873 if( ssl->in_msglen < ssl->transform_in->minlen )
3874 {
3875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3876 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3877 }
3878
3879#if defined(MBEDTLS_SSL_PROTO_SSL3)
3880 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3881 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_MAX_CONTENT_LEN )
3882 {
3883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3884 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3885 }
3886#endif
3887#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3888 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3889 /*
3890 * TLS encrypted messages can have up to 256 bytes of padding
3891 */
3892 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
3893 ssl->in_msglen > ssl->transform_in->minlen +
3894 MBEDTLS_SSL_MAX_CONTENT_LEN + 256 )
3895 {
3896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3897 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3898 }
3899#endif
3900 }
3901
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003902 return( 0 );
3903}
Paul Bakker5121ce52009-01-03 21:22:43 +00003904
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003905/*
3906 * If applicable, decrypt (and decompress) record content
3907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003909{
3910 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
3913 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003915#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3916 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920 ret = mbedtls_ssl_hw_record_read( ssl );
3921 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3924 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003925 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003926
3927 if( ret == 0 )
3928 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003929 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003930#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003931 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003932 {
3933 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003935 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003936 return( ret );
3937 }
3938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00003940 ssl->in_msg, ssl->in_msglen );
3941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003942 if( ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3945 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003946 }
3947 }
3948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003950 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003951 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003952 {
3953 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003956 return( ret );
3957 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00003958 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003962 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003965 }
3966#endif
3967
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003968 return( 0 );
3969}
3970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003972
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003973/*
3974 * Read a record.
3975 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003976 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3977 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3978 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003980int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003981{
3982 int ret;
3983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003985
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003986 if( ssl->keep_current_message == 0 )
3987 {
3988 do {
Simon Butcher99000142016-10-13 17:21:01 +01003989
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003990 if( ( ret = mbedtls_ssl_read_record_layer( ssl ) ) != 0 )
3991 {
3992 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
3993 return( ret );
3994 }
3995
3996 ret = mbedtls_ssl_handle_message_type( ssl );
3997
3998 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret );
3999
4000 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004001 {
4002 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4003 return( ret );
4004 }
4005
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004006 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4007 {
4008 mbedtls_ssl_update_handshake_status( ssl );
4009 }
Simon Butcher99000142016-10-13 17:21:01 +01004010 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004011 else
Simon Butcher99000142016-10-13 17:21:01 +01004012 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4014 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004015 }
4016
4017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4018
4019 return( 0 );
4020}
4021
4022int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4023{
4024 int ret;
4025
Hanno Becker4a810fb2017-05-24 16:27:30 +01004026 /*
4027 * Step A
4028 *
4029 * Consume last content-layer message and potentially
4030 * update in_msglen which keeps track of the contents'
4031 * consumption state.
4032 *
4033 * (1) Handshake messages:
4034 * Remove last handshake message, move content
4035 * and adapt in_msglen.
4036 *
4037 * (2) Alert messages:
4038 * Consume whole record content, in_msglen = 0.
4039 *
4040 * NOTE: This needs to be fixed, since like for
4041 * handshake messages it is allowed to have
4042 * multiple alerts witin a single record.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004043 * Internal reference IOTSSL-1321.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004044 *
4045 * (3) Change cipher spec:
4046 * Consume whole record content, in_msglen = 0.
4047 *
4048 * (4) Application data:
4049 * Don't do anything - the record layer provides
4050 * the application data as a stream transport
4051 * and consumes through mbedtls_ssl_read only.
4052 *
4053 */
4054
4055 /* Case (1): Handshake messages */
4056 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004057 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004058 /* Hard assertion to be sure that no application data
4059 * is in flight, as corrupting ssl->in_msglen during
4060 * ssl->in_offt != NULL is fatal. */
4061 if( ssl->in_offt != NULL )
4062 {
4063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4064 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4065 }
4066
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004067 /*
4068 * Get next Handshake message in the current record
4069 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004070
Hanno Becker4a810fb2017-05-24 16:27:30 +01004071 /* Notes:
4072 * (1) in_hslen is *NOT* necessarily the size of the
4073 * current handshake content: If DTLS handshake
4074 * fragmentation is used, that's the fragment
4075 * size instead. Using the total handshake message
4076 * size here is FAULTY and should be changed at
4077 * some point. Internal reference IOTSSL-1414.
4078 * (2) While it doesn't seem to cause problems, one
4079 * has to be very careful not to assume that in_hslen
4080 * is always <= in_msglen in a sensible communication.
4081 * Again, it's wrong for DTLS handshake fragmentation.
4082 * The following check is therefore mandatory, and
4083 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004084 * Additionally, ssl->in_hslen might be arbitrarily out of
4085 * bounds after handling a DTLS message with an unexpected
4086 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004087 */
4088 if( ssl->in_hslen < ssl->in_msglen )
4089 {
4090 ssl->in_msglen -= ssl->in_hslen;
4091 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4092 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004093
Hanno Becker4a810fb2017-05-24 16:27:30 +01004094 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4095 ssl->in_msg, ssl->in_msglen );
4096 }
4097 else
4098 {
4099 ssl->in_msglen = 0;
4100 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004101
Hanno Becker4a810fb2017-05-24 16:27:30 +01004102 ssl->in_hslen = 0;
4103 }
4104 /* Case (4): Application data */
4105 else if( ssl->in_offt != NULL )
4106 {
4107 return( 0 );
4108 }
4109 /* Everything else (CCS & Alerts) */
4110 else
4111 {
4112 ssl->in_msglen = 0;
4113 }
4114
4115 /*
4116 * Step B
4117 *
4118 * Fetch and decode new record if current one is fully consumed.
4119 *
4120 */
4121
4122 if( ssl->in_msglen > 0 )
4123 {
4124 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004125 return( 0 );
4126 }
4127
Hanno Becker4a810fb2017-05-24 16:27:30 +01004128 /* Need to fetch a new record */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004129
Simon Butcher99000142016-10-13 17:21:01 +01004130#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004131read_record_header:
Simon Butcher99000142016-10-13 17:21:01 +01004132#endif
4133
Hanno Becker4a810fb2017-05-24 16:27:30 +01004134 /* Current record either fully processed or to be discarded. */
4135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004138 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004139 return( ret );
4140 }
4141
4142 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004145 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4146 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004147 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004148 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4149 {
4150 /* Skip unexpected record (but not whole datagram) */
4151 ssl->next_record_offset = ssl->in_msglen
4152 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004153
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4155 "(header)" ) );
4156 }
4157 else
4158 {
4159 /* Skip invalid record and the rest of the datagram */
4160 ssl->next_record_offset = 0;
4161 ssl->in_left = 0;
4162
4163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4164 "(header)" ) );
4165 }
4166
4167 /* Get next record */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004168 goto read_record_header;
4169 }
4170#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004171 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004172 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004173
4174 /*
4175 * Read and optionally decrypt the message contents
4176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004177 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4178 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004181 return( ret );
4182 }
4183
4184 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004185#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004186 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004188 else
4189#endif
4190 ssl->in_left = 0;
4191
4192 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004194#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004195 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004196 {
4197 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4199 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004200 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004201 /* Except when waiting for Finished as a bad mac here
4202 * probably means something went wrong in the handshake
4203 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4204 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4205 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4206 {
4207#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4208 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4209 {
4210 mbedtls_ssl_send_alert_message( ssl,
4211 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4212 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4213 }
4214#endif
4215 return( ret );
4216 }
4217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004219 if( ssl->conf->badmac_limit != 0 &&
4220 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4223 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004224 }
4225#endif
4226
Hanno Becker4a810fb2017-05-24 16:27:30 +01004227 /* As above, invalid records cause
4228 * dismissal of the whole datagram. */
4229
4230 ssl->next_record_offset = 0;
4231 ssl->in_left = 0;
4232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004234 goto read_record_header;
4235 }
4236
4237 return( ret );
4238 }
4239 else
4240#endif
4241 {
4242 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4244 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004246 mbedtls_ssl_send_alert_message( ssl,
4247 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4248 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004249 }
4250#endif
4251 return( ret );
4252 }
4253 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004254
4255 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004256 * When we sent the last flight of the handshake, we MUST respond to a
4257 * retransmit of the peer's previous flight with a retransmit. (In
4258 * practice, only the Finished message will make it, other messages
4259 * including CCS use the old transform so they're dropped as invalid.)
4260 *
4261 * If the record we received is not a handshake message, however, it
4262 * means the peer received our last flight so we can clean up
4263 * handshake info.
4264 *
4265 * This check needs to be done before prepare_handshake() due to an edge
4266 * case: if the client immediately requests renegotiation, this
4267 * finishes the current handshake first, avoiding the new ClientHello
4268 * being mistaken for an ancient message in the current handshake.
4269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004271 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004272 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004273 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4276 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004283 return( ret );
4284 }
4285
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01004286 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004287 }
4288 else
4289 {
4290 ssl_handshake_wrapup_free_hs_transform( ssl );
4291 }
4292 }
4293#endif
4294
Simon Butcher99000142016-10-13 17:21:01 +01004295 return( 0 );
4296}
4297
4298int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4299{
4300 int ret;
4301
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004302 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004303 * Handle particular types of records
4304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004306 {
Simon Butcher99000142016-10-13 17:21:01 +01004307 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4308 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004309 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004310 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004311 }
4312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004313 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004314 {
Angus Gratton8946b0d2018-06-20 15:43:50 +10004315 if( ssl->in_msglen != 2 )
4316 {
4317 /* Note: Standard allows for more than one 2 byte alert
4318 to be packed in a single message, but Mbed TLS doesn't
4319 currently support this. */
4320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4321 ssl->in_msglen ) );
4322 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4323 }
4324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004326 ssl->in_msg[0], ssl->in_msg[1] ) );
4327
4328 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004329 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004334 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004335 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004336 }
4337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4339 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4342 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004343 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004344
4345#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4346 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4347 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4348 {
4349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4350 /* Will be handled when trying to parse ServerHello */
4351 return( 0 );
4352 }
4353#endif
4354
4355#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4356 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4357 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4358 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4359 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4360 {
4361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4362 /* Will be handled in mbedtls_ssl_parse_certificate() */
4363 return( 0 );
4364 }
4365#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4366
4367 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004368 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004369 }
4370
Paul Bakker5121ce52009-01-03 21:22:43 +00004371 return( 0 );
4372}
4373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004374int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004375{
4376 int ret;
4377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004378 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4379 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4380 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004381 {
4382 return( ret );
4383 }
4384
4385 return( 0 );
4386}
4387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004389 unsigned char level,
4390 unsigned char message )
4391{
4392 int ret;
4393
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004394 if( ssl == NULL || ssl->conf == NULL )
4395 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004398 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004401 ssl->out_msglen = 2;
4402 ssl->out_msg[0] = level;
4403 ssl->out_msg[1] = message;
4404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004405 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004407 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004408 return( ret );
4409 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004411
4412 return( 0 );
4413}
4414
Paul Bakker5121ce52009-01-03 21:22:43 +00004415/*
4416 * Handshake functions
4417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4419 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4420 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4421 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4422 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4423 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4424 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004425/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004426int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004427{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004428 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4433 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004434 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4435 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004438 ssl->state++;
4439 return( 0 );
4440 }
4441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4443 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004444}
4445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004446int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004447{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004448 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4453 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004454 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4455 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004458 ssl->state++;
4459 return( 0 );
4460 }
4461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4463 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004464}
Gilles Peskinef9828522017-05-03 12:28:43 +02004465
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004466#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004467/* Some certificate support -> implement write and parse */
4468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004470{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004472 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473 const mbedtls_x509_crt *crt;
4474 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004478 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4479 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004480 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4481 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004484 ssl->state++;
4485 return( 0 );
4486 }
4487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004488#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004489 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004490 {
4491 if( ssl->client_auth == 0 )
4492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004494 ssl->state++;
4495 return( 0 );
4496 }
4497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004498#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004499 /*
4500 * If using SSLv3 and got no cert, send an Alert message
4501 * (otherwise an empty Certificate message will be sent).
4502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004503 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4504 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004505 {
4506 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4508 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4509 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004512 goto write_msg;
4513 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004514#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004515 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516#endif /* MBEDTLS_SSL_CLI_C */
4517#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004518 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004520 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4523 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004524 }
4525 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004526#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004529
4530 /*
4531 * 0 . 0 handshake type
4532 * 1 . 3 handshake length
4533 * 4 . 6 length of all certs
4534 * 7 . 9 length of cert. 1
4535 * 10 . n-1 peer certificate
4536 * n . n+2 length of cert. 2
4537 * n+3 . ... upper level cert, etc.
4538 */
4539 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004540 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004541
Paul Bakker29087132010-03-21 21:03:34 +00004542 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004543 {
4544 n = crt->raw.len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004545 if( n > MBEDTLS_SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
4548 i + 3 + n, MBEDTLS_SSL_MAX_CONTENT_LEN ) );
4549 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004550 }
4551
4552 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4553 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4554 ssl->out_msg[i + 2] = (unsigned char)( n );
4555
4556 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4557 i += n; crt = crt->next;
4558 }
4559
4560 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4561 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4562 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4563
4564 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4566 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004567
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004568#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004569write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004570#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004571
4572 ssl->state++;
4573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004577 return( ret );
4578 }
4579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004581
Paul Bakkered27a042013-04-18 22:46:23 +02004582 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004583}
4584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004585int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004586{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004587 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004588 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004589 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004590 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004591 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4596 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004597 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4598 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004601 ssl->state++;
4602 return( 0 );
4603 }
4604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004605#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004606 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004607 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4608 {
4609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4610 ssl->state++;
4611 return( 0 );
4612 }
4613
4614#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4615 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4616 authmode = ssl->handshake->sni_authmode;
4617#endif
4618
4619 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4620 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004621 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004622 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004624 ssl->state++;
4625 return( 0 );
4626 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004627#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004630 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004631 /* mbedtls_ssl_read_record may have sent an alert already. We
4632 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004633 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004634 return( ret );
4635 }
4636
4637 ssl->state++;
4638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639#if defined(MBEDTLS_SSL_SRV_C)
4640#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004641 /*
4642 * Check if the client sent an empty certificate
4643 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004644 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004645 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004646 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004647 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4649 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4650 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004653
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004654 /* The client was asked for a certificate but didn't send
4655 one. The client should know what's going on, so we
4656 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004657 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004658 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004659 return( 0 );
4660 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004661 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004662 }
4663 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004664#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004666#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4667 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004668 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004669 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004671 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4672 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4673 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4674 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004677
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004678 /* The client was asked for a certificate but didn't send
4679 one. The client should know what's going on, so we
4680 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004681 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004682 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004683 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004684 else
4685 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004686 }
4687 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004688#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4689 MBEDTLS_SSL_PROTO_TLS1_2 */
4690#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004692 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004695 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4696 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004697 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004698 }
4699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004700 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4701 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004704 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4705 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004706 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004707 }
4708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004709 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004710
Paul Bakker5121ce52009-01-03 21:22:43 +00004711 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004713 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004714 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004715
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004716 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004720 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4721 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004722 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004723 }
4724
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004725 /* In case we tried to reuse a session but it failed */
4726 if( ssl->session_negotiate->peer_cert != NULL )
4727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4729 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004730 }
4731
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004732 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004733 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004734 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004737 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4738 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004739 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004740 }
4741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004743
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004744 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004745
4746 while( i < ssl->in_hslen )
4747 {
Philippe Antoine33e5c322018-07-09 10:39:02 +02004748 if ( i + 3 > ssl->in_hslen ) {
4749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4750 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4751 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4752 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
4753 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004754 if( ssl->in_msg[i] != 0 )
4755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004757 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4758 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004760 }
4761
4762 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4763 | (unsigned int) ssl->in_msg[i + 2];
4764 i += 3;
4765
4766 if( n < 128 || i + n > ssl->in_hslen )
4767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004769 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4770 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004771 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004772 }
4773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004774 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004775 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004776 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004777 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004778 case 0: /*ok*/
4779 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
4780 /* Ignore certificate with an unknown algorithm: maybe a
4781 prior certificate was already trusted. */
4782 break;
4783
4784 case MBEDTLS_ERR_X509_ALLOC_FAILED:
4785 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
4786 goto crt_parse_der_failed;
4787
4788 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
4789 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4790 goto crt_parse_der_failed;
4791
4792 default:
4793 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4794 crt_parse_der_failed:
4795 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004797 return( ret );
4798 }
4799
4800 i += n;
4801 }
4802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004803 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004804
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004805 /*
4806 * On client, make sure the server cert doesn't change during renego to
4807 * avoid "triple handshake" attack: https://secure-resumption.com/
4808 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004810 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004812 {
4813 if( ssl->session->peer_cert == NULL )
4814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004816 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4817 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004819 }
4820
4821 if( ssl->session->peer_cert->raw.len !=
4822 ssl->session_negotiate->peer_cert->raw.len ||
4823 memcmp( ssl->session->peer_cert->raw.p,
4824 ssl->session_negotiate->peer_cert->raw.p,
4825 ssl->session->peer_cert->raw.len ) != 0 )
4826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004828 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4829 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004830 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004831 }
4832 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004834
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004835 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004836 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004837 mbedtls_x509_crt *ca_chain;
4838 mbedtls_x509_crl *ca_crl;
4839
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004840#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004841 if( ssl->handshake->sni_ca_chain != NULL )
4842 {
4843 ca_chain = ssl->handshake->sni_ca_chain;
4844 ca_crl = ssl->handshake->sni_ca_crl;
4845 }
4846 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004847#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004848 {
4849 ca_chain = ssl->conf->ca_chain;
4850 ca_crl = ssl->conf->ca_crl;
4851 }
4852
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004853 /*
4854 * Main check: verify certificate
4855 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004856 ret = mbedtls_x509_crt_verify_with_profile(
4857 ssl->session_negotiate->peer_cert,
4858 ca_chain, ca_crl,
4859 ssl->conf->cert_profile,
4860 ssl->hostname,
4861 &ssl->session_negotiate->verify_result,
4862 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004863
4864 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004866 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004867 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004868
4869 /*
4870 * Secondary checks: always done, but change 'ret' only if it was 0
4871 */
4872
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004873#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004875 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004876
4877 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004878 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004879 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004880 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004881 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
4882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004884 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004885 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004886 }
4887 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004888#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004890 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004891 ciphersuite_info,
4892 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004893 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004896 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004897 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004898 }
4899
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004900 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
4901 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
4902 * with details encoded in the verification flags. All other kinds
4903 * of error codes, including those from the user provided f_vrfy
4904 * functions, are treated as fatal and lead to a failure of
4905 * ssl_parse_certificate even if verification was optional. */
4906 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
4907 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
4908 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
4909 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004910 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004911 }
4912
4913 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
4914 {
4915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
4916 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
4917 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004918
4919 if( ret != 0 )
4920 {
4921 /* The certificate may have been rejected for several reasons.
4922 Pick one and send the corresponding alert. Which alert to send
4923 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004924 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
4925 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
4926 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
4927 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4928 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
4929 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4930 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
4931 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4932 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
4933 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4934 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
4935 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4936 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
4937 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4938 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
4939 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
4940 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
4941 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
4942 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
4943 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02004944 else
4945 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004946 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4947 alert );
4948 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004949
Hanno Beckere6706e62017-05-15 16:05:15 +01004950#if defined(MBEDTLS_DEBUG_C)
4951 if( ssl->session_negotiate->verify_result != 0 )
4952 {
4953 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
4954 ssl->session_negotiate->verify_result ) );
4955 }
4956 else
4957 {
4958 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
4959 }
4960#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004961 }
4962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004964
4965 return( ret );
4966}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004967#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
4968 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
4969 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
4970 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4971 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4972 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
4973 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004975int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004976{
4977 int ret;
4978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004981 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004982 ssl->out_msglen = 1;
4983 ssl->out_msg[0] = 1;
4984
Paul Bakker5121ce52009-01-03 21:22:43 +00004985 ssl->state++;
4986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004987 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004990 return( ret );
4991 }
4992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004994
4995 return( 0 );
4996}
4997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004998int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004999{
5000 int ret;
5001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005004 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005007 return( ret );
5008 }
5009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005010 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005013 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5014 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005016 }
5017
5018 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005021 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5022 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005024 }
5025
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005026 /*
5027 * Switch to our negotiated transform and session parameters for inbound
5028 * data.
5029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005031 ssl->transform_in = ssl->transform_negotiate;
5032 ssl->session_in = ssl->session_negotiate;
5033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005034#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005035 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005037#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005038 ssl_dtls_replay_reset( ssl );
5039#endif
5040
5041 /* Increment epoch */
5042 if( ++ssl->in_epoch == 0 )
5043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005045 /* This is highly unlikely to happen for legitimate reasons, so
5046 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005047 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005048 }
5049 }
5050 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005051#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005052 memset( ssl->in_ctr, 0, 8 );
5053
5054 /*
5055 * Set the in_msg pointer to the correct location based on IV length
5056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005057 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005058 {
5059 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
5060 ssl->transform_negotiate->fixed_ivlen;
5061 }
5062 else
5063 ssl->in_msg = ssl->in_iv;
5064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5066 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005068 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005071 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5072 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005074 }
5075 }
5076#endif
5077
Paul Bakker5121ce52009-01-03 21:22:43 +00005078 ssl->state++;
5079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005081
5082 return( 0 );
5083}
5084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005085void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5086 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005087{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005088 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005090#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5091 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5092 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005093 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005094 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005095#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005096#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5097#if defined(MBEDTLS_SHA512_C)
5098 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005099 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5100 else
5101#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005102#if defined(MBEDTLS_SHA256_C)
5103 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005104 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005105 else
5106#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005107#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005110 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005111 }
Paul Bakker380da532012-04-18 16:10:25 +00005112}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005114void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005115{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005116#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5117 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005118 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5119 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5122#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005123 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005124#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005126 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005127#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005128#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005129}
5130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005131static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005132 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005134#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5135 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005136 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5137 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005138#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005139#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5140#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005141 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005142#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005143#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005144 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005145#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005146#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005147}
5148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005149#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5150 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5151static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005152 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005153{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005154 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5155 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005156}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005157#endif
Paul Bakker380da532012-04-18 16:10:25 +00005158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005159#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5160#if defined(MBEDTLS_SHA256_C)
5161static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005162 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005163{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005164 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005165}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005166#endif
Paul Bakker380da532012-04-18 16:10:25 +00005167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005168#if defined(MBEDTLS_SHA512_C)
5169static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005170 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005171{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005172 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005173}
Paul Bakker769075d2012-11-24 11:26:46 +01005174#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005175#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005177#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005178static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005179 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005180{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005181 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005182 mbedtls_md5_context md5;
5183 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005184
Paul Bakker5121ce52009-01-03 21:22:43 +00005185 unsigned char padbuf[48];
5186 unsigned char md5sum[16];
5187 unsigned char sha1sum[20];
5188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005189 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005190 if( !session )
5191 session = ssl->session;
5192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005194
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005195 mbedtls_md5_init( &md5 );
5196 mbedtls_sha1_init( &sha1 );
5197
5198 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5199 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005200
5201 /*
5202 * SSLv3:
5203 * hash =
5204 * MD5( master + pad2 +
5205 * MD5( handshake + sender + master + pad1 ) )
5206 * + SHA1( master + pad2 +
5207 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005208 */
5209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005210#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005211 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5212 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005213#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005216 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5217 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005218#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005221 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005222
Paul Bakker1ef83d62012-04-11 12:09:53 +00005223 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005224
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005225 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5226 mbedtls_md5_update_ret( &md5, session->master, 48 );
5227 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5228 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005229
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005230 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5231 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5232 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5233 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005234
Paul Bakker1ef83d62012-04-11 12:09:53 +00005235 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005236
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005237 mbedtls_md5_starts_ret( &md5 );
5238 mbedtls_md5_update_ret( &md5, session->master, 48 );
5239 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5240 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5241 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005242
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005243 mbedtls_sha1_starts_ret( &sha1 );
5244 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5245 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5246 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5247 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005249 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005250
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005251 mbedtls_md5_free( &md5 );
5252 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005254 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
5255 mbedtls_zeroize( md5sum, sizeof( md5sum ) );
5256 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005259}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005260#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005262#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005263static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005265{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005266 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005267 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005268 mbedtls_md5_context md5;
5269 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005270 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005272 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005273 if( !session )
5274 session = ssl->session;
5275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005277
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005278 mbedtls_md5_init( &md5 );
5279 mbedtls_sha1_init( &sha1 );
5280
5281 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5282 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005283
Paul Bakker1ef83d62012-04-11 12:09:53 +00005284 /*
5285 * TLSv1:
5286 * hash = PRF( master, finished_label,
5287 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5288 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005290#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005291 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5292 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005293#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005296 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5297 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005298#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005300 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005301 ? "client finished"
5302 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005303
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005304 mbedtls_md5_finish_ret( &md5, padbuf );
5305 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005306
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005307 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005308 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005310 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005311
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005312 mbedtls_md5_free( &md5 );
5313 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005315 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005318}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005321#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5322#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005323static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005324 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005325{
5326 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005327 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005328 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005329 unsigned char padbuf[32];
5330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005332 if( !session )
5333 session = ssl->session;
5334
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005335 mbedtls_sha256_init( &sha256 );
5336
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005338
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005339 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005340
5341 /*
5342 * TLSv1.2:
5343 * hash = PRF( master, finished_label,
5344 * Hash( handshake ) )[0.11]
5345 */
5346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347#if !defined(MBEDTLS_SHA256_ALT)
5348 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005349 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005350#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005352 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005353 ? "client finished"
5354 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005355
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005356 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005357
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005358 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005359 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005362
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005363 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005365 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005371#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005372static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005374{
5375 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005376 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005377 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005378 unsigned char padbuf[48];
5379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005380 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005381 if( !session )
5382 session = ssl->session;
5383
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005384 mbedtls_sha512_init( &sha512 );
5385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005387
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005388 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005389
5390 /*
5391 * TLSv1.2:
5392 * hash = PRF( master, finished_label,
5393 * Hash( handshake ) )[0.11]
5394 */
5395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005397 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5398 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005399#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005402 ? "client finished"
5403 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005404
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005405 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005406
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005407 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005408 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005411
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005412 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005414 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005417}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418#endif /* MBEDTLS_SHA512_C */
5419#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005422{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005423 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005424
5425 /*
5426 * Free our handshake params
5427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428 mbedtls_ssl_handshake_free( ssl->handshake );
5429 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005430 ssl->handshake = NULL;
5431
5432 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005433 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005434 */
5435 if( ssl->transform )
5436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437 mbedtls_ssl_transform_free( ssl->transform );
5438 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005439 }
5440 ssl->transform = ssl->transform_negotiate;
5441 ssl->transform_negotiate = NULL;
5442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005443 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005444}
5445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005447{
5448 int resume = ssl->handshake->resume;
5449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005450 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452#if defined(MBEDTLS_SSL_RENEGOTIATION)
5453 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005456 ssl->renego_records_seen = 0;
5457 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005458#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005459
5460 /*
5461 * Free the previous session and switch in the current one
5462 */
Paul Bakker0a597072012-09-25 21:55:46 +00005463 if( ssl->session )
5464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005466 /* RFC 7366 3.1: keep the EtM state */
5467 ssl->session_negotiate->encrypt_then_mac =
5468 ssl->session->encrypt_then_mac;
5469#endif
5470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471 mbedtls_ssl_session_free( ssl->session );
5472 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005473 }
5474 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005475 ssl->session_negotiate = NULL;
5476
Paul Bakker0a597072012-09-25 21:55:46 +00005477 /*
5478 * Add cache entry
5479 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005480 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005481 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005482 resume == 0 )
5483 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005484 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005486 }
Paul Bakker0a597072012-09-25 21:55:46 +00005487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005489 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005490 ssl->handshake->flight != NULL )
5491 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005492 /* Cancel handshake timer */
5493 ssl_set_timer( ssl, 0 );
5494
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005495 /* Keep last flight around in case we need to resend it:
5496 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005498 }
5499 else
5500#endif
5501 ssl_handshake_wrapup_free_hs_transform( ssl );
5502
Paul Bakker48916f92012-09-16 19:57:18 +00005503 ssl->state++;
5504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005506}
5507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005509{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005510 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005513
Paul Bakker92be97b2013-01-02 17:30:03 +01005514 /*
5515 * Set the out_msg pointer to the correct location based on IV length
5516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005517 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005518 {
5519 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5520 ssl->transform_negotiate->fixed_ivlen;
5521 }
5522 else
5523 ssl->out_msg = ssl->out_iv;
5524
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005525 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005526
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005527 /*
5528 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5529 * may define some other value. Currently (early 2016), no defined
5530 * ciphersuite does this (and this is unlikely to change as activity has
5531 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005533 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005536 ssl->verify_data_len = hash_len;
5537 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005538#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005539
Paul Bakker5121ce52009-01-03 21:22:43 +00005540 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005541 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5542 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005543
5544 /*
5545 * In case of session resuming, invert the client and server
5546 * ChangeCipherSpec messages order.
5547 */
Paul Bakker0a597072012-09-25 21:55:46 +00005548 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005550#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005551 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005552 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005553#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005554#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005555 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005557#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005558 }
5559 else
5560 ssl->state++;
5561
Paul Bakker48916f92012-09-16 19:57:18 +00005562 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005563 * Switch to our negotiated transform and session parameters for outbound
5564 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005568#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005569 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005570 {
5571 unsigned char i;
5572
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005573 /* Remember current epoch settings for resending */
5574 ssl->handshake->alt_transform_out = ssl->transform_out;
5575 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5576
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005577 /* Set sequence_number to zero */
5578 memset( ssl->out_ctr + 2, 0, 6 );
5579
5580 /* Increment epoch */
5581 for( i = 2; i > 0; i-- )
5582 if( ++ssl->out_ctr[i - 1] != 0 )
5583 break;
5584
5585 /* The loop goes to its end iff the counter is wrapping */
5586 if( i == 0 )
5587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5589 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005590 }
5591 }
5592 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005594 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005595
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005596 ssl->transform_out = ssl->transform_negotiate;
5597 ssl->session_out = ssl->session_negotiate;
5598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5600 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5605 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005606 }
5607 }
5608#endif
5609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005611 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005613#endif
5614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005618 return( ret );
5619 }
5620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005622
5623 return( 0 );
5624}
5625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005626#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005627#define SSL_MAX_HASH_LEN 36
5628#else
5629#define SSL_MAX_HASH_LEN 12
5630#endif
5631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005633{
Paul Bakker23986e52011-04-24 08:57:21 +00005634 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005635 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005636 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005639
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005640 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005642 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005645 return( ret );
5646 }
5647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005651 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5652 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005653 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005654 }
5655
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005656 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005657#if defined(MBEDTLS_SSL_PROTO_SSL3)
5658 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005659 hash_len = 36;
5660 else
5661#endif
5662 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5665 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005668 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5669 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005671 }
5672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005674 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005677 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5678 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005680 }
5681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005683 ssl->verify_data_len = hash_len;
5684 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005685#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005686
Paul Bakker0a597072012-09-25 21:55:46 +00005687 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005690 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005692#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005693#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005694 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005696#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005697 }
5698 else
5699 ssl->state++;
5700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005702 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005703 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005704#endif
5705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005707
5708 return( 0 );
5709}
5710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005711static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005712{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005713 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5716 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5717 mbedtls_md5_init( &handshake->fin_md5 );
5718 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005719 mbedtls_md5_starts_ret( &handshake->fin_md5 );
5720 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005721#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5723#if defined(MBEDTLS_SHA256_C)
5724 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005725 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005726#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727#if defined(MBEDTLS_SHA512_C)
5728 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005729 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005730#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005732
5733 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005734
5735#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5736 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5737 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5738#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740#if defined(MBEDTLS_DHM_C)
5741 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005742#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743#if defined(MBEDTLS_ECDH_C)
5744 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005745#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02005746#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005747 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02005748#if defined(MBEDTLS_SSL_CLI_C)
5749 handshake->ecjpake_cache = NULL;
5750 handshake->ecjpake_cache_len = 0;
5751#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005752#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005753
5754#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5755 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5756#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005757}
5758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005760{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005761 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5764 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005766 mbedtls_md_init( &transform->md_ctx_enc );
5767 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005768}
5769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005771{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005772 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005773}
5774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005776{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005777 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005778 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005780 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005781 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005782 if( ssl->handshake )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005783 mbedtls_ssl_handshake_free( ssl->handshake );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005784
5785 /*
5786 * Either the pointers are now NULL or cleared properly and can be freed.
5787 * Now allocate missing structures.
5788 */
5789 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005790 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005791 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005792 }
Paul Bakker48916f92012-09-16 19:57:18 +00005793
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005794 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005795 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005796 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005797 }
Paul Bakker48916f92012-09-16 19:57:18 +00005798
Paul Bakker82788fb2014-10-20 13:59:19 +02005799 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005800 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005801 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005802 }
Paul Bakker48916f92012-09-16 19:57:18 +00005803
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005804 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005805 if( ssl->handshake == NULL ||
5806 ssl->transform_negotiate == NULL ||
5807 ssl->session_negotiate == NULL )
5808 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811 mbedtls_free( ssl->handshake );
5812 mbedtls_free( ssl->transform_negotiate );
5813 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005814
5815 ssl->handshake = NULL;
5816 ssl->transform_negotiate = NULL;
5817 ssl->session_negotiate = NULL;
5818
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005819 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005820 }
5821
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005822 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005823 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005824 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005825 ssl_handshake_params_init( ssl->handshake );
5826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005828 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5829 {
5830 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005831
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005832 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5833 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5834 else
5835 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005836
5837 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005838 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005839#endif
5840
Paul Bakker48916f92012-09-16 19:57:18 +00005841 return( 0 );
5842}
5843
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005844#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005845/* Dummy cookie callbacks for defaults */
5846static int ssl_cookie_write_dummy( void *ctx,
5847 unsigned char **p, unsigned char *end,
5848 const unsigned char *cli_id, size_t cli_id_len )
5849{
5850 ((void) ctx);
5851 ((void) p);
5852 ((void) end);
5853 ((void) cli_id);
5854 ((void) cli_id_len);
5855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005857}
5858
5859static int ssl_cookie_check_dummy( void *ctx,
5860 const unsigned char *cookie, size_t cookie_len,
5861 const unsigned char *cli_id, size_t cli_id_len )
5862{
5863 ((void) ctx);
5864 ((void) cookie);
5865 ((void) cookie_len);
5866 ((void) cli_id);
5867 ((void) cli_id_len);
5868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005870}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005871#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005872
Paul Bakker5121ce52009-01-03 21:22:43 +00005873/*
5874 * Initialize an SSL context
5875 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005876void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
5877{
5878 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
5879}
5880
5881/*
5882 * Setup an SSL context
5883 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005884int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02005885 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00005886{
Paul Bakker48916f92012-09-16 19:57:18 +00005887 int ret;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005888 const size_t len = MBEDTLS_SSL_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005889
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005890 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00005891
5892 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005893 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00005894 */
k-stachowiakc2eddee2018-07-09 10:39:20 +02005895 ssl->in_buf = NULL;
5896 ssl->out_buf = NULL;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005897 if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL ||
5898 ( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005899 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", len ) );
k-stachowiak2c161142018-07-31 16:52:32 +02005901 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiakc2eddee2018-07-09 10:39:20 +02005902 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005903 }
5904
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005905#if defined(MBEDTLS_SSL_PROTO_DTLS)
5906 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5907 {
5908 ssl->out_hdr = ssl->out_buf;
5909 ssl->out_ctr = ssl->out_buf + 3;
5910 ssl->out_len = ssl->out_buf + 11;
5911 ssl->out_iv = ssl->out_buf + 13;
5912 ssl->out_msg = ssl->out_buf + 13;
5913
5914 ssl->in_hdr = ssl->in_buf;
5915 ssl->in_ctr = ssl->in_buf + 3;
5916 ssl->in_len = ssl->in_buf + 11;
5917 ssl->in_iv = ssl->in_buf + 13;
5918 ssl->in_msg = ssl->in_buf + 13;
5919 }
5920 else
5921#endif
5922 {
5923 ssl->out_ctr = ssl->out_buf;
5924 ssl->out_hdr = ssl->out_buf + 8;
5925 ssl->out_len = ssl->out_buf + 11;
5926 ssl->out_iv = ssl->out_buf + 13;
5927 ssl->out_msg = ssl->out_buf + 13;
5928
5929 ssl->in_ctr = ssl->in_buf;
5930 ssl->in_hdr = ssl->in_buf + 8;
5931 ssl->in_len = ssl->in_buf + 11;
5932 ssl->in_iv = ssl->in_buf + 13;
5933 ssl->in_msg = ssl->in_buf + 13;
5934 }
5935
Paul Bakker48916f92012-09-16 19:57:18 +00005936 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiakc2eddee2018-07-09 10:39:20 +02005937 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005938
5939 return( 0 );
k-stachowiakc2eddee2018-07-09 10:39:20 +02005940
5941error:
5942 mbedtls_free( ssl->in_buf );
5943 mbedtls_free( ssl->out_buf );
5944
5945 ssl->conf = NULL;
5946
5947 ssl->in_buf = NULL;
5948 ssl->out_buf = NULL;
5949
5950 ssl->in_hdr = NULL;
5951 ssl->in_ctr = NULL;
5952 ssl->in_len = NULL;
5953 ssl->in_iv = NULL;
5954 ssl->in_msg = NULL;
5955
5956 ssl->out_hdr = NULL;
5957 ssl->out_ctr = NULL;
5958 ssl->out_len = NULL;
5959 ssl->out_iv = NULL;
5960 ssl->out_msg = NULL;
5961
k-stachowiak2c161142018-07-31 16:52:32 +02005962 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005963}
5964
5965/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00005966 * Reset an initialized and used SSL context for re-use while retaining
5967 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005968 *
5969 * If partial is non-zero, keep data in the input buffer and client ID.
5970 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00005971 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005972static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00005973{
Paul Bakker48916f92012-09-16 19:57:18 +00005974 int ret;
5975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005977
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005978 /* Cancel any possibly running timer */
5979 ssl_set_timer( ssl, 0 );
5980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981#if defined(MBEDTLS_SSL_RENEGOTIATION)
5982 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005983 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005984
5985 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
5987 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005988#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005989 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00005990
Paul Bakker7eb013f2011-10-06 12:37:39 +00005991 ssl->in_offt = NULL;
5992
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005993 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005994 ssl->in_msgtype = 0;
5995 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005996 if( partial == 0 )
5997 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005999 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006000 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006001#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006003 ssl_dtls_replay_reset( ssl );
6004#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006005
6006 ssl->in_hslen = 0;
6007 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006008
6009 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006010
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006011 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006012 ssl->out_msgtype = 0;
6013 ssl->out_msglen = 0;
6014 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6016 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006017 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006018#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006019
Paul Bakker48916f92012-09-16 19:57:18 +00006020 ssl->transform_in = NULL;
6021 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006022
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006023 ssl->session_in = NULL;
6024 ssl->session_out = NULL;
6025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026 memset( ssl->out_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006027
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006028 if( partial == 0 )
6029 memset( ssl->in_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00006030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6032 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6035 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006037 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6038 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006039 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006040 }
6041#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006042
Paul Bakker48916f92012-09-16 19:57:18 +00006043 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006045 mbedtls_ssl_transform_free( ssl->transform );
6046 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006047 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006048 }
Paul Bakker48916f92012-09-16 19:57:18 +00006049
Paul Bakkerc0463502013-02-14 11:19:38 +01006050 if( ssl->session )
6051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052 mbedtls_ssl_session_free( ssl->session );
6053 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006054 ssl->session = NULL;
6055 }
6056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006058 ssl->alpn_chosen = NULL;
6059#endif
6060
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006061#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006062 if( partial == 0 )
6063 {
6064 mbedtls_free( ssl->cli_id );
6065 ssl->cli_id = NULL;
6066 ssl->cli_id_len = 0;
6067 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006068#endif
6069
Paul Bakker48916f92012-09-16 19:57:18 +00006070 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6071 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006072
6073 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006074}
6075
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006076/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006077 * Reset an initialized and used SSL context for re-use while retaining
6078 * all application-set variables, function pointers and data.
6079 */
6080int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6081{
6082 return( ssl_session_reset_int( ssl, 0 ) );
6083}
6084
6085/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006086 * SSL set accessors
6087 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006088void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006089{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006090 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006091}
6092
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006093void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006094{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006095 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006096}
6097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006098#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006099void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006100{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006101 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006102}
6103#endif
6104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006106void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006107{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006108 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006109}
6110#endif
6111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006113void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006114{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006115 conf->hs_timeout_min = min;
6116 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006117}
6118#endif
6119
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006120void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006121{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006122 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006123}
6124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006125#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006126void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006127 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006128 void *p_vrfy )
6129{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006130 conf->f_vrfy = f_vrfy;
6131 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006132}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006133#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006134
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006135void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006136 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006137 void *p_rng )
6138{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006139 conf->f_rng = f_rng;
6140 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006141}
6142
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006143void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006144 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006145 void *p_dbg )
6146{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006147 conf->f_dbg = f_dbg;
6148 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006149}
6150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006151void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006152 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006153 mbedtls_ssl_send_t *f_send,
6154 mbedtls_ssl_recv_t *f_recv,
6155 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006156{
6157 ssl->p_bio = p_bio;
6158 ssl->f_send = f_send;
6159 ssl->f_recv = f_recv;
6160 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006161}
6162
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006163void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006164{
6165 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006166}
6167
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006168void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6169 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006170 mbedtls_ssl_set_timer_t *f_set_timer,
6171 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006172{
6173 ssl->p_timer = p_timer;
6174 ssl->f_set_timer = f_set_timer;
6175 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006176
6177 /* Make sure we start with no timer running */
6178 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006179}
6180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006181#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006182void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006183 void *p_cache,
6184 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6185 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006186{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006187 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006188 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006189 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006191#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193#if defined(MBEDTLS_SSL_CLI_C)
6194int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006195{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006196 int ret;
6197
6198 if( ssl == NULL ||
6199 session == NULL ||
6200 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006201 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006204 }
6205
6206 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6207 return( ret );
6208
Paul Bakker0a597072012-09-25 21:55:46 +00006209 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006210
6211 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006212}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006214
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006215void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006216 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006217{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006218 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6219 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6220 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6221 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006222}
6223
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006224void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006225 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006226 int major, int minor )
6227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006229 return;
6230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006232 return;
6233
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006234 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006235}
6236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006238void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006239 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006240{
6241 conf->cert_profile = profile;
6242}
6243
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006244/* Append a new keycert entry to a (possibly empty) list */
6245static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6246 mbedtls_x509_crt *cert,
6247 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006248{
niisatoa35dbf12018-06-25 19:05:48 +09006249 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006250
niisatoa35dbf12018-06-25 19:05:48 +09006251 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6252 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006253 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006254
niisatoa35dbf12018-06-25 19:05:48 +09006255 new_cert->cert = cert;
6256 new_cert->key = key;
6257 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006258
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006259 /* Update head is the list was null, else add to the end */
6260 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006261 {
niisatoa35dbf12018-06-25 19:05:48 +09006262 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006263 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006264 else
6265 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006266 mbedtls_ssl_key_cert *cur = *head;
6267 while( cur->next != NULL )
6268 cur = cur->next;
niisatoa35dbf12018-06-25 19:05:48 +09006269 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006270 }
6271
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006272 return( 0 );
6273}
6274
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006275int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006276 mbedtls_x509_crt *own_cert,
6277 mbedtls_pk_context *pk_key )
6278{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006279 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006280}
6281
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006282void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006283 mbedtls_x509_crt *ca_chain,
6284 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006285{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006286 conf->ca_chain = ca_chain;
6287 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006288}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006290
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006291#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6292int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6293 mbedtls_x509_crt *own_cert,
6294 mbedtls_pk_context *pk_key )
6295{
6296 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6297 own_cert, pk_key ) );
6298}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006299
6300void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6301 mbedtls_x509_crt *ca_chain,
6302 mbedtls_x509_crl *ca_crl )
6303{
6304 ssl->handshake->sni_ca_chain = ca_chain;
6305 ssl->handshake->sni_ca_crl = ca_crl;
6306}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006307
6308void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6309 int authmode )
6310{
6311 ssl->handshake->sni_authmode = authmode;
6312}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006313#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6314
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006315#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006316/*
6317 * Set EC J-PAKE password for current handshake
6318 */
6319int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6320 const unsigned char *pw,
6321 size_t pw_len )
6322{
6323 mbedtls_ecjpake_role role;
6324
Janos Follath8eb64132016-06-03 15:40:57 +01006325 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006326 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6327
6328 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6329 role = MBEDTLS_ECJPAKE_SERVER;
6330 else
6331 role = MBEDTLS_ECJPAKE_CLIENT;
6332
6333 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6334 role,
6335 MBEDTLS_MD_SHA256,
6336 MBEDTLS_ECP_DP_SECP256R1,
6337 pw, pw_len ) );
6338}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006339#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006342int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006343 const unsigned char *psk, size_t psk_len,
6344 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006345{
Paul Bakker6db455e2013-09-18 17:29:31 +02006346 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006349 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6350 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006351
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006352 /* Identity len will be encoded on two bytes */
6353 if( ( psk_identity_len >> 16 ) != 0 ||
6354 psk_identity_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
6355 {
6356 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6357 }
6358
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006359 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006360 {
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006361 mbedtls_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006362
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006363 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006364 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006365 conf->psk_len = 0;
6366 }
6367 if( conf->psk_identity != NULL )
6368 {
6369 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006370 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006371 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006372 }
6373
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006374 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6375 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006376 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006377 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006378 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006379 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006380 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006381 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006382 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006383
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006384 conf->psk_len = psk_len;
6385 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006386
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006387 memcpy( conf->psk, psk, conf->psk_len );
6388 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006389
6390 return( 0 );
6391}
6392
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006393int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6394 const unsigned char *psk, size_t psk_len )
6395{
6396 if( psk == NULL || ssl->handshake == NULL )
6397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6398
6399 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6400 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6401
6402 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006403 {
6404 mbedtls_zeroize( ssl->handshake->psk, ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006405 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006406 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006407 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006408
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006409 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006410 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006411
6412 ssl->handshake->psk_len = psk_len;
6413 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6414
6415 return( 0 );
6416}
6417
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006418void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006419 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006420 size_t),
6421 void *p_psk )
6422{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006423 conf->f_psk = f_psk;
6424 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006425}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006427
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006428#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006429
6430#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006431int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006432{
6433 int ret;
6434
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006435 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6436 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6437 {
6438 mbedtls_mpi_free( &conf->dhm_P );
6439 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006440 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006441 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006442
6443 return( 0 );
6444}
Hanno Becker470a8c42017-10-04 15:28:46 +01006445#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006446
Hanno Beckera90658f2017-10-04 15:29:08 +01006447int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6448 const unsigned char *dhm_P, size_t P_len,
6449 const unsigned char *dhm_G, size_t G_len )
6450{
6451 int ret;
6452
6453 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6454 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6455 {
6456 mbedtls_mpi_free( &conf->dhm_P );
6457 mbedtls_mpi_free( &conf->dhm_G );
6458 return( ret );
6459 }
6460
6461 return( 0 );
6462}
Paul Bakker5121ce52009-01-03 21:22:43 +00006463
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006464int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006465{
6466 int ret;
6467
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006468 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6469 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6470 {
6471 mbedtls_mpi_free( &conf->dhm_P );
6472 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006473 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006474 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006475
6476 return( 0 );
6477}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006478#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006479
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006480#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6481/*
6482 * Set the minimum length for Diffie-Hellman parameters
6483 */
6484void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6485 unsigned int bitlen )
6486{
6487 conf->dhm_min_bitlen = bitlen;
6488}
6489#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6490
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006491#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006492/*
6493 * Set allowed/preferred hashes for handshake signatures
6494 */
6495void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6496 const int *hashes )
6497{
6498 conf->sig_hashes = hashes;
6499}
Hanno Becker947194e2017-04-07 13:25:49 +01006500#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006501
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006502#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006503/*
6504 * Set the allowed elliptic curves
6505 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006506void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006507 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006508{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006509 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006510}
Hanno Becker947194e2017-04-07 13:25:49 +01006511#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006512
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006513#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006514int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006515{
Hanno Becker947194e2017-04-07 13:25:49 +01006516 /* Initialize to suppress unnecessary compiler warning */
6517 size_t hostname_len = 0;
6518
6519 /* Check if new hostname is valid before
6520 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006521 if( hostname != NULL )
6522 {
6523 hostname_len = strlen( hostname );
6524
6525 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6527 }
6528
6529 /* Now it's clear that we will overwrite the old hostname,
6530 * so we can free it safely */
6531
6532 if( ssl->hostname != NULL )
6533 {
6534 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
6535 mbedtls_free( ssl->hostname );
6536 }
6537
6538 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006539
Paul Bakker5121ce52009-01-03 21:22:43 +00006540 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006541 {
6542 ssl->hostname = NULL;
6543 }
6544 else
6545 {
6546 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006547 if( ssl->hostname == NULL )
6548 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006549
Hanno Becker947194e2017-04-07 13:25:49 +01006550 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006551
Hanno Becker947194e2017-04-07 13:25:49 +01006552 ssl->hostname[hostname_len] = '\0';
6553 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006554
6555 return( 0 );
6556}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006557#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006558
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006559#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006560void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006562 const unsigned char *, size_t),
6563 void *p_sni )
6564{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006565 conf->f_sni = f_sni;
6566 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006567}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006571int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006572{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006573 size_t cur_len, tot_len;
6574 const char **p;
6575
6576 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006577 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6578 * MUST NOT be truncated."
6579 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006580 */
6581 tot_len = 0;
6582 for( p = protos; *p != NULL; p++ )
6583 {
6584 cur_len = strlen( *p );
6585 tot_len += cur_len;
6586
Ronald Crona32236c2020-04-23 16:41:44 +02006587 if( ( cur_len == 0 ) ||
6588 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
6589 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006591 }
6592
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006593 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006594
6595 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006596}
6597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006599{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006600 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006601}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006603
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006604void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006605{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006606 conf->max_major_ver = major;
6607 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006608}
6609
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006610void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006611{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006612 conf->min_major_ver = major;
6613 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006614}
6615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006616#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006617void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006618{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006619 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006620}
6621#endif
6622
Janos Follath088ce432017-04-10 12:42:31 +01006623#if defined(MBEDTLS_SSL_SRV_C)
6624void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6625 char cert_req_ca_list )
6626{
6627 conf->cert_req_ca_list = cert_req_ca_list;
6628}
6629#endif
6630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006632void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006633{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006634 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006635}
6636#endif
6637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006639void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006640{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006641 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006642}
6643#endif
6644
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006645#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006646void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006647{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006648 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006649}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006650#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006653int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006654{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
6656 mfl_code_to_length[mfl_code] > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006659 }
6660
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006661 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006662
6663 return( 0 );
6664}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006668void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006669{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006670 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006671}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006675void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006676{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006677 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006678}
6679#endif
6680
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006681void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006682{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006683 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006684}
6685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006687void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006688{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006689 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006690}
6691
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006692void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006693{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006694 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006695}
6696
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006697void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006698 const unsigned char period[8] )
6699{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006700 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006705#if defined(MBEDTLS_SSL_CLI_C)
6706void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006707{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006708 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006709}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006710#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006711
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006712#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006713void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6714 mbedtls_ssl_ticket_write_t *f_ticket_write,
6715 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6716 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006717{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006718 conf->f_ticket_write = f_ticket_write;
6719 conf->f_ticket_parse = f_ticket_parse;
6720 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006721}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006722#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006724
Robert Cragie4feb7ae2015-10-02 13:33:37 +01006725#if defined(MBEDTLS_SSL_EXPORT_KEYS)
6726void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
6727 mbedtls_ssl_export_keys_t *f_export_keys,
6728 void *p_export_keys )
6729{
6730 conf->f_export_keys = f_export_keys;
6731 conf->p_export_keys = p_export_keys;
6732}
6733#endif
6734
Paul Bakker5121ce52009-01-03 21:22:43 +00006735/*
6736 * SSL get accessors
6737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006738size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006739{
6740 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6741}
6742
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006743uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006744{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006745 if( ssl->session != NULL )
6746 return( ssl->session->verify_result );
6747
6748 if( ssl->session_negotiate != NULL )
6749 return( ssl->session_negotiate->verify_result );
6750
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006751 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006752}
6753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006755{
Paul Bakker926c8e42013-03-06 10:23:34 +01006756 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006757 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006760}
6761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006763{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006765 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006766 {
6767 switch( ssl->minor_ver )
6768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006770 return( "DTLSv1.0" );
6771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006773 return( "DTLSv1.2" );
6774
6775 default:
6776 return( "unknown (DTLS)" );
6777 }
6778 }
6779#endif
6780
Paul Bakker43ca69c2011-01-15 17:35:19 +00006781 switch( ssl->minor_ver )
6782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006784 return( "SSLv3.0" );
6785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006787 return( "TLSv1.0" );
6788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006790 return( "TLSv1.1" );
6791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00006793 return( "TLSv1.2" );
6794
Paul Bakker43ca69c2011-01-15 17:35:19 +00006795 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006796 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00006797 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00006798}
6799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006801{
Hanno Becker12f7ede2018-08-17 15:28:19 +01006802 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006804 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006805
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006806 if( transform == NULL )
6807 return( (int) mbedtls_ssl_hdr_len( ssl ) );
6808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809#if defined(MBEDTLS_ZLIB_SUPPORT)
6810 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
6811 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006812#endif
6813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006816 case MBEDTLS_MODE_GCM:
6817 case MBEDTLS_MODE_CCM:
6818 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006819 transform_expansion = transform->minlen;
6820 break;
6821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006822 case MBEDTLS_MODE_CBC:
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006823
6824 block_size = mbedtls_cipher_get_block_size(
6825 &transform->cipher_ctx_enc );
6826
Hanno Becker12f7ede2018-08-17 15:28:19 +01006827 /* Expansion due to the addition of the MAC. */
6828 transform_expansion += transform->maclen;
6829
6830 /* Expansion due to the addition of CBC padding;
6831 * Theoretically up to 256 bytes, but we never use
6832 * more than the block size of the underlying cipher. */
6833 transform_expansion += block_size;
6834
6835 /* For TLS 1.1 or higher, an explicit IV is added
6836 * after the record header. */
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006837#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
6838 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker12f7ede2018-08-17 15:28:19 +01006839 transform_expansion += block_size;
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006840#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker12f7ede2018-08-17 15:28:19 +01006841
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006842 break;
6843
6844 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006847 }
6848
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006849 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006850}
6851
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02006852#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
6853size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
6854{
6855 size_t max_len;
6856
6857 /*
6858 * Assume mfl_code is correct since it was checked when set
6859 */
6860 max_len = mfl_code_to_length[ssl->conf->mfl_code];
6861
6862 /*
6863 * Check if a smaller max length was negotiated
6864 */
6865 if( ssl->session_out != NULL &&
6866 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6867 {
6868 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6869 }
6870
6871 return max_len;
6872}
6873#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
6874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875#if defined(MBEDTLS_X509_CRT_PARSE_C)
6876const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00006877{
6878 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006879 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006880
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006881 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006882}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00006884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885#if defined(MBEDTLS_SSL_CLI_C)
6886int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006887{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006888 if( ssl == NULL ||
6889 dst == NULL ||
6890 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006891 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006894 }
6895
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006896 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006897}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006899
Paul Bakker5121ce52009-01-03 21:22:43 +00006900/*
Paul Bakker1961b702013-01-25 14:49:24 +01006901 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00006902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006904{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006906
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006907 if( ssl == NULL || ssl->conf == NULL )
6908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006911 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006913#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006915 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006917#endif
6918
Paul Bakker1961b702013-01-25 14:49:24 +01006919 return( ret );
6920}
6921
6922/*
6923 * Perform the SSL handshake
6924 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01006926{
6927 int ret = 0;
6928
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006929 if( ssl == NULL || ssl->conf == NULL )
6930 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01006933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006934 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01006935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006936 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01006937
6938 if( ret != 0 )
6939 break;
6940 }
6941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006943
6944 return( ret );
6945}
6946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947#if defined(MBEDTLS_SSL_RENEGOTIATION)
6948#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006949/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006950 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00006951 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006953{
6954 int ret;
6955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006957
6958 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6960 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006965 return( ret );
6966 }
6967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006969
6970 return( 0 );
6971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006973
6974/*
6975 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976 * - any side: calling mbedtls_ssl_renegotiate(),
6977 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
6978 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02006979 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006980 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006982 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006984{
6985 int ret;
6986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006988
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006989 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6990 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006991
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006992 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
6993 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006995 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006997 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006998 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006999 ssl->handshake->out_msg_seq = 1;
7000 else
7001 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007002 }
7003#endif
7004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7006 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007011 return( ret );
7012 }
7013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007015
7016 return( 0 );
7017}
7018
7019/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007020 * Renegotiate current connection on client,
7021 * or request renegotiation on server
7022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007024{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007026
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007027 if( ssl == NULL || ssl->conf == NULL )
7028 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007031 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007032 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007034 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7035 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007038
7039 /* Did we already try/start sending HelloRequest? */
7040 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007042
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007043 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007044 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007048 /*
7049 * On client, either start the renegotiation process or,
7050 * if already in progress, continue the handshake
7051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7055 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007056
7057 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007060 return( ret );
7061 }
7062 }
7063 else
7064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007065 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007068 return( ret );
7069 }
7070 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007072
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007073 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007074}
7075
7076/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007077 * Check record counters and renegotiate if they're above the limit.
7078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007080{
Andres AG2196c7f2016-12-15 17:01:16 +00007081 size_t ep_len = ssl_ep_len( ssl );
7082 int in_ctr_cmp;
7083 int out_ctr_cmp;
7084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7086 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007087 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007088 {
7089 return( 0 );
7090 }
7091
Andres AG2196c7f2016-12-15 17:01:16 +00007092 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7093 ssl->conf->renego_period + ep_len, 8 - ep_len );
7094 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
7095 ssl->conf->renego_period + ep_len, 8 - ep_len );
7096
7097 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007098 {
7099 return( 0 );
7100 }
7101
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007103 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007104}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007106
7107/*
7108 * Receive application data decrypted from the SSL layer
7109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007111{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007112 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007113 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007114
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007115 if( ssl == NULL || ssl->conf == NULL )
7116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007121 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007124 return( ret );
7125
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007126 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007130 return( ret );
7131 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007132 }
7133#endif
7134
Hanno Becker4a810fb2017-05-24 16:27:30 +01007135 /*
7136 * Check if renegotiation is necessary and/or handshake is
7137 * in process. If yes, perform/continue, and fall through
7138 * if an unexpected packet is received while the client
7139 * is waiting for the ServerHello.
7140 *
7141 * (There is no equivalent to the last condition on
7142 * the server-side as it is not treated as within
7143 * a handshake while waiting for the ClientHello
7144 * after a renegotiation request.)
7145 */
7146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007148 ret = ssl_check_ctr_renegotiate( ssl );
7149 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7150 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007152 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007153 return( ret );
7154 }
7155#endif
7156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007160 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7161 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007164 return( ret );
7165 }
7166 }
7167
7168 if( ssl->in_offt == NULL )
7169 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007170 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007171 if( ssl->f_get_timer != NULL &&
7172 ssl->f_get_timer( ssl->p_timer ) == -1 )
7173 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007174 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007175 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007176
Hanno Becker4a810fb2017-05-24 16:27:30 +01007177 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007178 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007179 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7180 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007181
Hanno Becker4a810fb2017-05-24 16:27:30 +01007182 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7183 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007184 }
7185
7186 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007188 {
7189 /*
7190 * OpenSSL sends empty messages to randomize the IV
7191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007195 return( 0 );
7196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007198 return( ret );
7199 }
7200 }
7201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007205
Hanno Becker4a810fb2017-05-24 16:27:30 +01007206 /*
7207 * - For client-side, expect SERVER_HELLO_REQUEST.
7208 * - For server-side, expect CLIENT_HELLO.
7209 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7210 */
7211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007213 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007215 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007218
7219 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007221 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007222 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007223#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007225 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007226#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007227
Hanno Becker4a810fb2017-05-24 16:27:30 +01007228#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007229 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007233
7234 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007236 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007237 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007238#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007239 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007240 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007241#endif /* MBEDTLS_SSL_SRV_C */
7242
Hanno Becker21df7f92017-10-17 11:03:26 +01007243#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007244 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007245 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7246 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7247 ssl->conf->allow_legacy_renegotiation ==
7248 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7249 {
7250 /*
7251 * Accept renegotiation request
7252 */
Paul Bakker48916f92012-09-16 19:57:18 +00007253
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007254 /* DTLS clients need to know renego is server-initiated */
7255#if defined(MBEDTLS_SSL_PROTO_DTLS)
7256 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7257 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7258 {
7259 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7260 }
7261#endif
7262 ret = ssl_start_renegotiation( ssl );
7263 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7264 ret != 0 )
7265 {
7266 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7267 return( ret );
7268 }
7269 }
7270 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007271#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007272 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007273 /*
7274 * Refuse renegotiation
7275 */
7276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279#if defined(MBEDTLS_SSL_PROTO_SSL3)
7280 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007281 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007282 /* SSLv3 does not have a "no_renegotiation" warning, so
7283 we send a fatal alert and abort the connection. */
7284 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7285 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7286 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007287 }
7288 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7290#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7291 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7292 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7295 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7296 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007297 {
7298 return( ret );
7299 }
Paul Bakker48916f92012-09-16 19:57:18 +00007300 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007301 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7303 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7306 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007307 }
Paul Bakker48916f92012-09-16 19:57:18 +00007308 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007309
Hanno Becker4a810fb2017-05-24 16:27:30 +01007310 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00007311 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007312#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007314 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007315 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007316 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007317 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007320 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007322 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007323 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007324 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7328 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007331 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007332 }
7333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7337 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007338 }
7339
7340 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007341
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007342 /* We're going to return something now, cancel timer,
7343 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007345 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007346
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007347#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007348 /* If we requested renego but received AppData, resend HelloRequest.
7349 * Do it now, after setting in_offt, to avoid taking this branch
7350 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007351#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007352 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007354 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007355 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007358 return( ret );
7359 }
7360 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007361#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007362#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007363 }
7364
7365 n = ( len < ssl->in_msglen )
7366 ? len : ssl->in_msglen;
7367
7368 memcpy( buf, ssl->in_offt, n );
7369 ssl->in_msglen -= n;
7370
7371 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007372 {
7373 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007374 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007375 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007376 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007377 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007378 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007379 /* more data available */
7380 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007384
Paul Bakker23986e52011-04-24 08:57:21 +00007385 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007386}
7387
7388/*
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007389 * Send application data to be encrypted by the SSL layer, taking care of max
7390 * fragment length and buffer size.
7391 *
7392 * According to RFC 5246 Section 6.2.1:
7393 *
7394 * Zero-length fragments of Application data MAY be sent as they are
7395 * potentially useful as a traffic analysis countermeasure.
7396 *
7397 * Therefore, it is possible that the input message length is 0 and the
7398 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007399 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007400static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007401 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007402{
Paul Bakker23986e52011-04-24 08:57:21 +00007403 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007405 size_t max_len = mbedtls_ssl_get_max_frag_len( ssl );
Florin0b7b83f2017-07-22 09:01:44 +02007406#else
7407 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
7408#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007409 if( len > max_len )
7410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007412 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007415 "maximum fragment length: %d > %d",
7416 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007418 }
7419 else
7420#endif
7421 len = max_len;
7422 }
Paul Bakker887bd502011-06-08 13:10:54 +00007423
Paul Bakker5121ce52009-01-03 21:22:43 +00007424 if( ssl->out_left != 0 )
7425 {
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007426 /*
7427 * The user has previously tried to send the data and
7428 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7429 * written. In this case, we expect the high-level write function
7430 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007435 return( ret );
7436 }
7437 }
Paul Bakker887bd502011-06-08 13:10:54 +00007438 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007439 {
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007440 /*
7441 * The user is trying to send a message the first time, so we need to
7442 * copy the data into the internal buffers and setup the data structure
7443 * to keep track of partial writes
7444 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007445 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007447 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007452 return( ret );
7453 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007454 }
7455
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007456 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007457}
7458
7459/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007460 * Write application data, doing 1/n-1 splitting if necessary.
7461 *
7462 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007463 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01007464 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007467static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007468 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007469{
7470 int ret;
7471
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007472 if( ssl->conf->cbc_record_splitting ==
7473 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007474 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007475 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7476 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7477 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007478 {
7479 return( ssl_write_real( ssl, buf, len ) );
7480 }
7481
7482 if( ssl->split_done == 0 )
7483 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007484 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007485 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007486 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007487 }
7488
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007489 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7490 return( ret );
7491 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007492
7493 return( ret + 1 );
7494}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007496
7497/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007498 * Write application data (public-facing wrapper)
7499 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007500int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007501{
7502 int ret;
7503
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007505
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007506 if( ssl == NULL || ssl->conf == NULL )
7507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7508
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007509#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007510 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7511 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007512 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007513 return( ret );
7514 }
7515#endif
7516
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007517 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007518 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007519 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007520 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007521 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007522 return( ret );
7523 }
7524 }
7525
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007526#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007527 ret = ssl_write_split( ssl, buf, len );
7528#else
7529 ret = ssl_write_real( ssl, buf, len );
7530#endif
7531
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007533
7534 return( ret );
7535}
7536
7537/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007538 * Notify the peer that the connection is being closed
7539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007541{
7542 int ret;
7543
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007544 if( ssl == NULL || ssl->conf == NULL )
7545 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007548
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007549 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007550 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007552 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7555 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7556 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007559 return( ret );
7560 }
7561 }
7562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007564
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007565 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007566}
7567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00007569{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007570 if( transform == NULL )
7571 return;
7572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00007574 deflateEnd( &transform->ctx_deflate );
7575 inflateEnd( &transform->ctx_inflate );
7576#endif
7577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578 mbedtls_cipher_free( &transform->cipher_ctx_enc );
7579 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02007580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581 mbedtls_md_free( &transform->md_ctx_enc );
7582 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02007583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584 mbedtls_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007585}
7586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587#if defined(MBEDTLS_X509_CRT_PARSE_C)
7588static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007589{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007591
7592 while( cur != NULL )
7593 {
7594 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007595 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007596 cur = next;
7597 }
7598}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake )
Paul Bakker48916f92012-09-16 19:57:18 +00007602{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007603 if( handshake == NULL )
7604 return;
7605
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007606#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7607 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7608 mbedtls_md5_free( &handshake->fin_md5 );
7609 mbedtls_sha1_free( &handshake->fin_sha1 );
7610#endif
7611#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7612#if defined(MBEDTLS_SHA256_C)
7613 mbedtls_sha256_free( &handshake->fin_sha256 );
7614#endif
7615#if defined(MBEDTLS_SHA512_C)
7616 mbedtls_sha512_free( &handshake->fin_sha512 );
7617#endif
7618#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007620#if defined(MBEDTLS_DHM_C)
7621 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007622#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623#if defined(MBEDTLS_ECDH_C)
7624 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007625#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007626#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007627 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007628#if defined(MBEDTLS_SSL_CLI_C)
7629 mbedtls_free( handshake->ecjpake_cache );
7630 handshake->ecjpake_cache = NULL;
7631 handshake->ecjpake_cache_len = 0;
7632#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007633#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02007634
Janos Follath4ae5c292016-02-10 11:27:43 +00007635#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
7636 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02007637 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007639#endif
7640
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007641#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7642 if( handshake->psk != NULL )
7643 {
7644 mbedtls_zeroize( handshake->psk, handshake->psk_len );
7645 mbedtls_free( handshake->psk );
7646 }
7647#endif
7648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7650 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007651 /*
7652 * Free only the linked list wrapper, not the keys themselves
7653 * since the belong to the SNI callback
7654 */
7655 if( handshake->sni_key_cert != NULL )
7656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007657 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007658
7659 while( cur != NULL )
7660 {
7661 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007663 cur = next;
7664 }
7665 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668#if defined(MBEDTLS_SSL_PROTO_DTLS)
7669 mbedtls_free( handshake->verify_cookie );
7670 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007671 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007672#endif
7673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674 mbedtls_zeroize( handshake, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007675}
7676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007678{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007679 if( session == NULL )
7680 return;
7681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007682#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007683 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685 mbedtls_x509_crt_free( session->peer_cert );
7686 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007687 }
Paul Bakkered27a042013-04-18 22:46:23 +02007688#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007689
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007690#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007692#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694 mbedtls_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007695}
7696
Paul Bakker5121ce52009-01-03 21:22:43 +00007697/*
7698 * Free an SSL context
7699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007701{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007702 if( ssl == NULL )
7703 return;
7704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007706
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007707 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709 mbedtls_zeroize( ssl->out_buf, MBEDTLS_SSL_BUFFER_LEN );
7710 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007711 }
7712
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007713 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007715 mbedtls_zeroize( ssl->in_buf, MBEDTLS_SSL_BUFFER_LEN );
7716 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007717 }
7718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02007720 if( ssl->compress_buf != NULL )
7721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722 mbedtls_zeroize( ssl->compress_buf, MBEDTLS_SSL_BUFFER_LEN );
7723 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02007724 }
7725#endif
7726
Paul Bakker48916f92012-09-16 19:57:18 +00007727 if( ssl->transform )
7728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 mbedtls_ssl_transform_free( ssl->transform );
7730 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007731 }
7732
7733 if( ssl->handshake )
7734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735 mbedtls_ssl_handshake_free( ssl->handshake );
7736 mbedtls_ssl_transform_free( ssl->transform_negotiate );
7737 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739 mbedtls_free( ssl->handshake );
7740 mbedtls_free( ssl->transform_negotiate );
7741 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007742 }
7743
Paul Bakkerc0463502013-02-14 11:19:38 +01007744 if( ssl->session )
7745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007746 mbedtls_ssl_session_free( ssl->session );
7747 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007748 }
7749
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02007750#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02007751 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007752 {
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007753 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00007755 }
Paul Bakker0be444a2013-08-27 21:55:01 +02007756#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7759 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
7762 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00007763 }
7764#endif
7765
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007766#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007768#endif
7769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00007771
Paul Bakker86f04f42013-02-14 11:20:09 +01007772 /* Actually clear after last debug message */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773 mbedtls_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007774}
7775
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007776/*
7777 * Initialze mbedtls_ssl_config
7778 */
7779void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
7780{
7781 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
7782}
7783
Simon Butcherc97b6972015-12-27 23:48:17 +00007784#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007785static int ssl_preset_default_hashes[] = {
7786#if defined(MBEDTLS_SHA512_C)
7787 MBEDTLS_MD_SHA512,
7788 MBEDTLS_MD_SHA384,
7789#endif
7790#if defined(MBEDTLS_SHA256_C)
7791 MBEDTLS_MD_SHA256,
7792 MBEDTLS_MD_SHA224,
7793#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02007794#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007795 MBEDTLS_MD_SHA1,
7796#endif
7797 MBEDTLS_MD_NONE
7798};
Simon Butcherc97b6972015-12-27 23:48:17 +00007799#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007800
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007801static int ssl_preset_suiteb_ciphersuites[] = {
7802 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
7803 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
7804 0
7805};
7806
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007807#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007808static int ssl_preset_suiteb_hashes[] = {
7809 MBEDTLS_MD_SHA256,
7810 MBEDTLS_MD_SHA384,
7811 MBEDTLS_MD_NONE
7812};
7813#endif
7814
7815#if defined(MBEDTLS_ECP_C)
7816static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007817#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007818 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007819#endif
7820#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007821 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007822#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007823 MBEDTLS_ECP_DP_NONE
7824};
7825#endif
7826
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007827/*
Tillmann Karras588ad502015-09-25 04:27:22 +02007828 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007829 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007830int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007831 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007832{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007833#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007834 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007835#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007836
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02007837 /* Use the functions here so that they are covered in tests,
7838 * but otherwise access member directly for efficiency */
7839 mbedtls_ssl_conf_endpoint( conf, endpoint );
7840 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007841
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007842 /*
7843 * Things that are common to all presets
7844 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007845#if defined(MBEDTLS_SSL_CLI_C)
7846 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7847 {
7848 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
7849#if defined(MBEDTLS_SSL_SESSION_TICKETS)
7850 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
7851#endif
7852 }
7853#endif
7854
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007855#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007856 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007857#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007858
7859#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7860 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
7861#endif
7862
7863#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7864 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
7865#endif
7866
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007867#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7868 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7869#endif
7870
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007871#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007872 conf->f_cookie_write = ssl_cookie_write_dummy;
7873 conf->f_cookie_check = ssl_cookie_check_dummy;
7874#endif
7875
7876#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7877 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7878#endif
7879
Janos Follath088ce432017-04-10 12:42:31 +01007880#if defined(MBEDTLS_SSL_SRV_C)
7881 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
7882#endif
7883
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007884#if defined(MBEDTLS_SSL_PROTO_DTLS)
7885 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7886 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7887#endif
7888
7889#if defined(MBEDTLS_SSL_RENEGOTIATION)
7890 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00007891 memset( conf->renego_period, 0x00, 2 );
7892 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007893#endif
7894
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007895#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
7896 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7897 {
Hanno Becker00d0a682017-10-04 13:14:29 +01007898 const unsigned char dhm_p[] =
7899 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
7900 const unsigned char dhm_g[] =
7901 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
7902
Hanno Beckera90658f2017-10-04 15:29:08 +01007903 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
7904 dhm_p, sizeof( dhm_p ),
7905 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007906 {
7907 return( ret );
7908 }
7909 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007910#endif
7911
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007912 /*
7913 * Preset-specific defaults
7914 */
7915 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007916 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007917 /*
7918 * NSA Suite B
7919 */
7920 case MBEDTLS_SSL_PRESET_SUITEB:
7921 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7922 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7923 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7924 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7925
7926 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7927 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7928 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7929 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7930 ssl_preset_suiteb_ciphersuites;
7931
7932#if defined(MBEDTLS_X509_CRT_PARSE_C)
7933 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007934#endif
7935
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007936#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007937 conf->sig_hashes = ssl_preset_suiteb_hashes;
7938#endif
7939
7940#if defined(MBEDTLS_ECP_C)
7941 conf->curve_list = ssl_preset_suiteb_curves;
7942#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007943 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007944
7945 /*
7946 * Default
7947 */
7948 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03007949 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
7950 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
7951 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7952 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7953 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
7954 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
7955 MBEDTLS_SSL_MIN_MINOR_VERSION :
7956 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007957 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7958 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7959
7960#if defined(MBEDTLS_SSL_PROTO_DTLS)
7961 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7962 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
7963#endif
7964
7965 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7966 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7967 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7968 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7969 mbedtls_ssl_list_ciphersuites();
7970
7971#if defined(MBEDTLS_X509_CRT_PARSE_C)
7972 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7973#endif
7974
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007975#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007976 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007977#endif
7978
7979#if defined(MBEDTLS_ECP_C)
7980 conf->curve_list = mbedtls_ecp_grp_id_list();
7981#endif
7982
7983#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7984 conf->dhm_min_bitlen = 1024;
7985#endif
7986 }
7987
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007988 return( 0 );
7989}
7990
7991/*
7992 * Free mbedtls_ssl_config
7993 */
7994void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
7995{
7996#if defined(MBEDTLS_DHM_C)
7997 mbedtls_mpi_free( &conf->dhm_P );
7998 mbedtls_mpi_free( &conf->dhm_G );
7999#endif
8000
8001#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8002 if( conf->psk != NULL )
8003 {
8004 mbedtls_zeroize( conf->psk, conf->psk_len );
8005 mbedtls_zeroize( conf->psk_identity, conf->psk_identity_len );
8006 mbedtls_free( conf->psk );
8007 mbedtls_free( conf->psk_identity );
8008 conf->psk_len = 0;
8009 conf->psk_identity_len = 0;
8010 }
8011#endif
8012
8013#if defined(MBEDTLS_X509_CRT_PARSE_C)
8014 ssl_key_cert_free( conf->key_cert );
8015#endif
8016
8017 mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
8018}
8019
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008020#if defined(MBEDTLS_PK_C) && \
8021 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008022/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008026{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008027#if defined(MBEDTLS_RSA_C)
8028 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8029 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008030#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008031#if defined(MBEDTLS_ECDSA_C)
8032 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8033 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008036}
8037
Hanno Becker7e5437a2017-04-28 17:15:26 +01008038unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8039{
8040 switch( type ) {
8041 case MBEDTLS_PK_RSA:
8042 return( MBEDTLS_SSL_SIG_RSA );
8043 case MBEDTLS_PK_ECDSA:
8044 case MBEDTLS_PK_ECKEY:
8045 return( MBEDTLS_SSL_SIG_ECDSA );
8046 default:
8047 return( MBEDTLS_SSL_SIG_ANON );
8048 }
8049}
8050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008052{
8053 switch( sig )
8054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055#if defined(MBEDTLS_RSA_C)
8056 case MBEDTLS_SSL_SIG_RSA:
8057 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008058#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008059#if defined(MBEDTLS_ECDSA_C)
8060 case MBEDTLS_SSL_SIG_ECDSA:
8061 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008062#endif
8063 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008064 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008065 }
8066}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008067#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008068
Hanno Becker7e5437a2017-04-28 17:15:26 +01008069#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8070 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8071
8072/* Find an entry in a signature-hash set matching a given hash algorithm. */
8073mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8074 mbedtls_pk_type_t sig_alg )
8075{
8076 switch( sig_alg )
8077 {
8078 case MBEDTLS_PK_RSA:
8079 return( set->rsa );
8080 case MBEDTLS_PK_ECDSA:
8081 return( set->ecdsa );
8082 default:
8083 return( MBEDTLS_MD_NONE );
8084 }
8085}
8086
8087/* Add a signature-hash-pair to a signature-hash set */
8088void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8089 mbedtls_pk_type_t sig_alg,
8090 mbedtls_md_type_t md_alg )
8091{
8092 switch( sig_alg )
8093 {
8094 case MBEDTLS_PK_RSA:
8095 if( set->rsa == MBEDTLS_MD_NONE )
8096 set->rsa = md_alg;
8097 break;
8098
8099 case MBEDTLS_PK_ECDSA:
8100 if( set->ecdsa == MBEDTLS_MD_NONE )
8101 set->ecdsa = md_alg;
8102 break;
8103
8104 default:
8105 break;
8106 }
8107}
8108
8109/* Allow exactly one hash algorithm for each signature. */
8110void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8111 mbedtls_md_type_t md_alg )
8112{
8113 set->rsa = md_alg;
8114 set->ecdsa = md_alg;
8115}
8116
8117#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8118 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8119
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008120/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008121 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008124{
8125 switch( hash )
8126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008127#if defined(MBEDTLS_MD5_C)
8128 case MBEDTLS_SSL_HASH_MD5:
8129 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008130#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131#if defined(MBEDTLS_SHA1_C)
8132 case MBEDTLS_SSL_HASH_SHA1:
8133 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008134#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135#if defined(MBEDTLS_SHA256_C)
8136 case MBEDTLS_SSL_HASH_SHA224:
8137 return( MBEDTLS_MD_SHA224 );
8138 case MBEDTLS_SSL_HASH_SHA256:
8139 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008140#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141#if defined(MBEDTLS_SHA512_C)
8142 case MBEDTLS_SSL_HASH_SHA384:
8143 return( MBEDTLS_MD_SHA384 );
8144 case MBEDTLS_SSL_HASH_SHA512:
8145 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008146#endif
8147 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008149 }
8150}
8151
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008152/*
8153 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8154 */
8155unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8156{
8157 switch( md )
8158 {
8159#if defined(MBEDTLS_MD5_C)
8160 case MBEDTLS_MD_MD5:
8161 return( MBEDTLS_SSL_HASH_MD5 );
8162#endif
8163#if defined(MBEDTLS_SHA1_C)
8164 case MBEDTLS_MD_SHA1:
8165 return( MBEDTLS_SSL_HASH_SHA1 );
8166#endif
8167#if defined(MBEDTLS_SHA256_C)
8168 case MBEDTLS_MD_SHA224:
8169 return( MBEDTLS_SSL_HASH_SHA224 );
8170 case MBEDTLS_MD_SHA256:
8171 return( MBEDTLS_SSL_HASH_SHA256 );
8172#endif
8173#if defined(MBEDTLS_SHA512_C)
8174 case MBEDTLS_MD_SHA384:
8175 return( MBEDTLS_SSL_HASH_SHA384 );
8176 case MBEDTLS_MD_SHA512:
8177 return( MBEDTLS_SSL_HASH_SHA512 );
8178#endif
8179 default:
8180 return( MBEDTLS_SSL_HASH_NONE );
8181 }
8182}
8183
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008184#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008185/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008186 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008187 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008188 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008189int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008190{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008191 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008192
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008193 if( ssl->conf->curve_list == NULL )
8194 return( -1 );
8195
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008196 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008197 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008198 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008199
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008200 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008201}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008202#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008203
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008204#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008205/*
8206 * Check if a hash proposed by the peer is in our list.
8207 * Return 0 if we're willing to use it, -1 otherwise.
8208 */
8209int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8210 mbedtls_md_type_t md )
8211{
8212 const int *cur;
8213
8214 if( ssl->conf->sig_hashes == NULL )
8215 return( -1 );
8216
8217 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8218 if( *cur == (int) md )
8219 return( 0 );
8220
8221 return( -1 );
8222}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008223#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008225#if defined(MBEDTLS_X509_CRT_PARSE_C)
8226int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8227 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008228 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008229 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008230{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008231 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008233 int usage = 0;
8234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008236 const char *ext_oid;
8237 size_t ext_len;
8238#endif
8239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008240#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8241 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008242 ((void) cert);
8243 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008244 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008245#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008247#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8248 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008249 {
8250 /* Server part of the key exchange */
8251 switch( ciphersuite->key_exchange )
8252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253 case MBEDTLS_KEY_EXCHANGE_RSA:
8254 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008255 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008256 break;
8257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8259 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8260 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8261 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008262 break;
8263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008264 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8265 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008266 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008267 break;
8268
8269 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008270 case MBEDTLS_KEY_EXCHANGE_NONE:
8271 case MBEDTLS_KEY_EXCHANGE_PSK:
8272 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8273 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008274 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008275 usage = 0;
8276 }
8277 }
8278 else
8279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8281 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008282 }
8283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008285 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008286 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008287 ret = -1;
8288 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008289#else
8290 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008291#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8294 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8297 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008298 }
8299 else
8300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008301 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8302 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008303 }
8304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008306 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008307 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008308 ret = -1;
8309 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008311
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008312 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008313}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008314#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008315
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008316/*
8317 * Convert version numbers to/from wire format
8318 * and, for DTLS, to/from TLS equivalent.
8319 *
8320 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008321 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008322 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8323 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008325void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008326 unsigned char ver[2] )
8327{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328#if defined(MBEDTLS_SSL_PROTO_DTLS)
8329 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008332 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8333
8334 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8335 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8336 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008337 else
8338#else
8339 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008340#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008341 {
8342 ver[0] = (unsigned char) major;
8343 ver[1] = (unsigned char) minor;
8344 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008345}
8346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008348 const unsigned char ver[2] )
8349{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350#if defined(MBEDTLS_SSL_PROTO_DTLS)
8351 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008352 {
8353 *major = 255 - ver[0] + 2;
8354 *minor = 255 - ver[1] + 1;
8355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008356 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008357 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8358 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008359 else
8360#else
8361 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008362#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008363 {
8364 *major = ver[0];
8365 *minor = ver[1];
8366 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008367}
8368
Simon Butcher99000142016-10-13 17:21:01 +01008369int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8370{
8371#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8372 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8373 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8374
8375 switch( md )
8376 {
8377#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8378#if defined(MBEDTLS_MD5_C)
8379 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008380 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008381#endif
8382#if defined(MBEDTLS_SHA1_C)
8383 case MBEDTLS_SSL_HASH_SHA1:
8384 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8385 break;
8386#endif
8387#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8388#if defined(MBEDTLS_SHA512_C)
8389 case MBEDTLS_SSL_HASH_SHA384:
8390 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8391 break;
8392#endif
8393#if defined(MBEDTLS_SHA256_C)
8394 case MBEDTLS_SSL_HASH_SHA256:
8395 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8396 break;
8397#endif
8398 default:
8399 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8400 }
8401
8402 return 0;
8403#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8404 (void) ssl;
8405 (void) md;
8406
8407 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8408#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8409}
8410
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008411#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8412 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8413int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8414 unsigned char *output,
8415 unsigned char *data, size_t data_len )
8416{
8417 int ret = 0;
8418 mbedtls_md5_context mbedtls_md5;
8419 mbedtls_sha1_context mbedtls_sha1;
8420
8421 mbedtls_md5_init( &mbedtls_md5 );
8422 mbedtls_sha1_init( &mbedtls_sha1 );
8423
8424 /*
8425 * digitally-signed struct {
8426 * opaque md5_hash[16];
8427 * opaque sha_hash[20];
8428 * };
8429 *
8430 * md5_hash
8431 * MD5(ClientHello.random + ServerHello.random
8432 * + ServerParams);
8433 * sha_hash
8434 * SHA(ClientHello.random + ServerHello.random
8435 * + ServerParams);
8436 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008437 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008438 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008440 goto exit;
8441 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008442 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008443 ssl->handshake->randbytes, 64 ) ) != 0 )
8444 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008446 goto exit;
8447 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008448 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008449 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008451 goto exit;
8452 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008453 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008454 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008455 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008456 goto exit;
8457 }
8458
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008459 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008460 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008462 goto exit;
8463 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008464 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008465 ssl->handshake->randbytes, 64 ) ) != 0 )
8466 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008468 goto exit;
8469 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008470 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008471 data_len ) ) != 0 )
8472 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008474 goto exit;
8475 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008476 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008477 output + 16 ) ) != 0 )
8478 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008479 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008480 goto exit;
8481 }
8482
8483exit:
8484 mbedtls_md5_free( &mbedtls_md5 );
8485 mbedtls_sha1_free( &mbedtls_sha1 );
8486
8487 if( ret != 0 )
8488 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8489 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8490
8491 return( ret );
8492
8493}
8494#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
8495 MBEDTLS_SSL_PROTO_TLS1_1 */
8496
8497#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8498 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8499int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8500 unsigned char *output,
8501 unsigned char *data, size_t data_len,
8502 mbedtls_md_type_t md_alg )
8503{
8504 int ret = 0;
8505 mbedtls_md_context_t ctx;
8506 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8507
8508 mbedtls_md_init( &ctx );
8509
8510 /*
8511 * digitally-signed struct {
8512 * opaque client_random[32];
8513 * opaque server_random[32];
8514 * ServerDHParams params;
8515 * };
8516 */
8517 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8518 {
8519 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8520 goto exit;
8521 }
8522 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8523 {
8524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8525 goto exit;
8526 }
8527 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8528 {
8529 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8530 goto exit;
8531 }
8532 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8533 {
8534 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8535 goto exit;
8536 }
8537 if( ( ret = mbedtls_md_finish( &ctx, output ) ) != 0 )
8538 {
8539 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8540 goto exit;
8541 }
8542
8543exit:
8544 mbedtls_md_free( &ctx );
8545
8546 if( ret != 0 )
8547 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8548 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8549
8550 return( ret );
8551}
8552#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
8553 MBEDTLS_SSL_PROTO_TLS1_2 */
8554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008555#endif /* MBEDTLS_SSL_TLS_C */