blob: a16568cc4259d9553b1f841772a6e5123721bd3e [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) */
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001316static void ssl_read_memory( const unsigned char *p, size_t len )
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02001317{
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é-Gonnard41df0f22020-07-28 11:35:39 +02001662#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001663/*
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001664 * Constant-flow conditional memcpy:
1665 * - if c1 == c2, equivalent to memcpy(dst, src, len),
1666 * - otherwise, a no-op,
1667 * but with execution flow independent of the values of c1 and c2.
1668 *
1669 * Use only bit operations to avoid branches that could be used by some
1670 * compilers on some platforms to translate comparison operators.
1671 */
1672static void mbedtls_ssl_cf_memcpy_if_eq(unsigned char *dst,
1673 const unsigned char *src,
1674 size_t len,
1675 size_t c1, size_t c2 )
1676{
1677 /* diff = 0 if c1 == c2, non-zero otherwise */
1678 const size_t diff = c1 ^ c2;
1679
1680 /* MSVC has a warning about unary minus on unsigned integer types,
1681 * but this is well-defined and precisely what we want to do here. */
1682#if defined(_MSC_VER)
1683#pragma warning( push )
1684#pragma warning( disable : 4146 )
1685#endif
1686
1687 /* diff_msb's most significant bit is bit equal to c1 != c2 */
1688 const size_t diff_msb = ( diff | -diff );
1689
1690 /* diff1 = c1 != c2 */
1691 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
1692
1693 /* mask = c1 != c2 ? 0xff : 0x00 */
1694 unsigned char mask = (unsigned char) -diff1;
1695
1696#if defined(_MSC_VER)
1697#pragma warning( pop )
1698#endif
1699
1700 /* dst[i] = c1 != c2 ? dst[i] : src[i] */
1701 for( size_t i = 0; i < len; i++ )
1702 dst[i] = ( dst[i] & mask ) | ( src[i] & ~mask );
1703}
1704
1705/*
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001706 * Compute HMAC of variable-length data with constant flow.
1707 */
1708int mbedtls_ssl_cf_hmac(
1709 mbedtls_md_context_t *ctx,
1710 const unsigned char *add_data, size_t add_data_len,
1711 const unsigned char *data, size_t data_len_secret,
1712 size_t min_data_len, size_t max_data_len,
1713 unsigned char *output )
1714{
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001715 /*
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001716 * This function breaks the HMAC abstraction and uses the md_clone()
1717 * extension to the MD API in order to get constant-flow behaviour.
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001718 *
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001719 * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
Manuel Pégourié-Gonnardec956b12020-07-28 11:42:31 +02001720 * concatenation, and okey/ikey are the XOR of the key with some fixed bit
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001721 * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001722 *
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001723 * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
1724 * minlen, then cloning the context, and for each byte up to maxlen
1725 * finishing up the hash computation, keeping only the correct result.
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001726 *
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001727 * Then we only need to compute HASH(okey + inner_hash) and we're done.
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001728 */
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001729 const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info );
Manuel Pégourié-Gonnardec956b12020-07-28 11:42:31 +02001730 /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5,
1731 * all of which have the same block size except SHA-384. */
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001732 const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
Manuel Pégourié-Gonnardc9ef5a22020-07-28 11:45:02 +02001733 const unsigned char * const ikey = ctx->hmac_ctx;
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001734 const unsigned char * const okey = ikey + block_size;
1735 const size_t hash_size = mbedtls_md_get_size( ctx->md_info );
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001736
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001737 unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
1738 mbedtls_md_context_t aux;
1739 size_t offset;
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001740
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001741 mbedtls_md_init( &aux );
1742 mbedtls_md_setup( &aux, ctx->md_info, 0 );
1743
1744 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
1745 * so we can start directly with the message */
1746 mbedtls_md_update( ctx, add_data, add_data_len );
1747 mbedtls_md_update( ctx, data, min_data_len );
1748
1749 /* For each possible length, compute the hash up to that point */
1750 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001751 {
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001752 mbedtls_md_clone( &aux, ctx );
1753 mbedtls_md_finish( &aux, aux_out );
1754 /* Keep only the correct inner_hash in the output buffer */
1755 mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size,
1756 offset, data_len_secret );
1757
1758 if( offset < max_data_len )
1759 mbedtls_md_update( ctx, data + offset, 1 );
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001760 }
1761
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001762 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001763 mbedtls_md_starts( ctx );
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001764 mbedtls_md_update( ctx, okey, block_size );
1765 mbedtls_md_update( ctx, output, hash_size );
1766 mbedtls_md_finish( ctx, output );
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001767
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001768 /* Done, get ready for next time */
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001769 mbedtls_md_hmac_reset( ctx );
1770
Manuel Pégourié-Gonnard4508c672020-07-28 11:25:34 +02001771 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001772 return( 0 );
1773}
Manuel Pégourié-Gonnard41df0f22020-07-28 11:35:39 +02001774#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001777{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001778 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001780 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001781#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001782 size_t padlen = 0, correct = 1;
1783#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001786
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001787 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1790 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001791 }
1792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001794
Paul Bakker48916f92012-09-16 19:57:18 +00001795 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001798 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001800 }
1801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1803 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001804 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001805 int ret;
1806 size_t olen = 0;
1807
Paul Bakker68884e32013-01-07 18:20:04 +01001808 padlen = 0;
1809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001811 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001812 ssl->transform_in->ivlen,
1813 ssl->in_msg, ssl->in_msglen,
1814 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001817 return( ret );
1818 }
1819
1820 if( ssl->in_msglen != olen )
1821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1823 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001824 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001825 }
Paul Bakker68884e32013-01-07 18:20:04 +01001826 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1828#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1829 if( mode == MBEDTLS_MODE_GCM ||
1830 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001831 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001832 int ret;
1833 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001834 unsigned char *dec_msg;
1835 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001836 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001837 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001839 size_t explicit_iv_len = ssl->transform_in->ivlen -
1840 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001841
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001842 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001845 "+ taglen (%d)", ssl->in_msglen,
1846 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001848 }
1849 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1850
Paul Bakker68884e32013-01-07 18:20:04 +01001851 dec_msg = ssl->in_msg;
1852 dec_msg_result = ssl->in_msg;
1853 ssl->in_msglen = dec_msglen;
1854
1855 memcpy( add_data, ssl->in_ctr, 8 );
1856 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001858 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001859 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1860 add_data[12] = ssl->in_msglen & 0xFF;
1861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakker68884e32013-01-07 18:20:04 +01001863 add_data, 13 );
1864
1865 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1866 ssl->in_iv,
1867 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
Paul Bakker68884e32013-01-07 18:20:04 +01001870 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001872
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001873 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001874 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001875 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001877 ssl->transform_in->iv_dec,
1878 ssl->transform_in->ivlen,
1879 add_data, 13,
1880 dec_msg, dec_msglen,
1881 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001882 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1887 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001888
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001889 return( ret );
1890 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001891 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001892
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001893 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1896 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001897 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001898 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001901#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001903 {
Paul Bakker45829992013-01-03 14:52:21 +01001904 /*
1905 * Decrypt and check the padding
1906 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001907 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001908 unsigned char *dec_msg;
1909 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001910 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001911 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001912 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001913
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 /*
Paul Bakker45829992013-01-03 14:52:21 +01001915 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1918 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001919 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001920#endif
Paul Bakker45829992013-01-03 14:52:21 +01001921
1922 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1923 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001926 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1927 ssl->transform_in->ivlen,
1928 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001930 }
1931
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001932 dec_msglen = ssl->in_msglen;
1933 dec_msg = ssl->in_msg;
1934 dec_msg_result = ssl->in_msg;
1935
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001936 /*
1937 * Authenticate before decrypt if enabled
1938 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1940 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001941 {
Hanno Becker992b6872017-11-09 18:57:39 +00001942 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001943 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001946
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001947 dec_msglen -= ssl->transform_in->maclen;
1948 ssl->in_msglen -= ssl->transform_in->maclen;
1949
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001950 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1951 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1952 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1953 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1958 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001959 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001960 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001964 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001965 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001966 ssl->transform_in->maclen );
1967
Hanno Becker992b6872017-11-09 18:57:39 +00001968 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1969 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001974 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001975 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001976 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001978
1979 /*
1980 * Check length sanity
1981 */
1982 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001985 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001987 }
1988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001990 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001991 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001994 {
Paul Bakker48916f92012-09-16 19:57:18 +00001995 dec_msglen -= ssl->transform_in->ivlen;
1996 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001997
Paul Bakker48916f92012-09-16 19:57:18 +00001998 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001999 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002000 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002004 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002005 ssl->transform_in->ivlen,
2006 dec_msg, dec_msglen,
2007 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002010 return( ret );
2011 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002012
Paul Bakkercca5b812013-08-31 17:40:26 +02002013 if( dec_msglen != olen )
2014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2016 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002017 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2020 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002021 {
2022 /*
2023 * Save IV in SSL3 and TLS1
2024 */
2025 memcpy( ssl->transform_in->iv_dec,
2026 ssl->transform_in->cipher_ctx_dec.iv,
2027 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002029#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002030
2031 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002032
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002033 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002034 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036#if defined(MBEDTLS_SSL_DEBUG_ALL)
2037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002038 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002039#endif
Paul Bakker45829992013-01-03 14:52:21 +01002040 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002041 correct = 0;
2042 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044#if defined(MBEDTLS_SSL_PROTO_SSL3)
2045 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 {
Paul Bakker48916f92012-09-16 19:57:18 +00002047 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049#if defined(MBEDTLS_SSL_DEBUG_ALL)
2050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002052 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002053#endif
Paul Bakker45829992013-01-03 14:52:21 +01002054 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 }
2056 }
2057 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2059#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2060 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2061 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 {
2063 /*
Paul Bakker45829992013-01-03 14:52:21 +01002064 * TLSv1+: always check the padding up to the first failure
2065 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002067 size_t pad_count = 0, real_count = 1;
Angus Gratton1ba8e912018-06-19 15:57:50 +10002068 size_t padding_idx = ssl->in_msglen - padlen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002069
Paul Bakker956c9e02013-12-19 14:42:28 +01002070 /*
2071 * Padding is guaranteed to be incorrect if:
Angus Gratton1ba8e912018-06-19 15:57:50 +10002072 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002073 *
Angus Gratton1ba8e912018-06-19 15:57:50 +10002074 * 2. padding_idx > MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002075 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002076 *
2077 * In both cases we reset padding_idx to a safe value (0) to
2078 * prevent out-of-buffer reads.
2079 */
Angus Gratton1ba8e912018-06-19 15:57:50 +10002080 correct &= ( padlen <= ssl->in_msglen );
2081 correct &= ( padding_idx <= MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002082 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002083
2084 padding_idx *= correct;
2085
Angus Gratton1ba8e912018-06-19 15:57:50 +10002086 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002087 {
Angus Gratton1ba8e912018-06-19 15:57:50 +10002088 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002089 pad_count += real_count *
2090 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2091 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002092
2093 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002096 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002098#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002099 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002101 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2103 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2106 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002107 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002108
2109 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002111 else
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02002112#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2115 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002116 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002117
Manuel Pégourié-Gonnard7c344322018-07-10 11:15:36 +02002118#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard7c344322018-07-10 11:15:36 +02002121#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
2123 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002124 * Authenticate if not done yet.
2125 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002127#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002128 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002129 {
Hanno Becker992b6872017-11-09 18:57:39 +00002130 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002131
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002132 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002133
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002134 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2135 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137#if defined(MBEDTLS_SSL_PROTO_SSL3)
2138 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002139 {
2140 ssl_mac( &ssl->transform_in->md_ctx_dec,
2141 ssl->transform_in->mac_dec,
2142 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002143 ssl->in_ctr, ssl->in_msgtype,
2144 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002145 }
2146 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2148#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2149 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2150 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002151 {
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002152 int ret;
2153 unsigned char add_data[13];
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002154
2155 /*
2156 * The next two sizes are the minimum and maximum values of
2157 * in_msglen over all padlen values.
2158 *
2159 * They're independent of padlen, since we previously did
2160 * in_msglen -= padlen.
2161 *
2162 * Note that max_len + maclen is never more than the buffer
2163 * length, as we previously did in_msglen -= maclen too.
2164 */
2165 const size_t max_len = ssl->in_msglen + padlen;
2166 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2167
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002168 memcpy( add_data + 0, ssl->in_ctr, 8 );
2169 memcpy( add_data + 8, ssl->in_hdr, 3 );
2170 memcpy( add_data + 11, ssl->in_len, 2 );
2171
2172 ret = mbedtls_ssl_cf_hmac( &ssl->transform_in->md_ctx_dec,
2173 add_data, sizeof( add_data ),
2174 ssl->in_msg, ssl->in_msglen,
2175 min_len, max_len,
2176 mac_expect );
2177 if( ret != 0 )
Gilles Peskinebb07ca02018-06-06 17:23:31 +02002178 {
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002179 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
2180 return( ret );
Gilles Peskinebb07ca02018-06-06 17:23:31 +02002181 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002182
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002183 /* Make sure we access all the memory that could contain the MAC,
2184 * before we check it in the next code block. This makes the
2185 * synchronisation requirements for just-in-time Prime+Probe
2186 * attacks much tighter and hopefully impractical. */
2187 ssl_read_memory( ssl->in_msg + min_len,
2188 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002189 }
2190 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2192 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002196 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002197
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002198#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002199 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2200 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2201 ssl->transform_in->maclen );
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002202#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002203
Hanno Becker992b6872017-11-09 18:57:39 +00002204 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2205 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207#if defined(MBEDTLS_SSL_DEBUG_ALL)
2208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002209#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002210 correct = 0;
2211 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002212 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002213 }
Hanno Beckerca31b472018-10-17 14:43:14 +01002214
2215 /*
2216 * Finally check the correct flag
2217 */
2218 if( correct == 0 )
2219 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002220#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002221
2222 /* Make extra sure authentication was performed, exactly once */
2223 if( auth_done != 1 )
2224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2226 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002227 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002228
2229 if( ssl->in_msglen == 0 )
2230 {
Angus Grattonb91cb6e2018-06-19 15:58:22 +10002231#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2232 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2233 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2234 {
2235 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2237 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2238 }
2239#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2240
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 ssl->nb_zero++;
2242
2243 /*
2244 * Three or more empty messages may be a DoS attack
2245 * (excessive CPU consumption).
2246 */
2247 if( ssl->nb_zero > 3 )
2248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 }
2253 }
2254 else
2255 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002258 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002259 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002260 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002261 }
2262 else
2263#endif
2264 {
2265 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2266 if( ++ssl->in_ctr[i - 1] != 0 )
2267 break;
2268
2269 /* The loop goes to its end iff the counter is wrapping */
2270 if( i == ssl_ep_len( ssl ) )
2271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2273 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002274 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002275 }
2276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
2279 return( 0 );
2280}
2281
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002282#undef MAC_NONE
2283#undef MAC_PLAINTEXT
2284#undef MAC_CIPHERTEXT
2285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002287/*
2288 * Compression/decompression functions
2289 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002291{
2292 int ret;
2293 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002294 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002295 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002296 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002299
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002300 if( len_pre == 0 )
2301 return( 0 );
2302
Paul Bakker2770fbd2012-07-03 13:30:23 +00002303 memcpy( msg_pre, ssl->out_msg, len_pre );
2304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002306 ssl->out_msglen ) );
2307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002309 ssl->out_msg, ssl->out_msglen );
2310
Paul Bakker48916f92012-09-16 19:57:18 +00002311 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2312 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2313 ssl->transform_out->ctx_deflate.next_out = msg_post;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002314 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002315
Paul Bakker48916f92012-09-16 19:57:18 +00002316 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002317 if( ret != Z_OK )
2318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2320 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002321 }
2322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 ssl->out_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002324 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002327 ssl->out_msglen ) );
2328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002330 ssl->out_msg, ssl->out_msglen );
2331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002333
2334 return( 0 );
2335}
2336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002338{
2339 int ret;
2340 unsigned char *msg_post = ssl->in_msg;
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002341 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002342 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002343 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002346
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002347 if( len_pre == 0 )
2348 return( 0 );
2349
Paul Bakker2770fbd2012-07-03 13:30:23 +00002350 memcpy( msg_pre, ssl->in_msg, len_pre );
2351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002353 ssl->in_msglen ) );
2354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002356 ssl->in_msg, ssl->in_msglen );
2357
Paul Bakker48916f92012-09-16 19:57:18 +00002358 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2359 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2360 ssl->transform_in->ctx_inflate.next_out = msg_post;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002361 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002362 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002363
Paul Bakker48916f92012-09-16 19:57:18 +00002364 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002365 if( ret != Z_OK )
2366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2368 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002369 }
2370
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002371 ssl->in_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002372 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002375 ssl->in_msglen ) );
2376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002378 ssl->in_msg, ssl->in_msglen );
2379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002381
2382 return( 0 );
2383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2387static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389#if defined(MBEDTLS_SSL_PROTO_DTLS)
2390static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002391{
2392 /* If renegotiation is not enforced, retransmit until we would reach max
2393 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002394 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002395 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002396 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002397 unsigned char doublings = 1;
2398
2399 while( ratio != 0 )
2400 {
2401 ++doublings;
2402 ratio >>= 1;
2403 }
2404
2405 if( ++ssl->renego_records_seen > doublings )
2406 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002408 return( 0 );
2409 }
2410 }
2411
2412 return( ssl_write_hello_request( ssl ) );
2413}
2414#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002416
Paul Bakker5121ce52009-01-03 21:22:43 +00002417/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002418 * Fill the input message buffer by appending data to it.
2419 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002420 *
2421 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2422 * available (from this read and/or a previous one). Otherwise, an error code
2423 * is returned (possibly EOF or WANT_READ).
2424 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002425 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2426 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2427 * since we always read a whole datagram at once.
2428 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002429 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002430 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002433{
Paul Bakker23986e52011-04-24 08:57:21 +00002434 int ret;
2435 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002438
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002439 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002442 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002444 }
2445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 if( nb_want > MBEDTLS_SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002450 }
2451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002453 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002455 uint32_t timeout;
2456
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002457 /* Just to be sure */
2458 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2459 {
2460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2461 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2462 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2463 }
2464
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002465 /*
2466 * The point is, we need to always read a full datagram at once, so we
2467 * sometimes read more then requested, and handle the additional data.
2468 * It could be the rest of the current record (while fetching the
2469 * header) and/or some other records in the same datagram.
2470 */
2471
2472 /*
2473 * Move to the next record in the already read datagram if applicable
2474 */
2475 if( ssl->next_record_offset != 0 )
2476 {
2477 if( ssl->in_left < ssl->next_record_offset )
2478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2480 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002481 }
2482
2483 ssl->in_left -= ssl->next_record_offset;
2484
2485 if( ssl->in_left != 0 )
2486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002488 ssl->next_record_offset ) );
2489 memmove( ssl->in_hdr,
2490 ssl->in_hdr + ssl->next_record_offset,
2491 ssl->in_left );
2492 }
2493
2494 ssl->next_record_offset = 0;
2495 }
2496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002499
2500 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002501 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002502 */
2503 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002506 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002507 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002508
2509 /*
Antonin Décimo8fd91562019-01-23 15:24:37 +01002510 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002511 * are not at the beginning of a new record, the caller did something
2512 * wrong.
2513 */
2514 if( ssl->in_left != 0 )
2515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2517 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002518 }
2519
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002520 /*
2521 * Don't even try to read if time's out already.
2522 * This avoids by-passing the timer when repeatedly receiving messages
2523 * that will end up being dropped.
2524 */
2525 if( ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002526 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002527 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 len = MBEDTLS_SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002532 timeout = ssl->handshake->retransmit_timeout;
2533 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002534 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002537
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002538 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002539 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2540 timeout );
2541 else
2542 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002545
2546 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002548 }
2549
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002550 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002553 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002556 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002557 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002560 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002561 }
2562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002566 return( ret );
2567 }
2568
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002569 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002570 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002572 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002574 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002575 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002578 return( ret );
2579 }
2580
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002581 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002582 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002584 }
2585
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 if( ret < 0 )
2587 return( ret );
2588
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002589 ssl->in_left = ret;
2590 }
2591 else
2592#endif
2593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002595 ssl->in_left, nb_want ) );
2596
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002597 while( ssl->in_left < nb_want )
2598 {
2599 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002600
2601 if( ssl_check_timer( ssl ) != 0 )
2602 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2603 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002604 {
2605 if( ssl->f_recv_timeout != NULL )
2606 {
2607 ret = ssl->f_recv_timeout( ssl->p_bio,
2608 ssl->in_hdr + ssl->in_left, len,
2609 ssl->conf->read_timeout );
2610 }
2611 else
2612 {
2613 ret = ssl->f_recv( ssl->p_bio,
2614 ssl->in_hdr + ssl->in_left, len );
2615 }
2616 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002619 ssl->in_left, nb_want ) );
2620 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002621
2622 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002624
2625 if( ret < 0 )
2626 return( ret );
2627
mohammad160344a6a682018-03-28 23:45:33 -07002628 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad1603b11af862018-03-19 07:18:13 -07002629 {
Jaeden Amerob5f53b12018-04-03 12:09:45 +01002630 MBEDTLS_SSL_DEBUG_MSG( 1,
2631 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160329ed80f2018-04-02 07:34:26 -07002632 ret, (unsigned long)len ) );
mohammad1603b11af862018-03-19 07:18:13 -07002633 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2634 }
2635
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002636 ssl->in_left += ret;
2637 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 }
2639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002641
2642 return( 0 );
2643}
2644
2645/*
2646 * Flush any data not yet written
2647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002649{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002650 int ret;
2651 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002655 if( ssl->f_send == NULL )
2656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002658 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002660 }
2661
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002662 /* Avoid incrementing counter if data is flushed */
2663 if( ssl->out_left == 0 )
2664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002666 return( 0 );
2667 }
2668
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 while( ssl->out_left > 0 )
2670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2672 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) +
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002675 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002676 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
2680 if( ret <= 0 )
2681 return( ret );
2682
mohammad160344a6a682018-03-28 23:45:33 -07002683 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16036085c722018-02-22 04:29:04 -08002684 {
Jaeden Amerob5f53b12018-04-03 12:09:45 +01002685 MBEDTLS_SSL_DEBUG_MSG( 1,
2686 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160329ed80f2018-04-02 07:34:26 -07002687 ret, (unsigned long)ssl->out_left ) );
mohammad16036085c722018-02-22 04:29:04 -08002688 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2689 }
2690
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 ssl->out_left -= ret;
2692 }
2693
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002694 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002695 if( ++ssl->out_ctr[i - 1] != 0 )
2696 break;
2697
2698 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002699 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2702 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002703 }
2704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002706
2707 return( 0 );
2708}
2709
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002710/*
2711 * Functions to handle the DTLS retransmission state machine
2712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002714/*
2715 * Append current handshake message to current outgoing flight
2716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002718{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 mbedtls_ssl_flight_item *msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002720
2721 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002722 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002723 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002726 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002727 }
2728
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002729 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002730 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002733 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002734 }
2735
2736 /* Copy current handshake message with headers */
2737 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2738 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002739 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002740 msg->next = NULL;
2741
2742 /* Append to the current flight */
2743 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002744 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002745 else
2746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002748 while( cur->next != NULL )
2749 cur = cur->next;
2750 cur->next = msg;
2751 }
2752
2753 return( 0 );
2754}
2755
2756/*
2757 * Free the current flight of handshake messages
2758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002760{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 mbedtls_ssl_flight_item *cur = flight;
2762 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002763
2764 while( cur != NULL )
2765 {
2766 next = cur->next;
2767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 mbedtls_free( cur->p );
2769 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002770
2771 cur = next;
2772 }
2773}
2774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2776static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002777#endif
2778
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002779/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002780 * Swap transform_out and out_ctr with the alternative ones
2781 */
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002782static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002783{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002785 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002786#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2787 int ret;
2788#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002789
2790 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002793 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002794 }
2795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002797
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002798 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002799 tmp_transform = ssl->transform_out;
2800 ssl->transform_out = ssl->handshake->alt_transform_out;
2801 ssl->handshake->alt_transform_out = tmp_transform;
2802
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002803 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002804 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2805 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2806 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002807
2808 /* Adjust to the newly activated transform */
2809 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002811 {
2812 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2813 ssl->transform_out->fixed_ivlen;
2814 }
2815 else
2816 ssl->out_msg = ssl->out_iv;
2817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2819 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2824 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002825 }
2826 }
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002827#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
2828
2829 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002830}
2831
2832/*
2833 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002834 *
2835 * Need to remember the current message in case flush_output returns
2836 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002837 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002840{
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002841 int ret;
2842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002848
2849 ssl->handshake->cur_msg = ssl->handshake->flight;
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002850 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
2851 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002854 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002855
2856 while( ssl->handshake->cur_msg != NULL )
2857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 mbedtls_ssl_flight_item *cur = ssl->handshake->cur_msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002859
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002860 /* Swap epochs before sending Finished: we can't do it after
2861 * sending ChangeCipherSpec, in case write returns WANT_READ.
2862 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2864 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002865 {
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002866 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
2867 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002868 }
2869
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002870 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002871 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002872 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002873
2874 ssl->handshake->cur_msg = cur->next;
2875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 MBEDTLS_SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002881 return( ret );
2882 }
2883 }
2884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2886 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002887 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002890 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2891 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002894
2895 return( 0 );
2896}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002897
2898/*
2899 * To be called when the last message of an incoming flight is received.
2900 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002902{
2903 /* We won't need to resend that one any more */
2904 ssl_flight_free( ssl->handshake->flight );
2905 ssl->handshake->flight = NULL;
2906 ssl->handshake->cur_msg = NULL;
2907
2908 /* The next incoming flight will start with this msg_seq */
2909 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2910
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002911 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002912 ssl_set_timer( ssl, 0 );
2913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2915 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002918 }
2919 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002921}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002922
2923/*
2924 * To be called when the last message of an outgoing flight is send.
2925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002927{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002928 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002929 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2932 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002935 }
2936 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002938}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002940
Paul Bakker5121ce52009-01-03 21:22:43 +00002941/*
2942 * Record layer functions
2943 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002944
2945/*
2946 * Write current record.
2947 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002950{
Nicholas Wilsonf0021642016-04-13 11:51:05 +01002951 int ret, done = 0, out_msg_type;
Paul Bakker23986e52011-04-24 08:57:21 +00002952 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002957 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002958 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002960 {
2961 ; /* Skip special handshake treatment when resending */
2962 }
2963 else
2964#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002966 {
Nicholas Wilsonf0021642016-04-13 11:51:05 +01002967 out_msg_type = ssl->out_msg[0];
2968
2969 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002970 ssl->handshake == NULL )
2971 {
2972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2973 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2974 }
2975
Paul Bakker5121ce52009-01-03 21:22:43 +00002976 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2977 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2978 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2979
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002980 /*
2981 * DTLS has additional fields in the Handshake layer,
2982 * between the length field and the actual payload:
2983 * uint16 message_seq;
2984 * uint24 fragment_offset;
2985 * uint24 fragment_length;
2986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002988 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002989 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002990 /* Make room for the additional DTLS fields */
Hanno Becker9648f8b2017-09-18 10:55:54 +01002991 if( MBEDTLS_SSL_MAX_CONTENT_LEN - ssl->out_msglen < 8 )
2992 {
2993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
2994 "size %u, maximum %u",
2995 (unsigned) ( ssl->in_hslen - 4 ),
2996 (unsigned) ( MBEDTLS_SSL_MAX_CONTENT_LEN - 12 ) ) );
2997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2998 }
2999
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003000 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003001 ssl->out_msglen += 8;
3002 len += 8;
3003
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003004 /* Write message_seq and update it, except for HelloRequest */
Nicholas Wilsonf0021642016-04-13 11:51:05 +01003005 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003006 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003007 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3008 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3009 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003010 }
3011 else
3012 {
3013 ssl->out_msg[4] = 0;
3014 ssl->out_msg[5] = 0;
3015 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003016
3017 /* We don't fragment, so frag_offset = 0 and frag_len = len */
3018 memset( ssl->out_msg + 6, 0x00, 3 );
3019 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003020 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003022
Nicholas Wilsonf0021642016-04-13 11:51:05 +01003023 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003024 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 }
3026
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003027 /* Save handshake and CCS messages for resending */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003029 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003030 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING &&
3032 ( ssl->out_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ||
3033 ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003034 {
3035 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003038 return( ret );
3039 }
3040 }
3041#endif
3042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003044 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003046 {
3047 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003050 return( ret );
3051 }
3052
3053 len = ssl->out_msglen;
3054 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3058 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 ret = mbedtls_ssl_hw_record_write( ssl );
3063 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3066 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003067 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003068
3069 if( ret == 0 )
3070 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003071 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003073 if( !done )
3074 {
3075 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003077 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003078
3079 ssl->out_len[0] = (unsigned char)( len >> 8 );
3080 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003081
Paul Bakker48916f92012-09-16 19:57:18 +00003082 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003083 {
3084 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003087 return( ret );
3088 }
3089
3090 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003091 ssl->out_len[0] = (unsigned char)( len >> 8 );
3092 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003093 }
3094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00003096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00003098 "version = [%d:%d], msglen = %d",
3099 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003100 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3103 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003104 }
3105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003109 return( ret );
3110 }
3111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
3114 return( 0 );
3115}
3116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003118/*
3119 * Mark bits in bitmask (used for DTLS HS reassembly)
3120 */
3121static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3122{
3123 unsigned int start_bits, end_bits;
3124
3125 start_bits = 8 - ( offset % 8 );
3126 if( start_bits != 8 )
3127 {
3128 size_t first_byte_idx = offset / 8;
3129
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003130 /* Special case */
3131 if( len <= start_bits )
3132 {
3133 for( ; len != 0; len-- )
3134 mask[first_byte_idx] |= 1 << ( start_bits - len );
3135
3136 /* Avoid potential issues with offset or len becoming invalid */
3137 return;
3138 }
3139
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003140 offset += start_bits; /* Now offset % 8 == 0 */
3141 len -= start_bits;
3142
3143 for( ; start_bits != 0; start_bits-- )
3144 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3145 }
3146
3147 end_bits = len % 8;
3148 if( end_bits != 0 )
3149 {
3150 size_t last_byte_idx = ( offset + len ) / 8;
3151
3152 len -= end_bits; /* Now len % 8 == 0 */
3153
3154 for( ; end_bits != 0; end_bits-- )
3155 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3156 }
3157
3158 memset( mask + offset / 8, 0xFF, len / 8 );
3159}
3160
3161/*
3162 * Check that bitmask is full
3163 */
3164static int ssl_bitmask_check( unsigned char *mask, size_t len )
3165{
3166 size_t i;
3167
3168 for( i = 0; i < len / 8; i++ )
3169 if( mask[i] != 0xFF )
3170 return( -1 );
3171
3172 for( i = 0; i < len % 8; i++ )
3173 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3174 return( -1 );
3175
3176 return( 0 );
3177}
3178
3179/*
3180 * Reassemble fragmented DTLS handshake messages.
3181 *
3182 * Use a temporary buffer for reassembly, divided in two parts:
3183 * - the first holds the reassembled message (including handshake header),
3184 * - the second holds a bitmask indicating which parts of the message
3185 * (excluding headers) have been received so far.
3186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003188{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003189 unsigned char *msg, *bitmask;
3190 size_t frag_len, frag_off;
3191 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3192
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003193 if( ssl->handshake == NULL )
3194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3196 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003197 }
3198
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003199 /*
3200 * For first fragment, check size and allocate buffer
3201 */
3202 if( ssl->handshake->hs_msg == NULL )
3203 {
3204 size_t alloc_len;
3205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003207 msg_len ) );
3208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209 if( ssl->in_hslen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3212 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003213 }
3214
3215 /* The bitmask needs one bit per byte of message excluding header */
3216 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3217
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003218 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003219 if( ssl->handshake->hs_msg == NULL )
3220 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003222 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003223 }
3224
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003225 /* Prepare final header: copy msg_type, length and message_seq,
3226 * then add standardised fragment_offset and fragment_length */
3227 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3228 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3229 memcpy( ssl->handshake->hs_msg + 9,
3230 ssl->handshake->hs_msg + 1, 3 );
3231 }
3232 else
3233 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003234 /* Make sure msg_type and length are consistent */
3235 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3238 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003239 }
3240 }
3241
3242 msg = ssl->handshake->hs_msg + 12;
3243 bitmask = msg + msg_len;
3244
3245 /*
3246 * Check and copy current fragment
3247 */
3248 frag_off = ( ssl->in_msg[6] << 16 ) |
3249 ( ssl->in_msg[7] << 8 ) |
3250 ssl->in_msg[8];
3251 frag_len = ( ssl->in_msg[9] << 16 ) |
3252 ( ssl->in_msg[10] << 8 ) |
3253 ssl->in_msg[11];
3254
3255 if( frag_off + frag_len > msg_len )
3256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003258 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003260 }
3261
3262 if( frag_len + 12 > ssl->in_msglen )
3263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003265 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003267 }
3268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003270 frag_off, frag_len ) );
3271
3272 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3273 ssl_bitmask_set( bitmask, frag_off, frag_len );
3274
3275 /*
3276 * Do we have the complete message by now?
3277 * If yes, finalize it, else ask to read the next record.
3278 */
3279 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003282 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003283 }
3284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003286
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003287 if( frag_len + 12 < ssl->in_msglen )
3288 {
3289 /*
3290 * We'got more handshake messages in the same record.
3291 * This case is not handled now because no know implementation does
3292 * that and it's hard to test, so we prefer to fail cleanly for now.
3293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3295 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003296 }
3297
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003298 if( ssl->in_left > ssl->next_record_offset )
3299 {
3300 /*
3301 * We've got more data in the buffer after the current record,
3302 * that we don't want to overwrite. Move it before writing the
3303 * reassembled message, and adjust in_left and next_record_offset.
3304 */
3305 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3306 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3307 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3308
3309 /* First compute and check new lengths */
3310 ssl->next_record_offset = new_remain - ssl->in_hdr;
3311 ssl->in_left = ssl->next_record_offset + remain_len;
3312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 if( ssl->in_left > MBEDTLS_SSL_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003314 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3317 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003318 }
3319
3320 memmove( new_remain, cur_remain, remain_len );
3321 }
3322
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003323 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3324
Hanno Beckerd82e0c02018-10-15 13:22:22 +01003325 mbedtls_zeroize( ssl->handshake->hs_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003327 ssl->handshake->hs_msg = NULL;
3328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003330 ssl->in_msg, ssl->in_hslen );
3331
3332 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003335
Simon Butcher99000142016-10-13 17:21:01 +01003336int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003337{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003341 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003343 }
3344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003346 ( ssl->in_msg[1] << 16 ) |
3347 ( ssl->in_msg[2] << 8 ) |
3348 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003351 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003352 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003355 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003356 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003357 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003358 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003359
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003360 /* ssl->handshake is NULL when receiving ClientHello for renego */
3361 if( ssl->handshake != NULL &&
3362 recv_msg_seq != ssl->handshake->in_msg_seq )
3363 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003364 /* Retransmit only on last message from previous flight, to avoid
3365 * too many retransmissions.
3366 * Besides, No sane server ever retransmits HelloVerifyRequest */
3367 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003371 "message_seq = %d, start_of_flight = %d",
3372 recv_msg_seq,
3373 ssl->handshake->in_flight_start_seq ) );
3374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003378 return( ret );
3379 }
3380 }
3381 else
3382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003384 "message_seq = %d, expected = %d",
3385 recv_msg_seq,
3386 ssl->handshake->in_msg_seq ) );
3387 }
3388
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003389 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003390 }
3391 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003392
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003393 /* Reassemble if current message is fragmented or reassembly is
3394 * already in progress */
3395 if( ssl->in_msglen < ssl->in_hslen ||
3396 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3397 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3398 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003401
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003402 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003405 return( ret );
3406 }
3407 }
3408 }
3409 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003411 /* With TLS we don't handle fragmentation (for now) */
3412 if( ssl->in_msglen < ssl->in_hslen )
3413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3415 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003416 }
3417
Simon Butcher99000142016-10-13 17:21:01 +01003418 return( 0 );
3419}
3420
3421void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3422{
3423
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003424 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3425 ssl->handshake != NULL )
3426 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003427 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003428 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003429
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003430 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003432 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003433 ssl->handshake != NULL )
3434 {
3435 ssl->handshake->in_msg_seq++;
3436 }
3437#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003438}
3439
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003440/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003441 * DTLS anti-replay: RFC 6347 4.1.2.6
3442 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003443 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3444 * Bit n is set iff record number in_window_top - n has been seen.
3445 *
3446 * Usually, in_window_top is the last record number seen and the lsb of
3447 * in_window is set. The only exception is the initial state (record number 0
3448 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3451static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003452{
3453 ssl->in_window_top = 0;
3454 ssl->in_window = 0;
3455}
3456
3457static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3458{
3459 return( ( (uint64_t) buf[0] << 40 ) |
3460 ( (uint64_t) buf[1] << 32 ) |
3461 ( (uint64_t) buf[2] << 24 ) |
3462 ( (uint64_t) buf[3] << 16 ) |
3463 ( (uint64_t) buf[4] << 8 ) |
3464 ( (uint64_t) buf[5] ) );
3465}
3466
3467/*
3468 * Return 0 if sequence number is acceptable, -1 otherwise
3469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003471{
3472 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3473 uint64_t bit;
3474
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003475 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003476 return( 0 );
3477
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003478 if( rec_seqnum > ssl->in_window_top )
3479 return( 0 );
3480
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003481 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003482
3483 if( bit >= 64 )
3484 return( -1 );
3485
3486 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3487 return( -1 );
3488
3489 return( 0 );
3490}
3491
3492/*
3493 * Update replay window on new validated record
3494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003496{
3497 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3498
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003499 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003500 return;
3501
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003502 if( rec_seqnum > ssl->in_window_top )
3503 {
3504 /* Update window_top and the contents of the window */
3505 uint64_t shift = rec_seqnum - ssl->in_window_top;
3506
3507 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003508 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003509 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003510 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003511 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003512 ssl->in_window |= 1;
3513 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003514
3515 ssl->in_window_top = rec_seqnum;
3516 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003517 else
3518 {
3519 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003520 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003521
3522 if( bit < 64 ) /* Always true, but be extra sure */
3523 ssl->in_window |= (uint64_t) 1 << bit;
3524 }
3525}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003527
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003528#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003529/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003530static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3531
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003532/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003533 * Without any SSL context, check if a datagram looks like a ClientHello with
3534 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003535 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003536 *
3537 * - if cookie is valid, return 0
3538 * - if ClientHello looks superficially valid but cookie is not,
3539 * fill obuf and set olen, then
3540 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3541 * - otherwise return a specific error code
3542 */
3543static int ssl_check_dtls_clihlo_cookie(
3544 mbedtls_ssl_cookie_write_t *f_cookie_write,
3545 mbedtls_ssl_cookie_check_t *f_cookie_check,
3546 void *p_cookie,
3547 const unsigned char *cli_id, size_t cli_id_len,
3548 const unsigned char *in, size_t in_len,
3549 unsigned char *obuf, size_t buf_len, size_t *olen )
3550{
3551 size_t sid_len, cookie_len;
3552 unsigned char *p;
3553
3554 if( f_cookie_write == NULL || f_cookie_check == NULL )
3555 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3556
3557 /*
3558 * Structure of ClientHello with record and handshake headers,
3559 * and expected values. We don't need to check a lot, more checks will be
3560 * done when actually parsing the ClientHello - skipping those checks
3561 * avoids code duplication and does not make cookie forging any easier.
3562 *
3563 * 0-0 ContentType type; copied, must be handshake
3564 * 1-2 ProtocolVersion version; copied
3565 * 3-4 uint16 epoch; copied, must be 0
3566 * 5-10 uint48 sequence_number; copied
3567 * 11-12 uint16 length; (ignored)
3568 *
3569 * 13-13 HandshakeType msg_type; (ignored)
3570 * 14-16 uint24 length; (ignored)
3571 * 17-18 uint16 message_seq; copied
3572 * 19-21 uint24 fragment_offset; copied, must be 0
3573 * 22-24 uint24 fragment_length; (ignored)
3574 *
3575 * 25-26 ProtocolVersion client_version; (ignored)
3576 * 27-58 Random random; (ignored)
3577 * 59-xx SessionID session_id; 1 byte len + sid_len content
3578 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3579 * ...
3580 *
3581 * Minimum length is 61 bytes.
3582 */
3583 if( in_len < 61 ||
3584 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3585 in[3] != 0 || in[4] != 0 ||
3586 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3587 {
3588 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3589 }
3590
3591 sid_len = in[59];
3592 if( sid_len > in_len - 61 )
3593 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3594
3595 cookie_len = in[60 + sid_len];
3596 if( cookie_len > in_len - 60 )
3597 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3598
3599 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3600 cli_id, cli_id_len ) == 0 )
3601 {
3602 /* Valid cookie */
3603 return( 0 );
3604 }
3605
3606 /*
3607 * If we get here, we've got an invalid cookie, let's prepare HVR.
3608 *
3609 * 0-0 ContentType type; copied
3610 * 1-2 ProtocolVersion version; copied
3611 * 3-4 uint16 epoch; copied
3612 * 5-10 uint48 sequence_number; copied
3613 * 11-12 uint16 length; olen - 13
3614 *
3615 * 13-13 HandshakeType msg_type; hello_verify_request
3616 * 14-16 uint24 length; olen - 25
3617 * 17-18 uint16 message_seq; copied
3618 * 19-21 uint24 fragment_offset; copied
3619 * 22-24 uint24 fragment_length; olen - 25
3620 *
3621 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3622 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3623 *
3624 * Minimum length is 28.
3625 */
3626 if( buf_len < 28 )
3627 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3628
3629 /* Copy most fields and adapt others */
3630 memcpy( obuf, in, 25 );
3631 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3632 obuf[25] = 0xfe;
3633 obuf[26] = 0xff;
3634
3635 /* Generate and write actual cookie */
3636 p = obuf + 28;
3637 if( f_cookie_write( p_cookie,
3638 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3639 {
3640 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3641 }
3642
3643 *olen = p - obuf;
3644
3645 /* Go back and fill length fields */
3646 obuf[27] = (unsigned char)( *olen - 28 );
3647
3648 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3649 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3650 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3651
3652 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3653 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3654
3655 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3656}
3657
3658/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003659 * Handle possible client reconnect with the same UDP quadruplet
3660 * (RFC 6347 Section 4.2.8).
3661 *
3662 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3663 * that looks like a ClientHello.
3664 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003665 * - if the input looks like a ClientHello without cookies,
3666 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003667 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003668 * - if the input looks like a ClientHello with a valid cookie,
3669 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003670 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003671 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003672 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003673 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003674 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3675 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003676 */
3677static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3678{
3679 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003680 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003681
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003682 /* Use out_msg as temporary buffer for writing out HelloVerifyRequest,
3683 * because the output buffer's already around. Don't use out_buf though,
3684 * as we don't want to overwrite out_ctr. */
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003685 ret = ssl_check_dtls_clihlo_cookie(
3686 ssl->conf->f_cookie_write,
3687 ssl->conf->f_cookie_check,
3688 ssl->conf->p_cookie,
3689 ssl->cli_id, ssl->cli_id_len,
3690 ssl->in_buf, ssl->in_left,
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003691 ssl->out_msg, MBEDTLS_SSL_MAX_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003692
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003693 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3694
3695 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003696 {
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003697 int send_ret;
3698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3699 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003700 ssl->out_msg, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003701 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003702 * If the error is permanent we'll catch it later,
3703 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003704 send_ret = ssl->f_send( ssl->p_bio, ssl->out_msg, len );
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003705 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3706 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003707
3708 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003709 }
3710
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003711 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003712 {
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003714 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3715 {
3716 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3717 return( ret );
3718 }
3719
3720 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003721 }
3722
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003723 return( ret );
3724}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003725#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003726
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003727/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003728 * ContentType type;
3729 * ProtocolVersion version;
3730 * uint16 epoch; // DTLS only
3731 * uint48 sequence_number; // DTLS only
3732 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003733 *
3734 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003735 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003736 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3737 *
3738 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003739 * 1. proceed with the record if this function returns 0
3740 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3741 * 3. return CLIENT_RECONNECT if this function return that value
3742 * 4. drop the whole datagram if this function returns anything else.
3743 * Point 2 is needed when the peer is resending, and we have already received
3744 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003746static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003747{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003748 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750 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 +02003751
Paul Bakker5121ce52009-01-03 21:22:43 +00003752 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003753 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003754 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003757 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003758 ssl->in_msgtype,
3759 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003760
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003761 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3763 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3764 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3765 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003768
3769#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01003770 /* Silently ignore invalid DTLS records as recommended by RFC 6347
3771 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003772 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3773#endif /* MBEDTLS_SSL_PROTO_DTLS */
3774 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3775 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
3776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003778 }
3779
3780 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003781 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3784 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003785 }
3786
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003787 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3790 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003791 }
3792
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003793 /* Check length against the size of our buffer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003794 if( ssl->in_msglen > MBEDTLS_SSL_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003795 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3798 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003799 }
3800
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003801 /*
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003802 * DTLS-related tests.
3803 * Check epoch before checking length constraint because
3804 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3805 * message gets duplicated before the corresponding Finished message,
3806 * the second ChangeCipherSpec should be discarded because it belongs
3807 * to an old epoch, but not because its length is shorter than
3808 * the minimum record length for packets using the new record transform.
3809 * Note that these two kinds of failures are handled differently,
3810 * as an unexpected record is silently skipped but an invalid
3811 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003812 */
3813#if defined(MBEDTLS_SSL_PROTO_DTLS)
3814 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3815 {
3816 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3817
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003818 /* Check epoch (and sequence number) with DTLS */
3819 if( rec_epoch != ssl->in_epoch )
3820 {
3821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3822 "expected %d, received %d",
3823 ssl->in_epoch, rec_epoch ) );
3824
3825#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3826 /*
3827 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3828 * access the first byte of record content (handshake type), as we
3829 * have an active transform (possibly iv_len != 0), so use the
3830 * fact that the record header len is 13 instead.
3831 */
3832 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3833 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3834 rec_epoch == 0 &&
3835 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3836 ssl->in_left > 13 &&
3837 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3838 {
3839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3840 "from the same port" ) );
3841 return( ssl_handle_possible_reconnect( ssl ) );
3842 }
3843 else
3844#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3845 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3846 }
3847
3848#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3849 /* Replay detection only works for the current epoch */
3850 if( rec_epoch == ssl->in_epoch &&
3851 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3852 {
3853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3854 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3855 }
3856#endif
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003857
3858 /* Drop unexpected ChangeCipherSpec messages */
3859 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3860 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3861 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3862 {
3863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3864 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3865 }
3866
3867 /* Drop unexpected ApplicationData records,
3868 * except at the beginning of renegotiations */
3869 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
3870 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
3871#if defined(MBEDTLS_SSL_RENEGOTIATION)
3872 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
3873 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
3874#endif
3875 )
3876 {
3877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3878 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3879 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003880 }
3881#endif /* MBEDTLS_SSL_PROTO_DTLS */
3882
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003883
3884 /* Check length against bounds of the current transform and version */
3885 if( ssl->transform_in == NULL )
3886 {
3887 if( ssl->in_msglen < 1 ||
3888 ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
3889 {
3890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3891 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3892 }
3893 }
3894 else
3895 {
3896 if( ssl->in_msglen < ssl->transform_in->minlen )
3897 {
3898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3899 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3900 }
3901
3902#if defined(MBEDTLS_SSL_PROTO_SSL3)
3903 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3904 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_MAX_CONTENT_LEN )
3905 {
3906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3907 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3908 }
3909#endif
3910#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3911 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3912 /*
3913 * TLS encrypted messages can have up to 256 bytes of padding
3914 */
3915 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
3916 ssl->in_msglen > ssl->transform_in->minlen +
3917 MBEDTLS_SSL_MAX_CONTENT_LEN + 256 )
3918 {
3919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3920 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3921 }
3922#endif
3923 }
3924
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003925 return( 0 );
3926}
Paul Bakker5121ce52009-01-03 21:22:43 +00003927
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003928/*
3929 * If applicable, decrypt (and decompress) record content
3930 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003931static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003932{
3933 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003935 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
3936 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3939 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 ret = mbedtls_ssl_hw_record_read( ssl );
3944 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003946 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3947 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003948 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003949
3950 if( ret == 0 )
3951 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003952 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003954 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003955 {
3956 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003959 return( ret );
3960 }
3961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00003963 ssl->in_msg, ssl->in_msglen );
3964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003965 if( ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3968 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003969 }
3970 }
3971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003973 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003974 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003975 {
3976 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003979 return( ret );
3980 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00003981 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003985 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003987 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003988 }
3989#endif
3990
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003991 return( 0 );
3992}
3993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003995
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003996/*
3997 * Read a record.
3998 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003999 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4000 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4001 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004004{
4005 int ret;
4006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004008
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004009 if( ssl->keep_current_message == 0 )
4010 {
4011 do {
Simon Butcher99000142016-10-13 17:21:01 +01004012
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004013 if( ( ret = mbedtls_ssl_read_record_layer( ssl ) ) != 0 )
4014 {
4015 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4016 return( ret );
4017 }
4018
4019 ret = mbedtls_ssl_handle_message_type( ssl );
4020
4021 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret );
4022
4023 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004024 {
4025 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4026 return( ret );
4027 }
4028
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004029 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4030 {
4031 mbedtls_ssl_update_handshake_status( ssl );
4032 }
Simon Butcher99000142016-10-13 17:21:01 +01004033 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004034 else
Simon Butcher99000142016-10-13 17:21:01 +01004035 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4037 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004038 }
4039
4040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4041
4042 return( 0 );
4043}
4044
4045int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4046{
4047 int ret;
4048
Hanno Becker4a810fb2017-05-24 16:27:30 +01004049 /*
4050 * Step A
4051 *
4052 * Consume last content-layer message and potentially
4053 * update in_msglen which keeps track of the contents'
4054 * consumption state.
4055 *
4056 * (1) Handshake messages:
4057 * Remove last handshake message, move content
4058 * and adapt in_msglen.
4059 *
4060 * (2) Alert messages:
4061 * Consume whole record content, in_msglen = 0.
4062 *
4063 * NOTE: This needs to be fixed, since like for
4064 * handshake messages it is allowed to have
4065 * multiple alerts witin a single record.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004066 * Internal reference IOTSSL-1321.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004067 *
4068 * (3) Change cipher spec:
4069 * Consume whole record content, in_msglen = 0.
4070 *
4071 * (4) Application data:
4072 * Don't do anything - the record layer provides
4073 * the application data as a stream transport
4074 * and consumes through mbedtls_ssl_read only.
4075 *
4076 */
4077
4078 /* Case (1): Handshake messages */
4079 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004080 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004081 /* Hard assertion to be sure that no application data
4082 * is in flight, as corrupting ssl->in_msglen during
4083 * ssl->in_offt != NULL is fatal. */
4084 if( ssl->in_offt != NULL )
4085 {
4086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4087 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4088 }
4089
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004090 /*
4091 * Get next Handshake message in the current record
4092 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004093
Hanno Becker4a810fb2017-05-24 16:27:30 +01004094 /* Notes:
4095 * (1) in_hslen is *NOT* necessarily the size of the
4096 * current handshake content: If DTLS handshake
4097 * fragmentation is used, that's the fragment
4098 * size instead. Using the total handshake message
4099 * size here is FAULTY and should be changed at
4100 * some point. Internal reference IOTSSL-1414.
4101 * (2) While it doesn't seem to cause problems, one
4102 * has to be very careful not to assume that in_hslen
4103 * is always <= in_msglen in a sensible communication.
4104 * Again, it's wrong for DTLS handshake fragmentation.
4105 * The following check is therefore mandatory, and
4106 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004107 * Additionally, ssl->in_hslen might be arbitrarily out of
4108 * bounds after handling a DTLS message with an unexpected
4109 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004110 */
4111 if( ssl->in_hslen < ssl->in_msglen )
4112 {
4113 ssl->in_msglen -= ssl->in_hslen;
4114 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4115 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004116
Hanno Becker4a810fb2017-05-24 16:27:30 +01004117 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4118 ssl->in_msg, ssl->in_msglen );
4119 }
4120 else
4121 {
4122 ssl->in_msglen = 0;
4123 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004124
Hanno Becker4a810fb2017-05-24 16:27:30 +01004125 ssl->in_hslen = 0;
4126 }
4127 /* Case (4): Application data */
4128 else if( ssl->in_offt != NULL )
4129 {
4130 return( 0 );
4131 }
4132 /* Everything else (CCS & Alerts) */
4133 else
4134 {
4135 ssl->in_msglen = 0;
4136 }
4137
4138 /*
4139 * Step B
4140 *
4141 * Fetch and decode new record if current one is fully consumed.
4142 *
4143 */
4144
4145 if( ssl->in_msglen > 0 )
4146 {
4147 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004148 return( 0 );
4149 }
4150
Hanno Becker4a810fb2017-05-24 16:27:30 +01004151 /* Need to fetch a new record */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004152
Simon Butcher99000142016-10-13 17:21:01 +01004153#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004154read_record_header:
Simon Butcher99000142016-10-13 17:21:01 +01004155#endif
4156
Hanno Becker4a810fb2017-05-24 16:27:30 +01004157 /* Current record either fully processed or to be discarded. */
4158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004161 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004162 return( ret );
4163 }
4164
4165 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004168 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4169 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004170 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004171 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4172 {
4173 /* Skip unexpected record (but not whole datagram) */
4174 ssl->next_record_offset = ssl->in_msglen
4175 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004176
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4178 "(header)" ) );
4179 }
4180 else
4181 {
4182 /* Skip invalid record and the rest of the datagram */
4183 ssl->next_record_offset = 0;
4184 ssl->in_left = 0;
4185
4186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4187 "(header)" ) );
4188 }
4189
4190 /* Get next record */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004191 goto read_record_header;
4192 }
4193#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004194 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004195 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004196
4197 /*
4198 * Read and optionally decrypt the message contents
4199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4201 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004204 return( ret );
4205 }
4206
4207 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004208#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004209 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004210 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004211 else
4212#endif
4213 ssl->in_left = 0;
4214
4215 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004217#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004218 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004219 {
4220 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004221 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4222 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004223 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004224 /* Except when waiting for Finished as a bad mac here
4225 * probably means something went wrong in the handshake
4226 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4227 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4228 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4229 {
4230#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4231 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4232 {
4233 mbedtls_ssl_send_alert_message( ssl,
4234 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4235 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4236 }
4237#endif
4238 return( ret );
4239 }
4240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004241#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004242 if( ssl->conf->badmac_limit != 0 &&
4243 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4246 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004247 }
4248#endif
4249
Hanno Becker4a810fb2017-05-24 16:27:30 +01004250 /* As above, invalid records cause
4251 * dismissal of the whole datagram. */
4252
4253 ssl->next_record_offset = 0;
4254 ssl->in_left = 0;
4255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004257 goto read_record_header;
4258 }
4259
4260 return( ret );
4261 }
4262 else
4263#endif
4264 {
4265 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4267 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004269 mbedtls_ssl_send_alert_message( ssl,
4270 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4271 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004272 }
4273#endif
4274 return( ret );
4275 }
4276 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004277
4278 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004279 * When we sent the last flight of the handshake, we MUST respond to a
4280 * retransmit of the peer's previous flight with a retransmit. (In
4281 * practice, only the Finished message will make it, other messages
4282 * including CCS use the old transform so they're dropped as invalid.)
4283 *
4284 * If the record we received is not a handshake message, however, it
4285 * means the peer received our last flight so we can clean up
4286 * handshake info.
4287 *
4288 * This check needs to be done before prepare_handshake() due to an edge
4289 * case: if the client immediately requests renegotiation, this
4290 * finishes the current handshake first, avoiding the new ClientHello
4291 * being mistaken for an ancient message in the current handshake.
4292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004294 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004295 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004296 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4299 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004303 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004306 return( ret );
4307 }
4308
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01004309 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004310 }
4311 else
4312 {
4313 ssl_handshake_wrapup_free_hs_transform( ssl );
4314 }
4315 }
4316#endif
4317
Simon Butcher99000142016-10-13 17:21:01 +01004318 return( 0 );
4319}
4320
4321int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4322{
4323 int ret;
4324
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004325 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004326 * Handle particular types of records
4327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004328 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004329 {
Simon Butcher99000142016-10-13 17:21:01 +01004330 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4331 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004332 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004333 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004334 }
4335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004337 {
Angus Gratton8946b0d2018-06-20 15:43:50 +10004338 if( ssl->in_msglen != 2 )
4339 {
4340 /* Note: Standard allows for more than one 2 byte alert
4341 to be packed in a single message, but Mbed TLS doesn't
4342 currently support this. */
4343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4344 ssl->in_msglen ) );
4345 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4346 }
4347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004349 ssl->in_msg[0], ssl->in_msg[1] ) );
4350
4351 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004352 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004357 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004358 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 }
4360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004361 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4362 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4365 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004366 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004367
4368#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4369 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4370 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4371 {
4372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4373 /* Will be handled when trying to parse ServerHello */
4374 return( 0 );
4375 }
4376#endif
4377
4378#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4379 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4380 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4381 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4382 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4383 {
4384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4385 /* Will be handled in mbedtls_ssl_parse_certificate() */
4386 return( 0 );
4387 }
4388#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4389
4390 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004391 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004392 }
4393
Paul Bakker5121ce52009-01-03 21:22:43 +00004394 return( 0 );
4395}
4396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004398{
4399 int ret;
4400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4402 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4403 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004404 {
4405 return( ret );
4406 }
4407
4408 return( 0 );
4409}
4410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004412 unsigned char level,
4413 unsigned char message )
4414{
4415 int ret;
4416
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004417 if( ssl == NULL || ssl->conf == NULL )
4418 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004421 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004424 ssl->out_msglen = 2;
4425 ssl->out_msg[0] = level;
4426 ssl->out_msg[1] = message;
4427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004428 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004431 return( ret );
4432 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004434
4435 return( 0 );
4436}
4437
Paul Bakker5121ce52009-01-03 21:22:43 +00004438/*
4439 * Handshake functions
4440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004441#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4442 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4443 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4444 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4445 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4446 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4447 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004448/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004450{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4456 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004457 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4458 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004461 ssl->state++;
4462 return( 0 );
4463 }
4464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4466 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004467}
4468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004470{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004475 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4476 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004477 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4478 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004481 ssl->state++;
4482 return( 0 );
4483 }
4484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4486 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004487}
Gilles Peskinef9828522017-05-03 12:28:43 +02004488
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004489#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004490/* Some certificate support -> implement write and parse */
4491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004493{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004494 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004495 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004496 const mbedtls_x509_crt *crt;
4497 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004501 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4502 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004503 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4504 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004507 ssl->state++;
4508 return( 0 );
4509 }
4510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004511#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004512 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004513 {
4514 if( ssl->client_auth == 0 )
4515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004517 ssl->state++;
4518 return( 0 );
4519 }
4520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004521#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004522 /*
4523 * If using SSLv3 and got no cert, send an Alert message
4524 * (otherwise an empty Certificate message will be sent).
4525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4527 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004528 {
4529 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4531 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4532 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004535 goto write_msg;
4536 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004537#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004538 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539#endif /* MBEDTLS_SSL_CLI_C */
4540#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004541 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4546 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004547 }
4548 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004549#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004552
4553 /*
4554 * 0 . 0 handshake type
4555 * 1 . 3 handshake length
4556 * 4 . 6 length of all certs
4557 * 7 . 9 length of cert. 1
4558 * 10 . n-1 peer certificate
4559 * n . n+2 length of cert. 2
4560 * n+3 . ... upper level cert, etc.
4561 */
4562 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004563 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004564
Paul Bakker29087132010-03-21 21:03:34 +00004565 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004566 {
4567 n = crt->raw.len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 if( n > MBEDTLS_SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
4571 i + 3 + n, MBEDTLS_SSL_MAX_CONTENT_LEN ) );
4572 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004573 }
4574
4575 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4576 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4577 ssl->out_msg[i + 2] = (unsigned char)( n );
4578
4579 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4580 i += n; crt = crt->next;
4581 }
4582
4583 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4584 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4585 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4586
4587 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004588 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4589 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004590
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004591#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004592write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004593#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004594
4595 ssl->state++;
4596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004600 return( ret );
4601 }
4602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004604
Paul Bakkered27a042013-04-18 22:46:23 +02004605 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004606}
4607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004608int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004609{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004610 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004611 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004612 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004613 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004614 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004618 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4619 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004620 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4621 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004624 ssl->state++;
4625 return( 0 );
4626 }
4627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004628#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004629 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004630 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4631 {
4632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4633 ssl->state++;
4634 return( 0 );
4635 }
4636
4637#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4638 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4639 authmode = ssl->handshake->sni_authmode;
4640#endif
4641
4642 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4643 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004644 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004645 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004647 ssl->state++;
4648 return( 0 );
4649 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004650#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004652 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004653 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004654 /* mbedtls_ssl_read_record may have sent an alert already. We
4655 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004657 return( ret );
4658 }
4659
4660 ssl->state++;
4661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662#if defined(MBEDTLS_SSL_SRV_C)
4663#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004664 /*
4665 * Check if the client sent an empty certificate
4666 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004667 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004668 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004669 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004670 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004671 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4672 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4673 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004676
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004677 /* The client was asked for a certificate but didn't send
4678 one. The client should know what's going on, so we
4679 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004680 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004681 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004682 return( 0 );
4683 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004684 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004685 }
4686 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004687#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004689#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4690 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004691 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004692 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004694 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4695 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4696 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4697 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004700
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004701 /* The client was asked for a certificate but didn't send
4702 one. The client should know what's going on, so we
4703 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004704 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004705 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004706 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004707 else
4708 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004709 }
4710 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004711#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4712 MBEDTLS_SSL_PROTO_TLS1_2 */
4713#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004715 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004718 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4719 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004720 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004721 }
4722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4724 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004727 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4728 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004729 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004730 }
4731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004732 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004733
Paul Bakker5121ce52009-01-03 21:22:43 +00004734 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004735 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004736 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004737 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004738
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004739 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004740 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004743 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4744 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004745 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004746 }
4747
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004748 /* In case we tried to reuse a session but it failed */
4749 if( ssl->session_negotiate->peer_cert != NULL )
4750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4752 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004753 }
4754
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004755 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004757 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004760 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4761 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004762 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004763 }
4764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004766
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004767 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004768
4769 while( i < ssl->in_hslen )
4770 {
Philippe Antoine33e5c322018-07-09 10:39:02 +02004771 if ( i + 3 > ssl->in_hslen ) {
4772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4773 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4774 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4775 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
4776 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004777 if( ssl->in_msg[i] != 0 )
4778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004780 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4781 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004783 }
4784
4785 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4786 | (unsigned int) ssl->in_msg[i + 2];
4787 i += 3;
4788
4789 if( n < 128 || i + n > ssl->in_hslen )
4790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004792 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4793 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004795 }
4796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004798 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004799 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004800 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004801 case 0: /*ok*/
4802 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
4803 /* Ignore certificate with an unknown algorithm: maybe a
4804 prior certificate was already trusted. */
4805 break;
4806
4807 case MBEDTLS_ERR_X509_ALLOC_FAILED:
4808 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
4809 goto crt_parse_der_failed;
4810
4811 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
4812 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4813 goto crt_parse_der_failed;
4814
4815 default:
4816 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4817 crt_parse_der_failed:
4818 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004820 return( ret );
4821 }
4822
4823 i += n;
4824 }
4825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004826 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004827
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004828 /*
4829 * On client, make sure the server cert doesn't change during renego to
4830 * avoid "triple handshake" attack: https://secure-resumption.com/
4831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004832#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004833 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004834 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004835 {
4836 if( ssl->session->peer_cert == NULL )
4837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004839 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4840 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004842 }
4843
4844 if( ssl->session->peer_cert->raw.len !=
4845 ssl->session_negotiate->peer_cert->raw.len ||
4846 memcmp( ssl->session->peer_cert->raw.p,
4847 ssl->session_negotiate->peer_cert->raw.p,
4848 ssl->session->peer_cert->raw.len ) != 0 )
4849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004851 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4852 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004853 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004854 }
4855 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004857
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004858 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004859 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004860 mbedtls_x509_crt *ca_chain;
4861 mbedtls_x509_crl *ca_crl;
4862
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004863#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004864 if( ssl->handshake->sni_ca_chain != NULL )
4865 {
4866 ca_chain = ssl->handshake->sni_ca_chain;
4867 ca_crl = ssl->handshake->sni_ca_crl;
4868 }
4869 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004870#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004871 {
4872 ca_chain = ssl->conf->ca_chain;
4873 ca_crl = ssl->conf->ca_crl;
4874 }
4875
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004876 /*
4877 * Main check: verify certificate
4878 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004879 ret = mbedtls_x509_crt_verify_with_profile(
4880 ssl->session_negotiate->peer_cert,
4881 ca_chain, ca_crl,
4882 ssl->conf->cert_profile,
4883 ssl->hostname,
4884 &ssl->session_negotiate->verify_result,
4885 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004886
4887 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004889 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004890 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004891
4892 /*
4893 * Secondary checks: always done, but change 'ret' only if it was 0
4894 */
4895
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004896#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004899
4900 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004901 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004902 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004903 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004904 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
4905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004907 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004908 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004909 }
4910 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004911#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004913 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004914 ciphersuite_info,
4915 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004916 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004919 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004921 }
4922
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004923 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
4924 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
4925 * with details encoded in the verification flags. All other kinds
4926 * of error codes, including those from the user provided f_vrfy
4927 * functions, are treated as fatal and lead to a failure of
4928 * ssl_parse_certificate even if verification was optional. */
4929 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
4930 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
4931 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
4932 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004933 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004934 }
4935
4936 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
4937 {
4938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
4939 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
4940 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004941
4942 if( ret != 0 )
4943 {
4944 /* The certificate may have been rejected for several reasons.
4945 Pick one and send the corresponding alert. Which alert to send
4946 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004947 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
4948 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
4949 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
4950 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4951 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
4952 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4953 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
4954 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4955 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
4956 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4957 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
4958 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4959 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
4960 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4961 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
4962 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
4963 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
4964 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
4965 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
4966 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02004967 else
4968 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004969 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4970 alert );
4971 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004972
Hanno Beckere6706e62017-05-15 16:05:15 +01004973#if defined(MBEDTLS_DEBUG_C)
4974 if( ssl->session_negotiate->verify_result != 0 )
4975 {
4976 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
4977 ssl->session_negotiate->verify_result ) );
4978 }
4979 else
4980 {
4981 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
4982 }
4983#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004984 }
4985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004987
4988 return( ret );
4989}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004990#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
4991 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
4992 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
4993 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4994 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4995 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
4996 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004998int mbedtls_ssl_write_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, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005004 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005005 ssl->out_msglen = 1;
5006 ssl->out_msg[0] = 1;
5007
Paul Bakker5121ce52009-01-03 21:22:43 +00005008 ssl->state++;
5009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005010 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005012 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005013 return( ret );
5014 }
5015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005017
5018 return( 0 );
5019}
5020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005021int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005022{
5023 int ret;
5024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005027 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005029 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005030 return( ret );
5031 }
5032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005033 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005036 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5037 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005038 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005039 }
5040
5041 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005044 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5045 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005046 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005047 }
5048
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005049 /*
5050 * Switch to our negotiated transform and session parameters for inbound
5051 * data.
5052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005053 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005054 ssl->transform_in = ssl->transform_negotiate;
5055 ssl->session_in = ssl->session_negotiate;
5056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005057#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005058 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005061 ssl_dtls_replay_reset( ssl );
5062#endif
5063
5064 /* Increment epoch */
5065 if( ++ssl->in_epoch == 0 )
5066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005068 /* This is highly unlikely to happen for legitimate reasons, so
5069 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005070 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005071 }
5072 }
5073 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005075 memset( ssl->in_ctr, 0, 8 );
5076
5077 /*
5078 * Set the in_msg pointer to the correct location based on IV length
5079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005080 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005081 {
5082 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
5083 ssl->transform_negotiate->fixed_ivlen;
5084 }
5085 else
5086 ssl->in_msg = ssl->in_iv;
5087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005088#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5089 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005091 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005093 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005094 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5095 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005096 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005097 }
5098 }
5099#endif
5100
Paul Bakker5121ce52009-01-03 21:22:43 +00005101 ssl->state++;
5102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005104
5105 return( 0 );
5106}
5107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005108void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5109 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005110{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005111 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005113#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5114 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5115 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005116 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005117 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005118#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005119#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5120#if defined(MBEDTLS_SHA512_C)
5121 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005122 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5123 else
5124#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125#if defined(MBEDTLS_SHA256_C)
5126 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005127 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005128 else
5129#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005133 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005134 }
Paul Bakker380da532012-04-18 16:10:25 +00005135}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005138{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005139#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5140 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005141 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5142 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005144#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5145#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005146 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005147#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005148#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005149 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005150#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005151#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005152}
5153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005154static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005155 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005157#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5158 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005159 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5160 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005161#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5163#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005164 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005165#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005166#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005167 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005168#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005169#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005170}
5171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005172#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5173 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5174static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005175 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005176{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005177 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5178 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005179}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005180#endif
Paul Bakker380da532012-04-18 16:10:25 +00005181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005182#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5183#if defined(MBEDTLS_SHA256_C)
5184static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005185 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005186{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005187 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005188}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005189#endif
Paul Bakker380da532012-04-18 16:10:25 +00005190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005191#if defined(MBEDTLS_SHA512_C)
5192static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005193 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005194{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005195 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005196}
Paul Bakker769075d2012-11-24 11:26:46 +01005197#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005198#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005201static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005202 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005203{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005204 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005205 mbedtls_md5_context md5;
5206 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005207
Paul Bakker5121ce52009-01-03 21:22:43 +00005208 unsigned char padbuf[48];
5209 unsigned char md5sum[16];
5210 unsigned char sha1sum[20];
5211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005213 if( !session )
5214 session = ssl->session;
5215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005217
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005218 mbedtls_md5_init( &md5 );
5219 mbedtls_sha1_init( &sha1 );
5220
5221 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5222 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005223
5224 /*
5225 * SSLv3:
5226 * hash =
5227 * MD5( master + pad2 +
5228 * MD5( handshake + sender + master + pad1 ) )
5229 * + SHA1( master + pad2 +
5230 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005231 */
5232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005233#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005234 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5235 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005236#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005238#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005239 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5240 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005241#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005243 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005244 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005245
Paul Bakker1ef83d62012-04-11 12:09:53 +00005246 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005247
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005248 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5249 mbedtls_md5_update_ret( &md5, session->master, 48 );
5250 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5251 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005252
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005253 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5254 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5255 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5256 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005257
Paul Bakker1ef83d62012-04-11 12:09:53 +00005258 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005259
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005260 mbedtls_md5_starts_ret( &md5 );
5261 mbedtls_md5_update_ret( &md5, session->master, 48 );
5262 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5263 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5264 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005265
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005266 mbedtls_sha1_starts_ret( &sha1 );
5267 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5268 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5269 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5270 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005272 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005273
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005274 mbedtls_md5_free( &md5 );
5275 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
5278 mbedtls_zeroize( md5sum, sizeof( md5sum ) );
5279 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005282}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005286static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005287 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005288{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005289 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005290 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005291 mbedtls_md5_context md5;
5292 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005293 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005296 if( !session )
5297 session = ssl->session;
5298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005300
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005301 mbedtls_md5_init( &md5 );
5302 mbedtls_sha1_init( &sha1 );
5303
5304 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5305 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005306
Paul Bakker1ef83d62012-04-11 12:09:53 +00005307 /*
5308 * TLSv1:
5309 * hash = PRF( master, finished_label,
5310 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5311 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005314 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5315 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005316#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005318#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005319 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5320 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005321#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005324 ? "client finished"
5325 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005326
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005327 mbedtls_md5_finish_ret( &md5, padbuf );
5328 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005329
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005330 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005331 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005334
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005335 mbedtls_md5_free( &md5 );
5336 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005338 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005341}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005342#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5345#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005346static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005348{
5349 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005350 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005351 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005352 unsigned char padbuf[32];
5353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005354 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005355 if( !session )
5356 session = ssl->session;
5357
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005358 mbedtls_sha256_init( &sha256 );
5359
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005361
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005362 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005363
5364 /*
5365 * TLSv1.2:
5366 * hash = PRF( master, finished_label,
5367 * Hash( handshake ) )[0.11]
5368 */
5369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370#if !defined(MBEDTLS_SHA256_ALT)
5371 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005372 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005373#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005375 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005376 ? "client finished"
5377 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005378
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005379 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005380
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005381 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005382 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005384 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005385
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005386 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005391}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005394#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005395static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005397{
5398 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005399 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005400 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005401 unsigned char padbuf[48];
5402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005404 if( !session )
5405 session = ssl->session;
5406
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005407 mbedtls_sha512_init( &sha512 );
5408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005410
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005411 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005412
5413 /*
5414 * TLSv1.2:
5415 * hash = PRF( master, finished_label,
5416 * Hash( handshake ) )[0.11]
5417 */
5418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005420 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5421 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005422#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005425 ? "client finished"
5426 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005427
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005428 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005429
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005430 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005431 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005433 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005434
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005435 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005440}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005441#endif /* MBEDTLS_SHA512_C */
5442#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005444static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005445{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005447
5448 /*
5449 * Free our handshake params
5450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 mbedtls_ssl_handshake_free( ssl->handshake );
5452 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005453 ssl->handshake = NULL;
5454
5455 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005456 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005457 */
5458 if( ssl->transform )
5459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460 mbedtls_ssl_transform_free( ssl->transform );
5461 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005462 }
5463 ssl->transform = ssl->transform_negotiate;
5464 ssl->transform_negotiate = NULL;
5465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005467}
5468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005470{
5471 int resume = ssl->handshake->resume;
5472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475#if defined(MBEDTLS_SSL_RENEGOTIATION)
5476 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005479 ssl->renego_records_seen = 0;
5480 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005481#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005482
5483 /*
5484 * Free the previous session and switch in the current one
5485 */
Paul Bakker0a597072012-09-25 21:55:46 +00005486 if( ssl->session )
5487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005489 /* RFC 7366 3.1: keep the EtM state */
5490 ssl->session_negotiate->encrypt_then_mac =
5491 ssl->session->encrypt_then_mac;
5492#endif
5493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 mbedtls_ssl_session_free( ssl->session );
5495 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005496 }
5497 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005498 ssl->session_negotiate = NULL;
5499
Paul Bakker0a597072012-09-25 21:55:46 +00005500 /*
5501 * Add cache entry
5502 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005503 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005504 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005505 resume == 0 )
5506 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005507 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005509 }
Paul Bakker0a597072012-09-25 21:55:46 +00005510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005512 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005513 ssl->handshake->flight != NULL )
5514 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005515 /* Cancel handshake timer */
5516 ssl_set_timer( ssl, 0 );
5517
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005518 /* Keep last flight around in case we need to resend it:
5519 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005520 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005521 }
5522 else
5523#endif
5524 ssl_handshake_wrapup_free_hs_transform( ssl );
5525
Paul Bakker48916f92012-09-16 19:57:18 +00005526 ssl->state++;
5527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005529}
5530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005531int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005532{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005533 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005536
Paul Bakker92be97b2013-01-02 17:30:03 +01005537 /*
5538 * Set the out_msg pointer to the correct location based on IV length
5539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005540 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005541 {
5542 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5543 ssl->transform_negotiate->fixed_ivlen;
5544 }
5545 else
5546 ssl->out_msg = ssl->out_iv;
5547
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005548 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005549
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005550 /*
5551 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5552 * may define some other value. Currently (early 2016), no defined
5553 * ciphersuite does this (and this is unlikely to change as activity has
5554 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005559 ssl->verify_data_len = hash_len;
5560 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005561#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005562
Paul Bakker5121ce52009-01-03 21:22:43 +00005563 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005564 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5565 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005566
5567 /*
5568 * In case of session resuming, invert the client and server
5569 * ChangeCipherSpec messages order.
5570 */
Paul Bakker0a597072012-09-25 21:55:46 +00005571 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005573#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005574 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005575 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005576#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005578 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005580#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005581 }
5582 else
5583 ssl->state++;
5584
Paul Bakker48916f92012-09-16 19:57:18 +00005585 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005586 * Switch to our negotiated transform and session parameters for outbound
5587 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005591#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005592 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005593 {
5594 unsigned char i;
5595
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005596 /* Remember current epoch settings for resending */
5597 ssl->handshake->alt_transform_out = ssl->transform_out;
5598 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5599
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005600 /* Set sequence_number to zero */
5601 memset( ssl->out_ctr + 2, 0, 6 );
5602
5603 /* Increment epoch */
5604 for( i = 2; i > 0; i-- )
5605 if( ++ssl->out_ctr[i - 1] != 0 )
5606 break;
5607
5608 /* The loop goes to its end iff the counter is wrapping */
5609 if( i == 0 )
5610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5612 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005613 }
5614 }
5615 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005617 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005618
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005619 ssl->transform_out = ssl->transform_negotiate;
5620 ssl->session_out = ssl->session_negotiate;
5621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5623 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5628 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005629 }
5630 }
5631#endif
5632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005634 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005636#endif
5637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005641 return( ret );
5642 }
5643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005645
5646 return( 0 );
5647}
5648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005650#define SSL_MAX_HASH_LEN 36
5651#else
5652#define SSL_MAX_HASH_LEN 12
5653#endif
5654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005656{
Paul Bakker23986e52011-04-24 08:57:21 +00005657 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005658 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005659 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005662
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005663 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005665 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005668 return( ret );
5669 }
5670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005671 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005674 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5675 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005677 }
5678
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005679 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005680#if defined(MBEDTLS_SSL_PROTO_SSL3)
5681 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005682 hash_len = 36;
5683 else
5684#endif
5685 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5688 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005691 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5692 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005693 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005694 }
5695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005696 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005697 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005700 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5701 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005703 }
5704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005705#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005706 ssl->verify_data_len = hash_len;
5707 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005708#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005709
Paul Bakker0a597072012-09-25 21:55:46 +00005710 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005713 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005715#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005717 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005718 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005719#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005720 }
5721 else
5722 ssl->state++;
5723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005724#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005725 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005726 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005727#endif
5728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005730
5731 return( 0 );
5732}
5733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005735{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005736 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5739 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5740 mbedtls_md5_init( &handshake->fin_md5 );
5741 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005742 mbedtls_md5_starts_ret( &handshake->fin_md5 );
5743 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005744#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005745#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5746#if defined(MBEDTLS_SHA256_C)
5747 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005748 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005749#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750#if defined(MBEDTLS_SHA512_C)
5751 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005752 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005753#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005755
5756 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005757
5758#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5759 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5760 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5761#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763#if defined(MBEDTLS_DHM_C)
5764 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005765#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005766#if defined(MBEDTLS_ECDH_C)
5767 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005768#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02005769#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005770 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02005771#if defined(MBEDTLS_SSL_CLI_C)
5772 handshake->ecjpake_cache = NULL;
5773 handshake->ecjpake_cache_len = 0;
5774#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005775#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005776
5777#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5778 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5779#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005780}
5781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005783{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005786 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5787 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005789 mbedtls_md_init( &transform->md_ctx_enc );
5790 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005791}
5792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005794{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005796}
5797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005799{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005800 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005801 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005803 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005805 if( ssl->handshake )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806 mbedtls_ssl_handshake_free( ssl->handshake );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005807
5808 /*
5809 * Either the pointers are now NULL or cleared properly and can be freed.
5810 * Now allocate missing structures.
5811 */
5812 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005813 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005814 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005815 }
Paul Bakker48916f92012-09-16 19:57:18 +00005816
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005817 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005818 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005819 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005820 }
Paul Bakker48916f92012-09-16 19:57:18 +00005821
Paul Bakker82788fb2014-10-20 13:59:19 +02005822 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005823 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005824 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005825 }
Paul Bakker48916f92012-09-16 19:57:18 +00005826
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005827 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005828 if( ssl->handshake == NULL ||
5829 ssl->transform_negotiate == NULL ||
5830 ssl->session_negotiate == NULL )
5831 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834 mbedtls_free( ssl->handshake );
5835 mbedtls_free( ssl->transform_negotiate );
5836 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005837
5838 ssl->handshake = NULL;
5839 ssl->transform_negotiate = NULL;
5840 ssl->session_negotiate = NULL;
5841
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005842 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005843 }
5844
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005845 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005847 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005848 ssl_handshake_params_init( ssl->handshake );
5849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005850#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005851 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5852 {
5853 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005854
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005855 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5856 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5857 else
5858 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005859
5860 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005861 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005862#endif
5863
Paul Bakker48916f92012-09-16 19:57:18 +00005864 return( 0 );
5865}
5866
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005867#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005868/* Dummy cookie callbacks for defaults */
5869static int ssl_cookie_write_dummy( void *ctx,
5870 unsigned char **p, unsigned char *end,
5871 const unsigned char *cli_id, size_t cli_id_len )
5872{
5873 ((void) ctx);
5874 ((void) p);
5875 ((void) end);
5876 ((void) cli_id);
5877 ((void) cli_id_len);
5878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005880}
5881
5882static int ssl_cookie_check_dummy( void *ctx,
5883 const unsigned char *cookie, size_t cookie_len,
5884 const unsigned char *cli_id, size_t cli_id_len )
5885{
5886 ((void) ctx);
5887 ((void) cookie);
5888 ((void) cookie_len);
5889 ((void) cli_id);
5890 ((void) cli_id_len);
5891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005893}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005894#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005895
Paul Bakker5121ce52009-01-03 21:22:43 +00005896/*
5897 * Initialize an SSL context
5898 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005899void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
5900{
5901 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
5902}
5903
5904/*
5905 * Setup an SSL context
5906 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005907int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02005908 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00005909{
Paul Bakker48916f92012-09-16 19:57:18 +00005910 int ret;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005911 const size_t len = MBEDTLS_SSL_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005912
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005913 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00005914
5915 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005916 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00005917 */
k-stachowiakc2eddee2018-07-09 10:39:20 +02005918 ssl->in_buf = NULL;
5919 ssl->out_buf = NULL;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005920 if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL ||
5921 ( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005922 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", len ) );
k-stachowiak2c161142018-07-31 16:52:32 +02005924 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiakc2eddee2018-07-09 10:39:20 +02005925 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005926 }
5927
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005928#if defined(MBEDTLS_SSL_PROTO_DTLS)
5929 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5930 {
5931 ssl->out_hdr = ssl->out_buf;
5932 ssl->out_ctr = ssl->out_buf + 3;
5933 ssl->out_len = ssl->out_buf + 11;
5934 ssl->out_iv = ssl->out_buf + 13;
5935 ssl->out_msg = ssl->out_buf + 13;
5936
5937 ssl->in_hdr = ssl->in_buf;
5938 ssl->in_ctr = ssl->in_buf + 3;
5939 ssl->in_len = ssl->in_buf + 11;
5940 ssl->in_iv = ssl->in_buf + 13;
5941 ssl->in_msg = ssl->in_buf + 13;
5942 }
5943 else
5944#endif
5945 {
5946 ssl->out_ctr = ssl->out_buf;
5947 ssl->out_hdr = ssl->out_buf + 8;
5948 ssl->out_len = ssl->out_buf + 11;
5949 ssl->out_iv = ssl->out_buf + 13;
5950 ssl->out_msg = ssl->out_buf + 13;
5951
5952 ssl->in_ctr = ssl->in_buf;
5953 ssl->in_hdr = ssl->in_buf + 8;
5954 ssl->in_len = ssl->in_buf + 11;
5955 ssl->in_iv = ssl->in_buf + 13;
5956 ssl->in_msg = ssl->in_buf + 13;
5957 }
5958
Paul Bakker48916f92012-09-16 19:57:18 +00005959 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiakc2eddee2018-07-09 10:39:20 +02005960 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005961
5962 return( 0 );
k-stachowiakc2eddee2018-07-09 10:39:20 +02005963
5964error:
5965 mbedtls_free( ssl->in_buf );
5966 mbedtls_free( ssl->out_buf );
5967
5968 ssl->conf = NULL;
5969
5970 ssl->in_buf = NULL;
5971 ssl->out_buf = NULL;
5972
5973 ssl->in_hdr = NULL;
5974 ssl->in_ctr = NULL;
5975 ssl->in_len = NULL;
5976 ssl->in_iv = NULL;
5977 ssl->in_msg = NULL;
5978
5979 ssl->out_hdr = NULL;
5980 ssl->out_ctr = NULL;
5981 ssl->out_len = NULL;
5982 ssl->out_iv = NULL;
5983 ssl->out_msg = NULL;
5984
k-stachowiak2c161142018-07-31 16:52:32 +02005985 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005986}
5987
5988/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00005989 * Reset an initialized and used SSL context for re-use while retaining
5990 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005991 *
5992 * If partial is non-zero, keep data in the input buffer and client ID.
5993 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00005994 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005995static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00005996{
Paul Bakker48916f92012-09-16 19:57:18 +00005997 int ret;
5998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006000
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006001 /* Cancel any possibly running timer */
6002 ssl_set_timer( ssl, 0 );
6003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006004#if defined(MBEDTLS_SSL_RENEGOTIATION)
6005 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006006 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006007
6008 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6010 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006011#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006013
Paul Bakker7eb013f2011-10-06 12:37:39 +00006014 ssl->in_offt = NULL;
6015
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006016 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006017 ssl->in_msgtype = 0;
6018 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006019 if( partial == 0 )
6020 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006022 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006023 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006024#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006026 ssl_dtls_replay_reset( ssl );
6027#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006028
6029 ssl->in_hslen = 0;
6030 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006031
6032 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006033
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006034 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006035 ssl->out_msgtype = 0;
6036 ssl->out_msglen = 0;
6037 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6039 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006040 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006041#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006042
Paul Bakker48916f92012-09-16 19:57:18 +00006043 ssl->transform_in = NULL;
6044 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006045
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006046 ssl->session_in = NULL;
6047 ssl->session_out = NULL;
6048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 memset( ssl->out_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006050
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006051 if( partial == 0 )
6052 memset( ssl->in_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00006053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6055 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6058 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6061 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006062 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006063 }
6064#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006065
Paul Bakker48916f92012-09-16 19:57:18 +00006066 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068 mbedtls_ssl_transform_free( ssl->transform );
6069 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006070 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006071 }
Paul Bakker48916f92012-09-16 19:57:18 +00006072
Paul Bakkerc0463502013-02-14 11:19:38 +01006073 if( ssl->session )
6074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075 mbedtls_ssl_session_free( ssl->session );
6076 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006077 ssl->session = NULL;
6078 }
6079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006081 ssl->alpn_chosen = NULL;
6082#endif
6083
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006084#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006085 if( partial == 0 )
6086 {
6087 mbedtls_free( ssl->cli_id );
6088 ssl->cli_id = NULL;
6089 ssl->cli_id_len = 0;
6090 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006091#endif
6092
Paul Bakker48916f92012-09-16 19:57:18 +00006093 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6094 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006095
6096 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006097}
6098
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006099/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006100 * Reset an initialized and used SSL context for re-use while retaining
6101 * all application-set variables, function pointers and data.
6102 */
6103int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6104{
6105 return( ssl_session_reset_int( ssl, 0 ) );
6106}
6107
6108/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006109 * SSL set accessors
6110 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006111void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006112{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006113 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006114}
6115
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006116void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006117{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006118 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006119}
6120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006122void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006123{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006124 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006125}
6126#endif
6127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006128#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006129void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006130{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006131 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006132}
6133#endif
6134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006136void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006137{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006138 conf->hs_timeout_min = min;
6139 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006140}
6141#endif
6142
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006143void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006144{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006145 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006146}
6147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006149void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006150 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006151 void *p_vrfy )
6152{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006153 conf->f_vrfy = f_vrfy;
6154 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006155}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006156#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006157
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006158void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006159 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006160 void *p_rng )
6161{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006162 conf->f_rng = f_rng;
6163 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006164}
6165
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006166void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006167 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006168 void *p_dbg )
6169{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006170 conf->f_dbg = f_dbg;
6171 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006172}
6173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006175 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006176 mbedtls_ssl_send_t *f_send,
6177 mbedtls_ssl_recv_t *f_recv,
6178 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006179{
6180 ssl->p_bio = p_bio;
6181 ssl->f_send = f_send;
6182 ssl->f_recv = f_recv;
6183 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006184}
6185
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006186void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006187{
6188 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006189}
6190
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006191void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6192 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006193 mbedtls_ssl_set_timer_t *f_set_timer,
6194 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006195{
6196 ssl->p_timer = p_timer;
6197 ssl->f_set_timer = f_set_timer;
6198 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006199
6200 /* Make sure we start with no timer running */
6201 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006202}
6203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006204#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006205void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006206 void *p_cache,
6207 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6208 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006209{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006210 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006211 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006212 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006213}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006214#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006216#if defined(MBEDTLS_SSL_CLI_C)
6217int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006218{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006219 int ret;
6220
6221 if( ssl == NULL ||
6222 session == NULL ||
6223 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006224 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006227 }
6228
6229 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6230 return( ret );
6231
Paul Bakker0a597072012-09-25 21:55:46 +00006232 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006233
6234 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006235}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006236#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006237
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006238void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006239 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006240{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006241 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6242 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6243 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6244 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006245}
6246
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006247void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006248 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006249 int major, int minor )
6250{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006252 return;
6253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006255 return;
6256
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006257 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006258}
6259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006260#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006261void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006262 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006263{
6264 conf->cert_profile = profile;
6265}
6266
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006267/* Append a new keycert entry to a (possibly empty) list */
6268static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6269 mbedtls_x509_crt *cert,
6270 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006271{
niisatoa35dbf12018-06-25 19:05:48 +09006272 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006273
niisatoa35dbf12018-06-25 19:05:48 +09006274 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6275 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006276 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006277
niisatoa35dbf12018-06-25 19:05:48 +09006278 new_cert->cert = cert;
6279 new_cert->key = key;
6280 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006281
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006282 /* Update head is the list was null, else add to the end */
6283 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006284 {
niisatoa35dbf12018-06-25 19:05:48 +09006285 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006286 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006287 else
6288 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006289 mbedtls_ssl_key_cert *cur = *head;
6290 while( cur->next != NULL )
6291 cur = cur->next;
niisatoa35dbf12018-06-25 19:05:48 +09006292 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006293 }
6294
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006295 return( 0 );
6296}
6297
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006298int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006299 mbedtls_x509_crt *own_cert,
6300 mbedtls_pk_context *pk_key )
6301{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006302 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006303}
6304
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006305void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006306 mbedtls_x509_crt *ca_chain,
6307 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006308{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006309 conf->ca_chain = ca_chain;
6310 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006311}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006313
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006314#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6315int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6316 mbedtls_x509_crt *own_cert,
6317 mbedtls_pk_context *pk_key )
6318{
6319 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6320 own_cert, pk_key ) );
6321}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006322
6323void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6324 mbedtls_x509_crt *ca_chain,
6325 mbedtls_x509_crl *ca_crl )
6326{
6327 ssl->handshake->sni_ca_chain = ca_chain;
6328 ssl->handshake->sni_ca_crl = ca_crl;
6329}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006330
6331void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6332 int authmode )
6333{
6334 ssl->handshake->sni_authmode = authmode;
6335}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006336#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6337
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006338#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006339/*
6340 * Set EC J-PAKE password for current handshake
6341 */
6342int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6343 const unsigned char *pw,
6344 size_t pw_len )
6345{
6346 mbedtls_ecjpake_role role;
6347
Janos Follath8eb64132016-06-03 15:40:57 +01006348 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006349 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6350
6351 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6352 role = MBEDTLS_ECJPAKE_SERVER;
6353 else
6354 role = MBEDTLS_ECJPAKE_CLIENT;
6355
6356 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6357 role,
6358 MBEDTLS_MD_SHA256,
6359 MBEDTLS_ECP_DP_SECP256R1,
6360 pw, pw_len ) );
6361}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006362#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006365int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006366 const unsigned char *psk, size_t psk_len,
6367 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006368{
Paul Bakker6db455e2013-09-18 17:29:31 +02006369 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006370 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6373 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006374
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006375 /* Identity len will be encoded on two bytes */
6376 if( ( psk_identity_len >> 16 ) != 0 ||
6377 psk_identity_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
6378 {
6379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6380 }
6381
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006382 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006383 {
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006384 mbedtls_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006385
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006386 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006387 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006388 conf->psk_len = 0;
6389 }
6390 if( conf->psk_identity != NULL )
6391 {
6392 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006393 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006394 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006395 }
6396
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006397 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6398 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006399 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006400 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006401 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006402 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006403 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006404 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006405 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006406
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006407 conf->psk_len = psk_len;
6408 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006409
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006410 memcpy( conf->psk, psk, conf->psk_len );
6411 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006412
6413 return( 0 );
6414}
6415
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006416int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6417 const unsigned char *psk, size_t psk_len )
6418{
6419 if( psk == NULL || ssl->handshake == NULL )
6420 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6421
6422 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6423 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6424
6425 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006426 {
6427 mbedtls_zeroize( ssl->handshake->psk, ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006428 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006429 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006430 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006431
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006432 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006433 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006434
6435 ssl->handshake->psk_len = psk_len;
6436 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6437
6438 return( 0 );
6439}
6440
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006441void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006443 size_t),
6444 void *p_psk )
6445{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006446 conf->f_psk = f_psk;
6447 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006448}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006450
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006451#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006452
6453#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006454int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006455{
6456 int ret;
6457
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006458 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6459 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6460 {
6461 mbedtls_mpi_free( &conf->dhm_P );
6462 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006463 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006465
6466 return( 0 );
6467}
Hanno Becker470a8c42017-10-04 15:28:46 +01006468#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006469
Hanno Beckera90658f2017-10-04 15:29:08 +01006470int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6471 const unsigned char *dhm_P, size_t P_len,
6472 const unsigned char *dhm_G, size_t G_len )
6473{
6474 int ret;
6475
6476 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6477 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6478 {
6479 mbedtls_mpi_free( &conf->dhm_P );
6480 mbedtls_mpi_free( &conf->dhm_G );
6481 return( ret );
6482 }
6483
6484 return( 0 );
6485}
Paul Bakker5121ce52009-01-03 21:22:43 +00006486
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006487int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006488{
6489 int ret;
6490
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006491 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6492 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6493 {
6494 mbedtls_mpi_free( &conf->dhm_P );
6495 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006496 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006497 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006498
6499 return( 0 );
6500}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006501#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006502
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006503#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6504/*
6505 * Set the minimum length for Diffie-Hellman parameters
6506 */
6507void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6508 unsigned int bitlen )
6509{
6510 conf->dhm_min_bitlen = bitlen;
6511}
6512#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6513
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006514#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006515/*
6516 * Set allowed/preferred hashes for handshake signatures
6517 */
6518void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6519 const int *hashes )
6520{
6521 conf->sig_hashes = hashes;
6522}
Hanno Becker947194e2017-04-07 13:25:49 +01006523#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006524
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006525#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006526/*
6527 * Set the allowed elliptic curves
6528 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006529void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006530 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006531{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006532 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006533}
Hanno Becker947194e2017-04-07 13:25:49 +01006534#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006535
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006536#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006538{
Hanno Becker947194e2017-04-07 13:25:49 +01006539 /* Initialize to suppress unnecessary compiler warning */
6540 size_t hostname_len = 0;
6541
6542 /* Check if new hostname is valid before
6543 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006544 if( hostname != NULL )
6545 {
6546 hostname_len = strlen( hostname );
6547
6548 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6549 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6550 }
6551
6552 /* Now it's clear that we will overwrite the old hostname,
6553 * so we can free it safely */
6554
6555 if( ssl->hostname != NULL )
6556 {
6557 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
6558 mbedtls_free( ssl->hostname );
6559 }
6560
6561 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006562
Paul Bakker5121ce52009-01-03 21:22:43 +00006563 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006564 {
6565 ssl->hostname = NULL;
6566 }
6567 else
6568 {
6569 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006570 if( ssl->hostname == NULL )
6571 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006572
Hanno Becker947194e2017-04-07 13:25:49 +01006573 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006574
Hanno Becker947194e2017-04-07 13:25:49 +01006575 ssl->hostname[hostname_len] = '\0';
6576 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006577
6578 return( 0 );
6579}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006580#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006581
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006582#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006583void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006584 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006585 const unsigned char *, size_t),
6586 void *p_sni )
6587{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006588 conf->f_sni = f_sni;
6589 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006590}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006594int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006595{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006596 size_t cur_len, tot_len;
6597 const char **p;
6598
6599 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006600 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6601 * MUST NOT be truncated."
6602 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006603 */
6604 tot_len = 0;
6605 for( p = protos; *p != NULL; p++ )
6606 {
6607 cur_len = strlen( *p );
6608 tot_len += cur_len;
6609
Ronald Crona32236c2020-04-23 16:41:44 +02006610 if( ( cur_len == 0 ) ||
6611 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
6612 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006614 }
6615
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006616 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006617
6618 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006619}
6620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006622{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006623 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006624}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006626
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006627void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006628{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006629 conf->max_major_ver = major;
6630 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006631}
6632
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006633void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006634{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006635 conf->min_major_ver = major;
6636 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006637}
6638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006640void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006641{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006642 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006643}
6644#endif
6645
Janos Follath088ce432017-04-10 12:42:31 +01006646#if defined(MBEDTLS_SSL_SRV_C)
6647void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6648 char cert_req_ca_list )
6649{
6650 conf->cert_req_ca_list = cert_req_ca_list;
6651}
6652#endif
6653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006655void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006656{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006657 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006658}
6659#endif
6660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006661#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006662void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006663{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006664 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006665}
6666#endif
6667
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006668#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006669void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006670{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006671 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006672}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006673#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006676int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006677{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
6679 mfl_code_to_length[mfl_code] > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006681 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006682 }
6683
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006684 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006685
6686 return( 0 );
6687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006690#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006691void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006692{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006693 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006694}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006698void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006699{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006700 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006701}
6702#endif
6703
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006704void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006705{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006706 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006707}
6708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006709#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006710void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006711{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006712 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006713}
6714
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006715void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006716{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006717 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006718}
6719
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006720void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006721 const unsigned char period[8] )
6722{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006723 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006724}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006728#if defined(MBEDTLS_SSL_CLI_C)
6729void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006730{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006731 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006732}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006733#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006734
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006735#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006736void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6737 mbedtls_ssl_ticket_write_t *f_ticket_write,
6738 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6739 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006740{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006741 conf->f_ticket_write = f_ticket_write;
6742 conf->f_ticket_parse = f_ticket_parse;
6743 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006744}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006745#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006747
Robert Cragie4feb7ae2015-10-02 13:33:37 +01006748#if defined(MBEDTLS_SSL_EXPORT_KEYS)
6749void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
6750 mbedtls_ssl_export_keys_t *f_export_keys,
6751 void *p_export_keys )
6752{
6753 conf->f_export_keys = f_export_keys;
6754 conf->p_export_keys = p_export_keys;
6755}
6756#endif
6757
Paul Bakker5121ce52009-01-03 21:22:43 +00006758/*
6759 * SSL get accessors
6760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006762{
6763 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6764}
6765
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006766uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006767{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006768 if( ssl->session != NULL )
6769 return( ssl->session->verify_result );
6770
6771 if( ssl->session_negotiate != NULL )
6772 return( ssl->session_negotiate->verify_result );
6773
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006774 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006775}
6776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006778{
Paul Bakker926c8e42013-03-06 10:23:34 +01006779 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006780 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006783}
6784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006786{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006788 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006789 {
6790 switch( ssl->minor_ver )
6791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006793 return( "DTLSv1.0" );
6794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006796 return( "DTLSv1.2" );
6797
6798 default:
6799 return( "unknown (DTLS)" );
6800 }
6801 }
6802#endif
6803
Paul Bakker43ca69c2011-01-15 17:35:19 +00006804 switch( ssl->minor_ver )
6805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006806 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006807 return( "SSLv3.0" );
6808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006810 return( "TLSv1.0" );
6811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006813 return( "TLSv1.1" );
6814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006815 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00006816 return( "TLSv1.2" );
6817
Paul Bakker43ca69c2011-01-15 17:35:19 +00006818 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006819 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00006820 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00006821}
6822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006824{
Hanno Becker12f7ede2018-08-17 15:28:19 +01006825 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006827 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006828
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006829 if( transform == NULL )
6830 return( (int) mbedtls_ssl_hdr_len( ssl ) );
6831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006832#if defined(MBEDTLS_ZLIB_SUPPORT)
6833 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
6834 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006835#endif
6836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839 case MBEDTLS_MODE_GCM:
6840 case MBEDTLS_MODE_CCM:
6841 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006842 transform_expansion = transform->minlen;
6843 break;
6844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845 case MBEDTLS_MODE_CBC:
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006846
6847 block_size = mbedtls_cipher_get_block_size(
6848 &transform->cipher_ctx_enc );
6849
Hanno Becker12f7ede2018-08-17 15:28:19 +01006850 /* Expansion due to the addition of the MAC. */
6851 transform_expansion += transform->maclen;
6852
6853 /* Expansion due to the addition of CBC padding;
6854 * Theoretically up to 256 bytes, but we never use
6855 * more than the block size of the underlying cipher. */
6856 transform_expansion += block_size;
6857
6858 /* For TLS 1.1 or higher, an explicit IV is added
6859 * after the record header. */
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006860#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
6861 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker12f7ede2018-08-17 15:28:19 +01006862 transform_expansion += block_size;
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006863#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker12f7ede2018-08-17 15:28:19 +01006864
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006865 break;
6866
6867 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006869 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006870 }
6871
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006872 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006873}
6874
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02006875#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
6876size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
6877{
6878 size_t max_len;
6879
6880 /*
6881 * Assume mfl_code is correct since it was checked when set
6882 */
6883 max_len = mfl_code_to_length[ssl->conf->mfl_code];
6884
6885 /*
6886 * Check if a smaller max length was negotiated
6887 */
6888 if( ssl->session_out != NULL &&
6889 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6890 {
6891 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6892 }
6893
6894 return max_len;
6895}
6896#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
6897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898#if defined(MBEDTLS_X509_CRT_PARSE_C)
6899const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00006900{
6901 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006902 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006903
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006904 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006905}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00006907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908#if defined(MBEDTLS_SSL_CLI_C)
6909int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006910{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006911 if( ssl == NULL ||
6912 dst == NULL ||
6913 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006914 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006917 }
6918
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006919 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006920}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006922
Paul Bakker5121ce52009-01-03 21:22:43 +00006923/*
Paul Bakker1961b702013-01-25 14:49:24 +01006924 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00006925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006926int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006927{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006929
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006930 if( ssl == NULL || ssl->conf == NULL )
6931 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006934 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006936#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006938 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006940#endif
6941
Paul Bakker1961b702013-01-25 14:49:24 +01006942 return( ret );
6943}
6944
6945/*
6946 * Perform the SSL handshake
6947 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01006949{
6950 int ret = 0;
6951
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006952 if( ssl == NULL || ssl->conf == NULL )
6953 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01006956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006957 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01006958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01006960
6961 if( ret != 0 )
6962 break;
6963 }
6964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006966
6967 return( ret );
6968}
6969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970#if defined(MBEDTLS_SSL_RENEGOTIATION)
6971#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006972/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006973 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00006974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006976{
6977 int ret;
6978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006980
6981 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6983 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006988 return( ret );
6989 }
6990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006992
6993 return( 0 );
6994}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006996
6997/*
6998 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 * - any side: calling mbedtls_ssl_renegotiate(),
7000 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7001 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007002 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007003 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007007{
7008 int ret;
7009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007011
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007012 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7013 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007014
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007015 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7016 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007018 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007020 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007021 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007022 ssl->handshake->out_msg_seq = 1;
7023 else
7024 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007025 }
7026#endif
7027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7029 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007033 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007034 return( ret );
7035 }
7036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007038
7039 return( 0 );
7040}
7041
7042/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007043 * Renegotiate current connection on client,
7044 * or request renegotiation on server
7045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007046int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007047{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007048 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007049
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007050 if( ssl == NULL || ssl->conf == NULL )
7051 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007054 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007055 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7058 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007060 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007061
7062 /* Did we already try/start sending HelloRequest? */
7063 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007065
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007066 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007067 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007070#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007071 /*
7072 * On client, either start the renegotiation process or,
7073 * if already in progress, continue the handshake
7074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007079
7080 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007083 return( ret );
7084 }
7085 }
7086 else
7087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007091 return( ret );
7092 }
7093 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007095
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007096 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007097}
7098
7099/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007100 * Check record counters and renegotiate if they're above the limit.
7101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007103{
Andres AG2196c7f2016-12-15 17:01:16 +00007104 size_t ep_len = ssl_ep_len( ssl );
7105 int in_ctr_cmp;
7106 int out_ctr_cmp;
7107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7109 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007110 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007111 {
7112 return( 0 );
7113 }
7114
Andres AG2196c7f2016-12-15 17:01:16 +00007115 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7116 ssl->conf->renego_period + ep_len, 8 - ep_len );
7117 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
7118 ssl->conf->renego_period + ep_len, 8 - ep_len );
7119
7120 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007121 {
7122 return( 0 );
7123 }
7124
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007127}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007129
7130/*
7131 * Receive application data decrypted from the SSL layer
7132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007134{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007135 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007136 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007137
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007138 if( ssl == NULL || ssl->conf == NULL )
7139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007144 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007147 return( ret );
7148
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007149 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007152 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007153 return( ret );
7154 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007155 }
7156#endif
7157
Hanno Becker4a810fb2017-05-24 16:27:30 +01007158 /*
7159 * Check if renegotiation is necessary and/or handshake is
7160 * in process. If yes, perform/continue, and fall through
7161 * if an unexpected packet is received while the client
7162 * is waiting for the ServerHello.
7163 *
7164 * (There is no equivalent to the last condition on
7165 * the server-side as it is not treated as within
7166 * a handshake while waiting for the ClientHello
7167 * after a renegotiation request.)
7168 */
7169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007171 ret = ssl_check_ctr_renegotiate( ssl );
7172 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7173 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007176 return( ret );
7177 }
7178#endif
7179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007183 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7184 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007187 return( ret );
7188 }
7189 }
7190
7191 if( ssl->in_offt == NULL )
7192 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007193 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007194 if( ssl->f_get_timer != NULL &&
7195 ssl->f_get_timer( ssl->p_timer ) == -1 )
7196 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007197 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007198 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007199
Hanno Becker4a810fb2017-05-24 16:27:30 +01007200 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007201 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007202 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7203 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007204
Hanno Becker4a810fb2017-05-24 16:27:30 +01007205 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7206 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007207 }
7208
7209 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007211 {
7212 /*
7213 * OpenSSL sends empty messages to randomize the IV
7214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007218 return( 0 );
7219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007221 return( ret );
7222 }
7223 }
7224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007225 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007228
Hanno Becker4a810fb2017-05-24 16:27:30 +01007229 /*
7230 * - For client-side, expect SERVER_HELLO_REQUEST.
7231 * - For server-side, expect CLIENT_HELLO.
7232 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7233 */
7234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007236 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007237 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007238 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007241
7242 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007244 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007245 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007246#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007248 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007249#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007250
Hanno Becker4a810fb2017-05-24 16:27:30 +01007251#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007252 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007256
7257 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007259 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007260 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007261#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007263 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007264#endif /* MBEDTLS_SSL_SRV_C */
7265
Hanno Becker21df7f92017-10-17 11:03:26 +01007266#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007267 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007268 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7269 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7270 ssl->conf->allow_legacy_renegotiation ==
7271 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7272 {
7273 /*
7274 * Accept renegotiation request
7275 */
Paul Bakker48916f92012-09-16 19:57:18 +00007276
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007277 /* DTLS clients need to know renego is server-initiated */
7278#if defined(MBEDTLS_SSL_PROTO_DTLS)
7279 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7280 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7281 {
7282 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7283 }
7284#endif
7285 ret = ssl_start_renegotiation( ssl );
7286 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7287 ret != 0 )
7288 {
7289 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7290 return( ret );
7291 }
7292 }
7293 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007294#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007295 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007296 /*
7297 * Refuse renegotiation
7298 */
7299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302#if defined(MBEDTLS_SSL_PROTO_SSL3)
7303 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007304 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007305 /* SSLv3 does not have a "no_renegotiation" warning, so
7306 we send a fatal alert and abort the connection. */
7307 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7308 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7309 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007310 }
7311 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7313#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7314 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7315 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7318 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7319 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007320 {
7321 return( ret );
7322 }
Paul Bakker48916f92012-09-16 19:57:18 +00007323 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007324 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7326 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7329 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007330 }
Paul Bakker48916f92012-09-16 19:57:18 +00007331 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007332
Hanno Becker4a810fb2017-05-24 16:27:30 +01007333 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00007334 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007335#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007337 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007338 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007339 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007340 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007343 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007345 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007346 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007347 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7351 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007354 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007355 }
7356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7360 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007361 }
7362
7363 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007364
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007365 /* We're going to return something now, cancel timer,
7366 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007368 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007369
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007370#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007371 /* If we requested renego but received AppData, resend HelloRequest.
7372 * Do it now, after setting in_offt, to avoid taking this branch
7373 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007375 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007377 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007378 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007381 return( ret );
7382 }
7383 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007385#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007386 }
7387
7388 n = ( len < ssl->in_msglen )
7389 ? len : ssl->in_msglen;
7390
7391 memcpy( buf, ssl->in_offt, n );
7392 ssl->in_msglen -= n;
7393
7394 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007395 {
7396 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007397 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007398 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007399 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007400 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007401 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007402 /* more data available */
7403 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007404 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007407
Paul Bakker23986e52011-04-24 08:57:21 +00007408 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007409}
7410
7411/*
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007412 * Send application data to be encrypted by the SSL layer, taking care of max
7413 * fragment length and buffer size.
7414 *
7415 * According to RFC 5246 Section 6.2.1:
7416 *
7417 * Zero-length fragments of Application data MAY be sent as they are
7418 * potentially useful as a traffic analysis countermeasure.
7419 *
7420 * Therefore, it is possible that the input message length is 0 and the
7421 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007422 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007423static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007424 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007425{
Paul Bakker23986e52011-04-24 08:57:21 +00007426 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007428 size_t max_len = mbedtls_ssl_get_max_frag_len( ssl );
Florin0b7b83f2017-07-22 09:01:44 +02007429#else
7430 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
7431#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007432 if( len > max_len )
7433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007435 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007438 "maximum fragment length: %d > %d",
7439 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007441 }
7442 else
7443#endif
7444 len = max_len;
7445 }
Paul Bakker887bd502011-06-08 13:10:54 +00007446
Paul Bakker5121ce52009-01-03 21:22:43 +00007447 if( ssl->out_left != 0 )
7448 {
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007449 /*
7450 * The user has previously tried to send the data and
7451 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7452 * written. In this case, we expect the high-level write function
7453 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007458 return( ret );
7459 }
7460 }
Paul Bakker887bd502011-06-08 13:10:54 +00007461 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007462 {
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007463 /*
7464 * The user is trying to send a message the first time, so we need to
7465 * copy the data into the internal buffers and setup the data structure
7466 * to keep track of partial writes
7467 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007468 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007470 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007475 return( ret );
7476 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007477 }
7478
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007479 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007480}
7481
7482/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007483 * Write application data, doing 1/n-1 splitting if necessary.
7484 *
7485 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007486 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01007487 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007490static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007491 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007492{
7493 int ret;
7494
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007495 if( ssl->conf->cbc_record_splitting ==
7496 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007497 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7499 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7500 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007501 {
7502 return( ssl_write_real( ssl, buf, len ) );
7503 }
7504
7505 if( ssl->split_done == 0 )
7506 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007507 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007508 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007509 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007510 }
7511
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007512 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7513 return( ret );
7514 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007515
7516 return( ret + 1 );
7517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007519
7520/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007521 * Write application data (public-facing wrapper)
7522 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007523int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007524{
7525 int ret;
7526
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007528
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007529 if( ssl == NULL || ssl->conf == NULL )
7530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7531
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007532#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007533 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7534 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007535 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007536 return( ret );
7537 }
7538#endif
7539
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007540 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007541 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007542 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007543 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007544 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007545 return( ret );
7546 }
7547 }
7548
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007549#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007550 ret = ssl_write_split( ssl, buf, len );
7551#else
7552 ret = ssl_write_real( ssl, buf, len );
7553#endif
7554
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007556
7557 return( ret );
7558}
7559
7560/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007561 * Notify the peer that the connection is being closed
7562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007564{
7565 int ret;
7566
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007567 if( ssl == NULL || ssl->conf == NULL )
7568 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007571
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007572 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7578 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7579 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007582 return( ret );
7583 }
7584 }
7585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007587
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007588 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007589}
7590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00007592{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007593 if( transform == NULL )
7594 return;
7595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00007597 deflateEnd( &transform->ctx_deflate );
7598 inflateEnd( &transform->ctx_inflate );
7599#endif
7600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601 mbedtls_cipher_free( &transform->cipher_ctx_enc );
7602 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02007603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604 mbedtls_md_free( &transform->md_ctx_enc );
7605 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02007606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607 mbedtls_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007608}
7609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610#if defined(MBEDTLS_X509_CRT_PARSE_C)
7611static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007612{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007614
7615 while( cur != NULL )
7616 {
7617 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007619 cur = next;
7620 }
7621}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake )
Paul Bakker48916f92012-09-16 19:57:18 +00007625{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007626 if( handshake == NULL )
7627 return;
7628
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007629#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7630 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7631 mbedtls_md5_free( &handshake->fin_md5 );
7632 mbedtls_sha1_free( &handshake->fin_sha1 );
7633#endif
7634#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7635#if defined(MBEDTLS_SHA256_C)
7636 mbedtls_sha256_free( &handshake->fin_sha256 );
7637#endif
7638#if defined(MBEDTLS_SHA512_C)
7639 mbedtls_sha512_free( &handshake->fin_sha512 );
7640#endif
7641#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007643#if defined(MBEDTLS_DHM_C)
7644 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007645#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646#if defined(MBEDTLS_ECDH_C)
7647 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007648#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007649#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007650 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007651#if defined(MBEDTLS_SSL_CLI_C)
7652 mbedtls_free( handshake->ecjpake_cache );
7653 handshake->ecjpake_cache = NULL;
7654 handshake->ecjpake_cache_len = 0;
7655#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007656#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02007657
Janos Follath4ae5c292016-02-10 11:27:43 +00007658#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
7659 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02007660 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007661 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007662#endif
7663
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007664#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7665 if( handshake->psk != NULL )
7666 {
7667 mbedtls_zeroize( handshake->psk, handshake->psk_len );
7668 mbedtls_free( handshake->psk );
7669 }
7670#endif
7671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7673 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007674 /*
7675 * Free only the linked list wrapper, not the keys themselves
7676 * since the belong to the SNI callback
7677 */
7678 if( handshake->sni_key_cert != NULL )
7679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007680 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007681
7682 while( cur != NULL )
7683 {
7684 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007686 cur = next;
7687 }
7688 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691#if defined(MBEDTLS_SSL_PROTO_DTLS)
7692 mbedtls_free( handshake->verify_cookie );
7693 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007694 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007695#endif
7696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007697 mbedtls_zeroize( handshake, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007698}
7699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007701{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007702 if( session == NULL )
7703 return;
7704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007706 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708 mbedtls_x509_crt_free( session->peer_cert );
7709 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007710 }
Paul Bakkered27a042013-04-18 22:46:23 +02007711#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007712
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007713#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007715#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717 mbedtls_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007718}
7719
Paul Bakker5121ce52009-01-03 21:22:43 +00007720/*
7721 * Free an SSL context
7722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007724{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007725 if( ssl == NULL )
7726 return;
7727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007729
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007730 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007732 mbedtls_zeroize( ssl->out_buf, MBEDTLS_SSL_BUFFER_LEN );
7733 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007734 }
7735
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007736 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738 mbedtls_zeroize( ssl->in_buf, MBEDTLS_SSL_BUFFER_LEN );
7739 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007740 }
7741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02007743 if( ssl->compress_buf != NULL )
7744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745 mbedtls_zeroize( ssl->compress_buf, MBEDTLS_SSL_BUFFER_LEN );
7746 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02007747 }
7748#endif
7749
Paul Bakker48916f92012-09-16 19:57:18 +00007750 if( ssl->transform )
7751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 mbedtls_ssl_transform_free( ssl->transform );
7753 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007754 }
7755
7756 if( ssl->handshake )
7757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 mbedtls_ssl_handshake_free( ssl->handshake );
7759 mbedtls_ssl_transform_free( ssl->transform_negotiate );
7760 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762 mbedtls_free( ssl->handshake );
7763 mbedtls_free( ssl->transform_negotiate );
7764 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007765 }
7766
Paul Bakkerc0463502013-02-14 11:19:38 +01007767 if( ssl->session )
7768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769 mbedtls_ssl_session_free( ssl->session );
7770 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007771 }
7772
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02007773#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02007774 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007775 {
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007776 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00007778 }
Paul Bakker0be444a2013-08-27 21:55:01 +02007779#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7782 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
7785 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00007786 }
7787#endif
7788
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007789#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007791#endif
7792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00007794
Paul Bakker86f04f42013-02-14 11:20:09 +01007795 /* Actually clear after last debug message */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796 mbedtls_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007797}
7798
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007799/*
7800 * Initialze mbedtls_ssl_config
7801 */
7802void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
7803{
7804 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
7805}
7806
Simon Butcherc97b6972015-12-27 23:48:17 +00007807#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007808static int ssl_preset_default_hashes[] = {
7809#if defined(MBEDTLS_SHA512_C)
7810 MBEDTLS_MD_SHA512,
7811 MBEDTLS_MD_SHA384,
7812#endif
7813#if defined(MBEDTLS_SHA256_C)
7814 MBEDTLS_MD_SHA256,
7815 MBEDTLS_MD_SHA224,
7816#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02007817#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007818 MBEDTLS_MD_SHA1,
7819#endif
7820 MBEDTLS_MD_NONE
7821};
Simon Butcherc97b6972015-12-27 23:48:17 +00007822#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007823
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007824static int ssl_preset_suiteb_ciphersuites[] = {
7825 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
7826 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
7827 0
7828};
7829
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007830#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007831static int ssl_preset_suiteb_hashes[] = {
7832 MBEDTLS_MD_SHA256,
7833 MBEDTLS_MD_SHA384,
7834 MBEDTLS_MD_NONE
7835};
7836#endif
7837
7838#if defined(MBEDTLS_ECP_C)
7839static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007840#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007841 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007842#endif
7843#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007844 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007845#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007846 MBEDTLS_ECP_DP_NONE
7847};
7848#endif
7849
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007850/*
Tillmann Karras588ad502015-09-25 04:27:22 +02007851 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007852 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007853int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007854 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007855{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007856#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007857 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007858#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007859
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02007860 /* Use the functions here so that they are covered in tests,
7861 * but otherwise access member directly for efficiency */
7862 mbedtls_ssl_conf_endpoint( conf, endpoint );
7863 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007864
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007865 /*
7866 * Things that are common to all presets
7867 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007868#if defined(MBEDTLS_SSL_CLI_C)
7869 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7870 {
7871 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
7872#if defined(MBEDTLS_SSL_SESSION_TICKETS)
7873 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
7874#endif
7875 }
7876#endif
7877
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007878#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007879 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007880#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007881
7882#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7883 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
7884#endif
7885
7886#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7887 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
7888#endif
7889
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007890#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7891 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7892#endif
7893
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007894#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007895 conf->f_cookie_write = ssl_cookie_write_dummy;
7896 conf->f_cookie_check = ssl_cookie_check_dummy;
7897#endif
7898
7899#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7900 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7901#endif
7902
Janos Follath088ce432017-04-10 12:42:31 +01007903#if defined(MBEDTLS_SSL_SRV_C)
7904 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
7905#endif
7906
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007907#if defined(MBEDTLS_SSL_PROTO_DTLS)
7908 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7909 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7910#endif
7911
7912#if defined(MBEDTLS_SSL_RENEGOTIATION)
7913 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00007914 memset( conf->renego_period, 0x00, 2 );
7915 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007916#endif
7917
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007918#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
7919 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7920 {
Hanno Becker00d0a682017-10-04 13:14:29 +01007921 const unsigned char dhm_p[] =
7922 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
7923 const unsigned char dhm_g[] =
7924 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
7925
Hanno Beckera90658f2017-10-04 15:29:08 +01007926 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
7927 dhm_p, sizeof( dhm_p ),
7928 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007929 {
7930 return( ret );
7931 }
7932 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007933#endif
7934
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007935 /*
7936 * Preset-specific defaults
7937 */
7938 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007939 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007940 /*
7941 * NSA Suite B
7942 */
7943 case MBEDTLS_SSL_PRESET_SUITEB:
7944 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7945 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7946 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7947 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7948
7949 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7950 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7951 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7952 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7953 ssl_preset_suiteb_ciphersuites;
7954
7955#if defined(MBEDTLS_X509_CRT_PARSE_C)
7956 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007957#endif
7958
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007959#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007960 conf->sig_hashes = ssl_preset_suiteb_hashes;
7961#endif
7962
7963#if defined(MBEDTLS_ECP_C)
7964 conf->curve_list = ssl_preset_suiteb_curves;
7965#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007966 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007967
7968 /*
7969 * Default
7970 */
7971 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03007972 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
7973 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
7974 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7975 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7976 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
7977 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
7978 MBEDTLS_SSL_MIN_MINOR_VERSION :
7979 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007980 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7981 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7982
7983#if defined(MBEDTLS_SSL_PROTO_DTLS)
7984 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7985 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
7986#endif
7987
7988 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7989 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7990 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7991 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7992 mbedtls_ssl_list_ciphersuites();
7993
7994#if defined(MBEDTLS_X509_CRT_PARSE_C)
7995 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7996#endif
7997
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007998#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007999 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008000#endif
8001
8002#if defined(MBEDTLS_ECP_C)
8003 conf->curve_list = mbedtls_ecp_grp_id_list();
8004#endif
8005
8006#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8007 conf->dhm_min_bitlen = 1024;
8008#endif
8009 }
8010
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008011 return( 0 );
8012}
8013
8014/*
8015 * Free mbedtls_ssl_config
8016 */
8017void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8018{
8019#if defined(MBEDTLS_DHM_C)
8020 mbedtls_mpi_free( &conf->dhm_P );
8021 mbedtls_mpi_free( &conf->dhm_G );
8022#endif
8023
8024#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8025 if( conf->psk != NULL )
8026 {
8027 mbedtls_zeroize( conf->psk, conf->psk_len );
8028 mbedtls_zeroize( conf->psk_identity, conf->psk_identity_len );
8029 mbedtls_free( conf->psk );
8030 mbedtls_free( conf->psk_identity );
8031 conf->psk_len = 0;
8032 conf->psk_identity_len = 0;
8033 }
8034#endif
8035
8036#if defined(MBEDTLS_X509_CRT_PARSE_C)
8037 ssl_key_cert_free( conf->key_cert );
8038#endif
8039
8040 mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
8041}
8042
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008043#if defined(MBEDTLS_PK_C) && \
8044 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008045/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008046 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008047 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008049{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008050#if defined(MBEDTLS_RSA_C)
8051 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8052 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008054#if defined(MBEDTLS_ECDSA_C)
8055 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8056 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008057#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008058 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008059}
8060
Hanno Becker7e5437a2017-04-28 17:15:26 +01008061unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8062{
8063 switch( type ) {
8064 case MBEDTLS_PK_RSA:
8065 return( MBEDTLS_SSL_SIG_RSA );
8066 case MBEDTLS_PK_ECDSA:
8067 case MBEDTLS_PK_ECKEY:
8068 return( MBEDTLS_SSL_SIG_ECDSA );
8069 default:
8070 return( MBEDTLS_SSL_SIG_ANON );
8071 }
8072}
8073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008075{
8076 switch( sig )
8077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008078#if defined(MBEDTLS_RSA_C)
8079 case MBEDTLS_SSL_SIG_RSA:
8080 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008081#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008082#if defined(MBEDTLS_ECDSA_C)
8083 case MBEDTLS_SSL_SIG_ECDSA:
8084 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008085#endif
8086 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008087 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008088 }
8089}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008090#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008091
Hanno Becker7e5437a2017-04-28 17:15:26 +01008092#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8093 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8094
8095/* Find an entry in a signature-hash set matching a given hash algorithm. */
8096mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8097 mbedtls_pk_type_t sig_alg )
8098{
8099 switch( sig_alg )
8100 {
8101 case MBEDTLS_PK_RSA:
8102 return( set->rsa );
8103 case MBEDTLS_PK_ECDSA:
8104 return( set->ecdsa );
8105 default:
8106 return( MBEDTLS_MD_NONE );
8107 }
8108}
8109
8110/* Add a signature-hash-pair to a signature-hash set */
8111void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8112 mbedtls_pk_type_t sig_alg,
8113 mbedtls_md_type_t md_alg )
8114{
8115 switch( sig_alg )
8116 {
8117 case MBEDTLS_PK_RSA:
8118 if( set->rsa == MBEDTLS_MD_NONE )
8119 set->rsa = md_alg;
8120 break;
8121
8122 case MBEDTLS_PK_ECDSA:
8123 if( set->ecdsa == MBEDTLS_MD_NONE )
8124 set->ecdsa = md_alg;
8125 break;
8126
8127 default:
8128 break;
8129 }
8130}
8131
8132/* Allow exactly one hash algorithm for each signature. */
8133void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8134 mbedtls_md_type_t md_alg )
8135{
8136 set->rsa = md_alg;
8137 set->ecdsa = md_alg;
8138}
8139
8140#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8141 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8142
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008143/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008144 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008147{
8148 switch( hash )
8149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008150#if defined(MBEDTLS_MD5_C)
8151 case MBEDTLS_SSL_HASH_MD5:
8152 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008153#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154#if defined(MBEDTLS_SHA1_C)
8155 case MBEDTLS_SSL_HASH_SHA1:
8156 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008157#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008158#if defined(MBEDTLS_SHA256_C)
8159 case MBEDTLS_SSL_HASH_SHA224:
8160 return( MBEDTLS_MD_SHA224 );
8161 case MBEDTLS_SSL_HASH_SHA256:
8162 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008163#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164#if defined(MBEDTLS_SHA512_C)
8165 case MBEDTLS_SSL_HASH_SHA384:
8166 return( MBEDTLS_MD_SHA384 );
8167 case MBEDTLS_SSL_HASH_SHA512:
8168 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008169#endif
8170 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008172 }
8173}
8174
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008175/*
8176 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8177 */
8178unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8179{
8180 switch( md )
8181 {
8182#if defined(MBEDTLS_MD5_C)
8183 case MBEDTLS_MD_MD5:
8184 return( MBEDTLS_SSL_HASH_MD5 );
8185#endif
8186#if defined(MBEDTLS_SHA1_C)
8187 case MBEDTLS_MD_SHA1:
8188 return( MBEDTLS_SSL_HASH_SHA1 );
8189#endif
8190#if defined(MBEDTLS_SHA256_C)
8191 case MBEDTLS_MD_SHA224:
8192 return( MBEDTLS_SSL_HASH_SHA224 );
8193 case MBEDTLS_MD_SHA256:
8194 return( MBEDTLS_SSL_HASH_SHA256 );
8195#endif
8196#if defined(MBEDTLS_SHA512_C)
8197 case MBEDTLS_MD_SHA384:
8198 return( MBEDTLS_SSL_HASH_SHA384 );
8199 case MBEDTLS_MD_SHA512:
8200 return( MBEDTLS_SSL_HASH_SHA512 );
8201#endif
8202 default:
8203 return( MBEDTLS_SSL_HASH_NONE );
8204 }
8205}
8206
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008207#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008208/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008209 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008210 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008211 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008212int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008213{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008215
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008216 if( ssl->conf->curve_list == NULL )
8217 return( -1 );
8218
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008219 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008220 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008221 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008222
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008223 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008224}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008225#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008226
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008227#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008228/*
8229 * Check if a hash proposed by the peer is in our list.
8230 * Return 0 if we're willing to use it, -1 otherwise.
8231 */
8232int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8233 mbedtls_md_type_t md )
8234{
8235 const int *cur;
8236
8237 if( ssl->conf->sig_hashes == NULL )
8238 return( -1 );
8239
8240 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8241 if( *cur == (int) md )
8242 return( 0 );
8243
8244 return( -1 );
8245}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008246#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008248#if defined(MBEDTLS_X509_CRT_PARSE_C)
8249int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8250 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008251 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008252 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008253{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008254 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008256 int usage = 0;
8257#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008259 const char *ext_oid;
8260 size_t ext_len;
8261#endif
8262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008263#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8264 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008265 ((void) cert);
8266 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008267 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008268#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008270#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8271 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008272 {
8273 /* Server part of the key exchange */
8274 switch( ciphersuite->key_exchange )
8275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276 case MBEDTLS_KEY_EXCHANGE_RSA:
8277 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008278 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008279 break;
8280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008281 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8282 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8283 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8284 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008285 break;
8286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8288 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008289 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008290 break;
8291
8292 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293 case MBEDTLS_KEY_EXCHANGE_NONE:
8294 case MBEDTLS_KEY_EXCHANGE_PSK:
8295 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8296 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008297 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008298 usage = 0;
8299 }
8300 }
8301 else
8302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8304 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008305 }
8306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008308 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008309 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008310 ret = -1;
8311 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008312#else
8313 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008314#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8317 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008319 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8320 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008321 }
8322 else
8323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8325 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008326 }
8327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008329 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008330 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008331 ret = -1;
8332 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008334
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008335 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008336}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008338
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008339/*
8340 * Convert version numbers to/from wire format
8341 * and, for DTLS, to/from TLS equivalent.
8342 *
8343 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008344 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008345 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8346 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008349 unsigned char ver[2] )
8350{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351#if defined(MBEDTLS_SSL_PROTO_DTLS)
8352 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008355 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8356
8357 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8358 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8359 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008360 else
8361#else
8362 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008363#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008364 {
8365 ver[0] = (unsigned char) major;
8366 ver[1] = (unsigned char) minor;
8367 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008368}
8369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008371 const unsigned char ver[2] )
8372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373#if defined(MBEDTLS_SSL_PROTO_DTLS)
8374 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008375 {
8376 *major = 255 - ver[0] + 2;
8377 *minor = 255 - ver[1] + 1;
8378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008380 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8381 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008382 else
8383#else
8384 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008385#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008386 {
8387 *major = ver[0];
8388 *minor = ver[1];
8389 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008390}
8391
Simon Butcher99000142016-10-13 17:21:01 +01008392int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8393{
8394#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8395 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8396 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8397
8398 switch( md )
8399 {
8400#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8401#if defined(MBEDTLS_MD5_C)
8402 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008403 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008404#endif
8405#if defined(MBEDTLS_SHA1_C)
8406 case MBEDTLS_SSL_HASH_SHA1:
8407 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8408 break;
8409#endif
8410#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8411#if defined(MBEDTLS_SHA512_C)
8412 case MBEDTLS_SSL_HASH_SHA384:
8413 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8414 break;
8415#endif
8416#if defined(MBEDTLS_SHA256_C)
8417 case MBEDTLS_SSL_HASH_SHA256:
8418 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8419 break;
8420#endif
8421 default:
8422 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8423 }
8424
8425 return 0;
8426#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8427 (void) ssl;
8428 (void) md;
8429
8430 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8431#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8432}
8433
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008434#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8435 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8436int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8437 unsigned char *output,
8438 unsigned char *data, size_t data_len )
8439{
8440 int ret = 0;
8441 mbedtls_md5_context mbedtls_md5;
8442 mbedtls_sha1_context mbedtls_sha1;
8443
8444 mbedtls_md5_init( &mbedtls_md5 );
8445 mbedtls_sha1_init( &mbedtls_sha1 );
8446
8447 /*
8448 * digitally-signed struct {
8449 * opaque md5_hash[16];
8450 * opaque sha_hash[20];
8451 * };
8452 *
8453 * md5_hash
8454 * MD5(ClientHello.random + ServerHello.random
8455 * + ServerParams);
8456 * sha_hash
8457 * SHA(ClientHello.random + ServerHello.random
8458 * + ServerParams);
8459 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008460 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008461 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008463 goto exit;
8464 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008465 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008466 ssl->handshake->randbytes, 64 ) ) != 0 )
8467 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008469 goto exit;
8470 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008471 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008472 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_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_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008477 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008479 goto exit;
8480 }
8481
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008482 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008483 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008485 goto exit;
8486 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008487 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008488 ssl->handshake->randbytes, 64 ) ) != 0 )
8489 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008490 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008491 goto exit;
8492 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008493 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008494 data_len ) ) != 0 )
8495 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008496 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008497 goto exit;
8498 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008499 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008500 output + 16 ) ) != 0 )
8501 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008502 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008503 goto exit;
8504 }
8505
8506exit:
8507 mbedtls_md5_free( &mbedtls_md5 );
8508 mbedtls_sha1_free( &mbedtls_sha1 );
8509
8510 if( ret != 0 )
8511 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8512 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8513
8514 return( ret );
8515
8516}
8517#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
8518 MBEDTLS_SSL_PROTO_TLS1_1 */
8519
8520#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8521 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8522int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8523 unsigned char *output,
8524 unsigned char *data, size_t data_len,
8525 mbedtls_md_type_t md_alg )
8526{
8527 int ret = 0;
8528 mbedtls_md_context_t ctx;
8529 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8530
8531 mbedtls_md_init( &ctx );
8532
8533 /*
8534 * digitally-signed struct {
8535 * opaque client_random[32];
8536 * opaque server_random[32];
8537 * ServerDHParams params;
8538 * };
8539 */
8540 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8541 {
8542 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8543 goto exit;
8544 }
8545 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8546 {
8547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8548 goto exit;
8549 }
8550 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8551 {
8552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8553 goto exit;
8554 }
8555 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8556 {
8557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8558 goto exit;
8559 }
8560 if( ( ret = mbedtls_md_finish( &ctx, output ) ) != 0 )
8561 {
8562 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8563 goto exit;
8564 }
8565
8566exit:
8567 mbedtls_md_free( &ctx );
8568
8569 if( ret != 0 )
8570 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8571 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8572
8573 return( ret );
8574}
8575#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
8576 MBEDTLS_SSL_PROTO_TLS1_2 */
8577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578#endif /* MBEDTLS_SSL_TLS_C */