blob: bc056eea545d6656400e6ce95c972138ab938298 [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é-Gonnard3ba2bca2020-07-28 10:19:45 +02001662#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
1663 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1664 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1665 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1666/*
1667 * Compute HMAC of variable-length data with constant flow.
1668 */
1669int mbedtls_ssl_cf_hmac(
1670 mbedtls_md_context_t *ctx,
1671 const unsigned char *add_data, size_t add_data_len,
1672 const unsigned char *data, size_t data_len_secret,
1673 size_t min_data_len, size_t max_data_len,
1674 unsigned char *output )
1675{
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001676 /* WORK IN PROGRESS - THIS IS ONLY PSEUDO-CONTANT-TIME */
1677
1678 /*
1679 * Process MAC and always update for padlen afterwards to make
1680 * total time independent of padlen.
1681 *
1682 * Known timing attacks:
1683 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1684 *
1685 * To compensate for different timings for the MAC calculation
1686 * depending on how much padding was removed (which is determined
1687 * by padlen), process extra_run more blocks through the hash
1688 * function.
1689 *
1690 * The formula in the paper is
1691 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
1692 * where L1 is the size of the header plus the decrypted message
1693 * plus CBC padding and L2 is the size of the header plus the
1694 * decrypted message. This is for an underlying hash function
1695 * with 64-byte blocks.
1696 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
1697 * correctly. We round down instead of up, so -56 is the correct
1698 * value for our calculations instead of -55.
1699 *
1700 * Repeat the formula rather than defining a block_size variable.
1701 * This avoids requiring division by a variable at runtime
1702 * (which would be marginally less efficient and would require
1703 * linking an extra division function in some builds).
1704 */
1705 size_t j, extra_run = 0;
1706 /* This size is enough to server either as input to
1707 * md_process() or as output to md_finish() */
1708 unsigned char tmp[128];
1709
1710 memset( tmp, 0, sizeof( tmp ) );
1711
1712 switch( mbedtls_md_get_type( ctx->md_info ) )
1713 {
1714#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
1715defined(MBEDTLS_SHA256_C)
1716 case MBEDTLS_MD_MD5:
1717 case MBEDTLS_MD_SHA1:
1718 case MBEDTLS_MD_SHA256:
1719 /* 8 bytes of message size, 64-byte compression blocks */
1720 extra_run = ( add_data_len + max_data_len + 8 ) / 64 -
1721 ( add_data_len + data_len_secret + 8 ) / 64;
1722 break;
1723#endif
1724#if defined(MBEDTLS_SHA512_C)
1725 case MBEDTLS_MD_SHA384:
1726 /* 16 bytes of message size, 128-byte compression blocks */
1727 extra_run = ( add_data_len + max_data_len + 16 ) / 128 -
1728 ( add_data_len + data_len_secret + 16 ) / 128;
1729 break;
1730#endif
1731 default:
1732 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1733 }
1734
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001735 mbedtls_md_hmac_update( ctx, add_data, add_data_len );
1736 mbedtls_md_hmac_update( ctx, data, data_len_secret );
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001737 /* Make sure we access everything even when padlen > 0. This
1738 * makes the synchronisation requirements for just-in-time
1739 * Prime+Probe attacks much tighter and hopefully impractical. */
1740 ssl_read_memory( data + min_data_len, max_data_len - min_data_len );
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001741 mbedtls_md_hmac_finish( ctx, output );
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02001742
1743 /* Dummy calls to compression function.
1744 * Call mbedtls_md_process at least once due to cache attacks
1745 * that observe whether md_process() was called of not.
1746 * Respect the usual start-(process|update)-finish sequence for
1747 * the sake of hardware accelerators that might require it. */
1748 mbedtls_md_starts( ctx );
1749 for( j = 0; j < extra_run + 1; j++ )
1750 mbedtls_md_process( ctx, tmp );
1751 mbedtls_md_finish( ctx, tmp );
1752
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +02001753 mbedtls_md_hmac_reset( ctx );
1754
1755 return( 0 );
1756}
1757#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC && TLS 1.0-1.2 */
1758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001760{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001761 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001763 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001764#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001765 size_t padlen = 0, correct = 1;
1766#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001769
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001770 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001774 }
1775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001777
Paul Bakker48916f92012-09-16 19:57:18 +00001778 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001781 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001783 }
1784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1786 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001787 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001788 int ret;
1789 size_t olen = 0;
1790
Paul Bakker68884e32013-01-07 18:20:04 +01001791 padlen = 0;
1792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001794 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001795 ssl->transform_in->ivlen,
1796 ssl->in_msg, ssl->in_msglen,
1797 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001800 return( ret );
1801 }
1802
1803 if( ssl->in_msglen != olen )
1804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1806 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001807 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001808 }
Paul Bakker68884e32013-01-07 18:20:04 +01001809 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1811#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1812 if( mode == MBEDTLS_MODE_GCM ||
1813 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001814 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001815 int ret;
1816 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001817 unsigned char *dec_msg;
1818 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001819 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001820 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001822 size_t explicit_iv_len = ssl->transform_in->ivlen -
1823 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001824
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001825 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001828 "+ taglen (%d)", ssl->in_msglen,
1829 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001831 }
1832 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1833
Paul Bakker68884e32013-01-07 18:20:04 +01001834 dec_msg = ssl->in_msg;
1835 dec_msg_result = ssl->in_msg;
1836 ssl->in_msglen = dec_msglen;
1837
1838 memcpy( add_data, ssl->in_ctr, 8 );
1839 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001841 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001842 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1843 add_data[12] = ssl->in_msglen & 0xFF;
1844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakker68884e32013-01-07 18:20:04 +01001846 add_data, 13 );
1847
1848 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1849 ssl->in_iv,
1850 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
Paul Bakker68884e32013-01-07 18:20:04 +01001853 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001855
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001856 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001857 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001860 ssl->transform_in->iv_dec,
1861 ssl->transform_in->ivlen,
1862 add_data, 13,
1863 dec_msg, dec_msglen,
1864 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001865 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1870 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001871
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001872 return( ret );
1873 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001874 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001875
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001876 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1879 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001880 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001881 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001882 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02001884#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 {
Paul Bakker45829992013-01-03 14:52:21 +01001887 /*
1888 * Decrypt and check the padding
1889 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001890 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001891 unsigned char *dec_msg;
1892 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001893 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001894 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001895 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001896
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 /*
Paul Bakker45829992013-01-03 14:52:21 +01001898 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1901 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001902 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001903#endif
Paul Bakker45829992013-01-03 14:52:21 +01001904
1905 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1906 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001909 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1910 ssl->transform_in->ivlen,
1911 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001913 }
1914
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001915 dec_msglen = ssl->in_msglen;
1916 dec_msg = ssl->in_msg;
1917 dec_msg_result = ssl->in_msg;
1918
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001919 /*
1920 * Authenticate before decrypt if enabled
1921 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1923 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001924 {
Hanno Becker992b6872017-11-09 18:57:39 +00001925 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001926 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001929
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001930 dec_msglen -= ssl->transform_in->maclen;
1931 ssl->in_msglen -= ssl->transform_in->maclen;
1932
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001933 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1934 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1935 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1936 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1941 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001942 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001943 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001947 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001948 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001949 ssl->transform_in->maclen );
1950
Hanno Becker992b6872017-11-09 18:57:39 +00001951 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1952 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001957 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001958 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001959 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001961
1962 /*
1963 * Check length sanity
1964 */
1965 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001968 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001970 }
1971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001973 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001974 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001977 {
Paul Bakker48916f92012-09-16 19:57:18 +00001978 dec_msglen -= ssl->transform_in->ivlen;
1979 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001980
Paul Bakker48916f92012-09-16 19:57:18 +00001981 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001982 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001983 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001987 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001988 ssl->transform_in->ivlen,
1989 dec_msg, dec_msglen,
1990 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001993 return( ret );
1994 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001995
Paul Bakkercca5b812013-08-31 17:40:26 +02001996 if( dec_msglen != olen )
1997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1999 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002000 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2003 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002004 {
2005 /*
2006 * Save IV in SSL3 and TLS1
2007 */
2008 memcpy( ssl->transform_in->iv_dec,
2009 ssl->transform_in->cipher_ctx_dec.iv,
2010 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002012#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002013
2014 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002015
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002016 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002017 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019#if defined(MBEDTLS_SSL_DEBUG_ALL)
2020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002021 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002022#endif
Paul Bakker45829992013-01-03 14:52:21 +01002023 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002024 correct = 0;
2025 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027#if defined(MBEDTLS_SSL_PROTO_SSL3)
2028 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 {
Paul Bakker48916f92012-09-16 19:57:18 +00002030 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#if defined(MBEDTLS_SSL_DEBUG_ALL)
2033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002035 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002036#endif
Paul Bakker45829992013-01-03 14:52:21 +01002037 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 }
2039 }
2040 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2042#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2043 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2044 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 {
2046 /*
Paul Bakker45829992013-01-03 14:52:21 +01002047 * TLSv1+: always check the padding up to the first failure
2048 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002050 size_t pad_count = 0, real_count = 1;
Angus Gratton1ba8e912018-06-19 15:57:50 +10002051 size_t padding_idx = ssl->in_msglen - padlen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002052
Paul Bakker956c9e02013-12-19 14:42:28 +01002053 /*
2054 * Padding is guaranteed to be incorrect if:
Angus Gratton1ba8e912018-06-19 15:57:50 +10002055 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002056 *
Angus Gratton1ba8e912018-06-19 15:57:50 +10002057 * 2. padding_idx > MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002058 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002059 *
2060 * In both cases we reset padding_idx to a safe value (0) to
2061 * prevent out-of-buffer reads.
2062 */
Angus Gratton1ba8e912018-06-19 15:57:50 +10002063 correct &= ( padlen <= ssl->in_msglen );
2064 correct &= ( padding_idx <= MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002065 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002066
2067 padding_idx *= correct;
2068
Angus Gratton1ba8e912018-06-19 15:57:50 +10002069 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002070 {
Angus Gratton1ba8e912018-06-19 15:57:50 +10002071 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002072 pad_count += real_count *
2073 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2074 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002075
2076 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002079 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002081#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002082 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002084 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2086 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2089 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002090 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002091
2092 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002094 else
Manuel Pégourié-Gonnard8ebb88d2020-07-28 09:55:33 +02002095#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2098 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002099 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002100
Manuel Pégourié-Gonnard7c344322018-07-10 11:15:36 +02002101#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard7c344322018-07-10 11:15:36 +02002104#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002105
2106 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002107 * Authenticate if not done yet.
2108 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002110#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002111 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002112 {
Hanno Becker992b6872017-11-09 18:57:39 +00002113 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002114
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002115 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002116
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002117 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2118 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120#if defined(MBEDTLS_SSL_PROTO_SSL3)
2121 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002122 {
2123 ssl_mac( &ssl->transform_in->md_ctx_dec,
2124 ssl->transform_in->mac_dec,
2125 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002126 ssl->in_ctr, ssl->in_msgtype,
2127 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002128 }
2129 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2131#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2132 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2133 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002134 {
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002135 int ret;
2136 unsigned char add_data[13];
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002137
2138 /*
2139 * The next two sizes are the minimum and maximum values of
2140 * in_msglen over all padlen values.
2141 *
2142 * They're independent of padlen, since we previously did
2143 * in_msglen -= padlen.
2144 *
2145 * Note that max_len + maclen is never more than the buffer
2146 * length, as we previously did in_msglen -= maclen too.
2147 */
2148 const size_t max_len = ssl->in_msglen + padlen;
2149 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2150
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002151 memcpy( add_data + 0, ssl->in_ctr, 8 );
2152 memcpy( add_data + 8, ssl->in_hdr, 3 );
2153 memcpy( add_data + 11, ssl->in_len, 2 );
2154
2155 ret = mbedtls_ssl_cf_hmac( &ssl->transform_in->md_ctx_dec,
2156 add_data, sizeof( add_data ),
2157 ssl->in_msg, ssl->in_msglen,
2158 min_len, max_len,
2159 mac_expect );
2160 if( ret != 0 )
Gilles Peskinebb07ca02018-06-06 17:23:31 +02002161 {
Manuel Pégourié-Gonnardd1197182020-07-28 10:43:03 +02002162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
2163 return( ret );
Gilles Peskinebb07ca02018-06-06 17:23:31 +02002164 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002165
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002166 /* Make sure we access all the memory that could contain the MAC,
2167 * before we check it in the next code block. This makes the
2168 * synchronisation requirements for just-in-time Prime+Probe
2169 * attacks much tighter and hopefully impractical. */
2170 ssl_read_memory( ssl->in_msg + min_len,
2171 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002172 }
2173 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2175 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2178 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002179 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002180
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002181#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002182 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2183 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2184 ssl->transform_in->maclen );
Manuel Pégourié-Gonnardaeeaaf22018-06-28 10:38:35 +02002185#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
Hanno Becker992b6872017-11-09 18:57:39 +00002187 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2188 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190#if defined(MBEDTLS_SSL_DEBUG_ALL)
2191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002192#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002193 correct = 0;
2194 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002195 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002196 }
Hanno Beckerca31b472018-10-17 14:43:14 +01002197
2198 /*
2199 * Finally check the correct flag
2200 */
2201 if( correct == 0 )
2202 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002203#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002204
2205 /* Make extra sure authentication was performed, exactly once */
2206 if( auth_done != 1 )
2207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2209 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002210 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002211
2212 if( ssl->in_msglen == 0 )
2213 {
Angus Grattonb91cb6e2018-06-19 15:58:22 +10002214#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2215 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2216 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2217 {
2218 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2220 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2221 }
2222#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2223
Paul Bakker5121ce52009-01-03 21:22:43 +00002224 ssl->nb_zero++;
2225
2226 /*
2227 * Three or more empty messages may be a DoS attack
2228 * (excessive CPU consumption).
2229 */
2230 if( ssl->nb_zero > 3 )
2231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002233 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 }
2236 }
2237 else
2238 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002241 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002242 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002243 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002244 }
2245 else
2246#endif
2247 {
2248 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2249 if( ++ssl->in_ctr[i - 1] != 0 )
2250 break;
2251
2252 /* The loop goes to its end iff the counter is wrapping */
2253 if( i == ssl_ep_len( ssl ) )
2254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2256 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002257 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002258 }
2259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
2262 return( 0 );
2263}
2264
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002265#undef MAC_NONE
2266#undef MAC_PLAINTEXT
2267#undef MAC_CIPHERTEXT
2268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002270/*
2271 * Compression/decompression functions
2272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002274{
2275 int ret;
2276 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002277 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002278 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002279 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002282
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002283 if( len_pre == 0 )
2284 return( 0 );
2285
Paul Bakker2770fbd2012-07-03 13:30:23 +00002286 memcpy( msg_pre, ssl->out_msg, len_pre );
2287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002289 ssl->out_msglen ) );
2290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002292 ssl->out_msg, ssl->out_msglen );
2293
Paul Bakker48916f92012-09-16 19:57:18 +00002294 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2295 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2296 ssl->transform_out->ctx_deflate.next_out = msg_post;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002297 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002298
Paul Bakker48916f92012-09-16 19:57:18 +00002299 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002300 if( ret != Z_OK )
2301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2303 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002304 }
2305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 ssl->out_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002307 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002310 ssl->out_msglen ) );
2311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002313 ssl->out_msg, ssl->out_msglen );
2314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002316
2317 return( 0 );
2318}
2319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002321{
2322 int ret;
2323 unsigned char *msg_post = ssl->in_msg;
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002324 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002325 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002326 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002329
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002330 if( len_pre == 0 )
2331 return( 0 );
2332
Paul Bakker2770fbd2012-07-03 13:30:23 +00002333 memcpy( msg_pre, ssl->in_msg, len_pre );
2334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002336 ssl->in_msglen ) );
2337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002339 ssl->in_msg, ssl->in_msglen );
2340
Paul Bakker48916f92012-09-16 19:57:18 +00002341 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2342 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2343 ssl->transform_in->ctx_inflate.next_out = msg_post;
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002344 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002345 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002346
Paul Bakker48916f92012-09-16 19:57:18 +00002347 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002348 if( ret != Z_OK )
2349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2351 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002352 }
2353
Andrzej Kurekc3a3e2d2018-04-23 08:39:13 -04002354 ssl->in_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek149f3a42018-04-24 06:32:44 -04002355 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002358 ssl->in_msglen ) );
2359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002361 ssl->in_msg, ssl->in_msglen );
2362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002364
2365 return( 0 );
2366}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2370static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372#if defined(MBEDTLS_SSL_PROTO_DTLS)
2373static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002374{
2375 /* If renegotiation is not enforced, retransmit until we would reach max
2376 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002377 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002378 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002379 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002380 unsigned char doublings = 1;
2381
2382 while( ratio != 0 )
2383 {
2384 ++doublings;
2385 ratio >>= 1;
2386 }
2387
2388 if( ++ssl->renego_records_seen > doublings )
2389 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002391 return( 0 );
2392 }
2393 }
2394
2395 return( ssl_write_hello_request( ssl ) );
2396}
2397#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002399
Paul Bakker5121ce52009-01-03 21:22:43 +00002400/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002401 * Fill the input message buffer by appending data to it.
2402 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002403 *
2404 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2405 * available (from this read and/or a previous one). Otherwise, an error code
2406 * is returned (possibly EOF or WANT_READ).
2407 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002408 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2409 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2410 * since we always read a whole datagram at once.
2411 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002412 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002413 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002416{
Paul Bakker23986e52011-04-24 08:57:21 +00002417 int ret;
2418 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002422 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002425 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002427 }
2428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 if( nb_want > MBEDTLS_SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002433 }
2434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002436 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002438 uint32_t timeout;
2439
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002440 /* Just to be sure */
2441 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2442 {
2443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2444 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2445 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2446 }
2447
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002448 /*
2449 * The point is, we need to always read a full datagram at once, so we
2450 * sometimes read more then requested, and handle the additional data.
2451 * It could be the rest of the current record (while fetching the
2452 * header) and/or some other records in the same datagram.
2453 */
2454
2455 /*
2456 * Move to the next record in the already read datagram if applicable
2457 */
2458 if( ssl->next_record_offset != 0 )
2459 {
2460 if( ssl->in_left < ssl->next_record_offset )
2461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2463 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002464 }
2465
2466 ssl->in_left -= ssl->next_record_offset;
2467
2468 if( ssl->in_left != 0 )
2469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002471 ssl->next_record_offset ) );
2472 memmove( ssl->in_hdr,
2473 ssl->in_hdr + ssl->next_record_offset,
2474 ssl->in_left );
2475 }
2476
2477 ssl->next_record_offset = 0;
2478 }
2479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002482
2483 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002484 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002485 */
2486 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002489 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002490 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002491
2492 /*
Antonin Décimo8fd91562019-01-23 15:24:37 +01002493 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002494 * are not at the beginning of a new record, the caller did something
2495 * wrong.
2496 */
2497 if( ssl->in_left != 0 )
2498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2500 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002501 }
2502
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002503 /*
2504 * Don't even try to read if time's out already.
2505 * This avoids by-passing the timer when repeatedly receiving messages
2506 * that will end up being dropped.
2507 */
2508 if( ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002509 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002510 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 len = MBEDTLS_SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002515 timeout = ssl->handshake->retransmit_timeout;
2516 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002517 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002520
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002521 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002522 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2523 timeout );
2524 else
2525 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002528
2529 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002531 }
2532
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002533 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002536 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002539 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002540 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002543 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002544 }
2545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002549 return( ret );
2550 }
2551
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002552 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002553 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002555 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002557 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002558 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002561 return( ret );
2562 }
2563
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002564 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002565 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002567 }
2568
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 if( ret < 0 )
2570 return( ret );
2571
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002572 ssl->in_left = ret;
2573 }
2574 else
2575#endif
2576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002578 ssl->in_left, nb_want ) );
2579
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002580 while( ssl->in_left < nb_want )
2581 {
2582 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002583
2584 if( ssl_check_timer( ssl ) != 0 )
2585 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2586 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002587 {
2588 if( ssl->f_recv_timeout != NULL )
2589 {
2590 ret = ssl->f_recv_timeout( ssl->p_bio,
2591 ssl->in_hdr + ssl->in_left, len,
2592 ssl->conf->read_timeout );
2593 }
2594 else
2595 {
2596 ret = ssl->f_recv( ssl->p_bio,
2597 ssl->in_hdr + ssl->in_left, len );
2598 }
2599 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002602 ssl->in_left, nb_want ) );
2603 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002604
2605 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002607
2608 if( ret < 0 )
2609 return( ret );
2610
mohammad160344a6a682018-03-28 23:45:33 -07002611 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad1603b11af862018-03-19 07:18:13 -07002612 {
Jaeden Amerob5f53b12018-04-03 12:09:45 +01002613 MBEDTLS_SSL_DEBUG_MSG( 1,
2614 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160329ed80f2018-04-02 07:34:26 -07002615 ret, (unsigned long)len ) );
mohammad1603b11af862018-03-19 07:18:13 -07002616 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2617 }
2618
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002619 ssl->in_left += ret;
2620 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 }
2622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
2625 return( 0 );
2626}
2627
2628/*
2629 * Flush any data not yet written
2630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002632{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002633 int ret;
2634 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002638 if( ssl->f_send == NULL )
2639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002641 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002643 }
2644
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002645 /* Avoid incrementing counter if data is flushed */
2646 if( ssl->out_left == 0 )
2647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002649 return( 0 );
2650 }
2651
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 while( ssl->out_left > 0 )
2653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2655 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) +
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002658 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002659 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002662
2663 if( ret <= 0 )
2664 return( ret );
2665
mohammad160344a6a682018-03-28 23:45:33 -07002666 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16036085c722018-02-22 04:29:04 -08002667 {
Jaeden Amerob5f53b12018-04-03 12:09:45 +01002668 MBEDTLS_SSL_DEBUG_MSG( 1,
2669 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160329ed80f2018-04-02 07:34:26 -07002670 ret, (unsigned long)ssl->out_left ) );
mohammad16036085c722018-02-22 04:29:04 -08002671 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2672 }
2673
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 ssl->out_left -= ret;
2675 }
2676
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002677 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002678 if( ++ssl->out_ctr[i - 1] != 0 )
2679 break;
2680
2681 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002682 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2685 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002686 }
2687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
2690 return( 0 );
2691}
2692
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002693/*
2694 * Functions to handle the DTLS retransmission state machine
2695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002697/*
2698 * Append current handshake message to current outgoing flight
2699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002701{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702 mbedtls_ssl_flight_item *msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002703
2704 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002705 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002706 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002709 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002710 }
2711
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002712 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002713 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002716 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002717 }
2718
2719 /* Copy current handshake message with headers */
2720 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2721 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002722 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002723 msg->next = NULL;
2724
2725 /* Append to the current flight */
2726 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002727 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002728 else
2729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002731 while( cur->next != NULL )
2732 cur = cur->next;
2733 cur->next = msg;
2734 }
2735
2736 return( 0 );
2737}
2738
2739/*
2740 * Free the current flight of handshake messages
2741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002743{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744 mbedtls_ssl_flight_item *cur = flight;
2745 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002746
2747 while( cur != NULL )
2748 {
2749 next = cur->next;
2750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 mbedtls_free( cur->p );
2752 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002753
2754 cur = next;
2755 }
2756}
2757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2759static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002760#endif
2761
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002762/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002763 * Swap transform_out and out_ctr with the alternative ones
2764 */
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002765static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002768 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002769#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2770 int ret;
2771#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002772
2773 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002776 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002777 }
2778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002780
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002781 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002782 tmp_transform = ssl->transform_out;
2783 ssl->transform_out = ssl->handshake->alt_transform_out;
2784 ssl->handshake->alt_transform_out = tmp_transform;
2785
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002786 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002787 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2788 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2789 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002790
2791 /* Adjust to the newly activated transform */
2792 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002794 {
2795 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2796 ssl->transform_out->fixed_ivlen;
2797 }
2798 else
2799 ssl->out_msg = ssl->out_iv;
2800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2802 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2807 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002808 }
2809 }
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002810#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
2811
2812 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002813}
2814
2815/*
2816 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002817 *
2818 * Need to remember the current message in case flush_output returns
2819 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002820 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002823{
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002824 int ret;
2825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002831
2832 ssl->handshake->cur_msg = ssl->handshake->flight;
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002833 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
2834 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002837 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002838
2839 while( ssl->handshake->cur_msg != NULL )
2840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841 mbedtls_ssl_flight_item *cur = ssl->handshake->cur_msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002842
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002843 /* Swap epochs before sending Finished: we can't do it after
2844 * sending ChangeCipherSpec, in case write returns WANT_READ.
2845 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2847 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002848 {
Andres Amaya Garcia87580532018-12-05 21:57:57 +00002849 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
2850 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002851 }
2852
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002853 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002854 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002855 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002856
2857 ssl->handshake->cur_msg = cur->next;
2858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859 MBEDTLS_SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002864 return( ret );
2865 }
2866 }
2867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2869 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002870 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002873 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2874 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002877
2878 return( 0 );
2879}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002880
2881/*
2882 * To be called when the last message of an incoming flight is received.
2883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002885{
2886 /* We won't need to resend that one any more */
2887 ssl_flight_free( ssl->handshake->flight );
2888 ssl->handshake->flight = NULL;
2889 ssl->handshake->cur_msg = NULL;
2890
2891 /* The next incoming flight will start with this msg_seq */
2892 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2893
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002894 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002895 ssl_set_timer( ssl, 0 );
2896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2898 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002901 }
2902 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002904}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002905
2906/*
2907 * To be called when the last message of an outgoing flight is send.
2908 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002910{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002911 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002912 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002913
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é-Gonnard7de3c9e2014-09-29 15:29:48 +02002916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002918 }
2919 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002921}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002923
Paul Bakker5121ce52009-01-03 21:22:43 +00002924/*
2925 * Record layer functions
2926 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002927
2928/*
2929 * Write current record.
2930 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2931 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002933{
Nicholas Wilsonf0021642016-04-13 11:51:05 +01002934 int ret, done = 0, out_msg_type;
Paul Bakker23986e52011-04-24 08:57:21 +00002935 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002940 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002941 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002943 {
2944 ; /* Skip special handshake treatment when resending */
2945 }
2946 else
2947#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002949 {
Nicholas Wilsonf0021642016-04-13 11:51:05 +01002950 out_msg_type = ssl->out_msg[0];
2951
2952 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002953 ssl->handshake == NULL )
2954 {
2955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2956 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2957 }
2958
Paul Bakker5121ce52009-01-03 21:22:43 +00002959 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2960 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2961 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2962
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002963 /*
2964 * DTLS has additional fields in the Handshake layer,
2965 * between the length field and the actual payload:
2966 * uint16 message_seq;
2967 * uint24 fragment_offset;
2968 * uint24 fragment_length;
2969 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002971 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002972 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002973 /* Make room for the additional DTLS fields */
Hanno Becker9648f8b2017-09-18 10:55:54 +01002974 if( MBEDTLS_SSL_MAX_CONTENT_LEN - ssl->out_msglen < 8 )
2975 {
2976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
2977 "size %u, maximum %u",
2978 (unsigned) ( ssl->in_hslen - 4 ),
2979 (unsigned) ( MBEDTLS_SSL_MAX_CONTENT_LEN - 12 ) ) );
2980 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2981 }
2982
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002983 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002984 ssl->out_msglen += 8;
2985 len += 8;
2986
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002987 /* Write message_seq and update it, except for HelloRequest */
Nicholas Wilsonf0021642016-04-13 11:51:05 +01002988 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002989 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002990 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2991 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2992 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002993 }
2994 else
2995 {
2996 ssl->out_msg[4] = 0;
2997 ssl->out_msg[5] = 0;
2998 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002999
3000 /* We don't fragment, so frag_offset = 0 and frag_len = len */
3001 memset( ssl->out_msg + 6, 0x00, 3 );
3002 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003003 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003005
Nicholas Wilsonf0021642016-04-13 11:51:05 +01003006 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003007 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003008 }
3009
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003010 /* Save handshake and CCS messages for resending */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003012 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003013 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING &&
3015 ( ssl->out_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ||
3016 ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003017 {
3018 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003020 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003021 return( ret );
3022 }
3023 }
3024#endif
3025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003027 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003029 {
3030 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003033 return( ret );
3034 }
3035
3036 len = ssl->out_msglen;
3037 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3041 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045 ret = mbedtls_ssl_hw_record_write( ssl );
3046 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3049 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003050 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003051
3052 if( ret == 0 )
3053 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003054 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003056 if( !done )
3057 {
3058 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003060 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003061
3062 ssl->out_len[0] = (unsigned char)( len >> 8 );
3063 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003064
Paul Bakker48916f92012-09-16 19:57:18 +00003065 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003066 {
3067 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003070 return( ret );
3071 }
3072
3073 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003074 ssl->out_len[0] = (unsigned char)( len >> 8 );
3075 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003076 }
3077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00003079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00003081 "version = [%d:%d], msglen = %d",
3082 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003083 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3086 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003087 }
3088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 return( ret );
3093 }
3094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003096
3097 return( 0 );
3098}
3099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003101/*
3102 * Mark bits in bitmask (used for DTLS HS reassembly)
3103 */
3104static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3105{
3106 unsigned int start_bits, end_bits;
3107
3108 start_bits = 8 - ( offset % 8 );
3109 if( start_bits != 8 )
3110 {
3111 size_t first_byte_idx = offset / 8;
3112
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003113 /* Special case */
3114 if( len <= start_bits )
3115 {
3116 for( ; len != 0; len-- )
3117 mask[first_byte_idx] |= 1 << ( start_bits - len );
3118
3119 /* Avoid potential issues with offset or len becoming invalid */
3120 return;
3121 }
3122
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003123 offset += start_bits; /* Now offset % 8 == 0 */
3124 len -= start_bits;
3125
3126 for( ; start_bits != 0; start_bits-- )
3127 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3128 }
3129
3130 end_bits = len % 8;
3131 if( end_bits != 0 )
3132 {
3133 size_t last_byte_idx = ( offset + len ) / 8;
3134
3135 len -= end_bits; /* Now len % 8 == 0 */
3136
3137 for( ; end_bits != 0; end_bits-- )
3138 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3139 }
3140
3141 memset( mask + offset / 8, 0xFF, len / 8 );
3142}
3143
3144/*
3145 * Check that bitmask is full
3146 */
3147static int ssl_bitmask_check( unsigned char *mask, size_t len )
3148{
3149 size_t i;
3150
3151 for( i = 0; i < len / 8; i++ )
3152 if( mask[i] != 0xFF )
3153 return( -1 );
3154
3155 for( i = 0; i < len % 8; i++ )
3156 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3157 return( -1 );
3158
3159 return( 0 );
3160}
3161
3162/*
3163 * Reassemble fragmented DTLS handshake messages.
3164 *
3165 * Use a temporary buffer for reassembly, divided in two parts:
3166 * - the first holds the reassembled message (including handshake header),
3167 * - the second holds a bitmask indicating which parts of the message
3168 * (excluding headers) have been received so far.
3169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003171{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003172 unsigned char *msg, *bitmask;
3173 size_t frag_len, frag_off;
3174 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3175
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003176 if( ssl->handshake == NULL )
3177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3179 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003180 }
3181
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003182 /*
3183 * For first fragment, check size and allocate buffer
3184 */
3185 if( ssl->handshake->hs_msg == NULL )
3186 {
3187 size_t alloc_len;
3188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003190 msg_len ) );
3191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 if( ssl->in_hslen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3195 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003196 }
3197
3198 /* The bitmask needs one bit per byte of message excluding header */
3199 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3200
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003201 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003202 if( ssl->handshake->hs_msg == NULL )
3203 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003205 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003206 }
3207
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003208 /* Prepare final header: copy msg_type, length and message_seq,
3209 * then add standardised fragment_offset and fragment_length */
3210 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3211 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3212 memcpy( ssl->handshake->hs_msg + 9,
3213 ssl->handshake->hs_msg + 1, 3 );
3214 }
3215 else
3216 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003217 /* Make sure msg_type and length are consistent */
3218 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3221 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003222 }
3223 }
3224
3225 msg = ssl->handshake->hs_msg + 12;
3226 bitmask = msg + msg_len;
3227
3228 /*
3229 * Check and copy current fragment
3230 */
3231 frag_off = ( ssl->in_msg[6] << 16 ) |
3232 ( ssl->in_msg[7] << 8 ) |
3233 ssl->in_msg[8];
3234 frag_len = ( ssl->in_msg[9] << 16 ) |
3235 ( ssl->in_msg[10] << 8 ) |
3236 ssl->in_msg[11];
3237
3238 if( frag_off + frag_len > msg_len )
3239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003241 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003243 }
3244
3245 if( frag_len + 12 > ssl->in_msglen )
3246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003248 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003250 }
3251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003253 frag_off, frag_len ) );
3254
3255 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3256 ssl_bitmask_set( bitmask, frag_off, frag_len );
3257
3258 /*
3259 * Do we have the complete message by now?
3260 * If yes, finalize it, else ask to read the next record.
3261 */
3262 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003265 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003266 }
3267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003269
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003270 if( frag_len + 12 < ssl->in_msglen )
3271 {
3272 /*
3273 * We'got more handshake messages in the same record.
3274 * This case is not handled now because no know implementation does
3275 * that and it's hard to test, so we prefer to fail cleanly for now.
3276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3278 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003279 }
3280
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003281 if( ssl->in_left > ssl->next_record_offset )
3282 {
3283 /*
3284 * We've got more data in the buffer after the current record,
3285 * that we don't want to overwrite. Move it before writing the
3286 * reassembled message, and adjust in_left and next_record_offset.
3287 */
3288 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3289 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3290 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3291
3292 /* First compute and check new lengths */
3293 ssl->next_record_offset = new_remain - ssl->in_hdr;
3294 ssl->in_left = ssl->next_record_offset + remain_len;
3295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296 if( ssl->in_left > MBEDTLS_SSL_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003297 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3300 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003301 }
3302
3303 memmove( new_remain, cur_remain, remain_len );
3304 }
3305
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003306 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3307
Hanno Beckerd82e0c02018-10-15 13:22:22 +01003308 mbedtls_zeroize( ssl->handshake->hs_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003310 ssl->handshake->hs_msg = NULL;
3311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003313 ssl->in_msg, ssl->in_hslen );
3314
3315 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003316}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003318
Simon Butcher99000142016-10-13 17:21:01 +01003319int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003320{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003324 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003326 }
3327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003329 ( ssl->in_msg[1] << 16 ) |
3330 ( ssl->in_msg[2] << 8 ) |
3331 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003334 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003335 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003338 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003339 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003340 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003341 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003342
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003343 /* ssl->handshake is NULL when receiving ClientHello for renego */
3344 if( ssl->handshake != NULL &&
3345 recv_msg_seq != ssl->handshake->in_msg_seq )
3346 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003347 /* Retransmit only on last message from previous flight, to avoid
3348 * too many retransmissions.
3349 * Besides, No sane server ever retransmits HelloVerifyRequest */
3350 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003354 "message_seq = %d, start_of_flight = %d",
3355 recv_msg_seq,
3356 ssl->handshake->in_flight_start_seq ) );
3357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003361 return( ret );
3362 }
3363 }
3364 else
3365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003367 "message_seq = %d, expected = %d",
3368 recv_msg_seq,
3369 ssl->handshake->in_msg_seq ) );
3370 }
3371
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003372 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003373 }
3374 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003375
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003376 /* Reassemble if current message is fragmented or reassembly is
3377 * already in progress */
3378 if( ssl->in_msglen < ssl->in_hslen ||
3379 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3380 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3381 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003384
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003385 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003388 return( ret );
3389 }
3390 }
3391 }
3392 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003394 /* With TLS we don't handle fragmentation (for now) */
3395 if( ssl->in_msglen < ssl->in_hslen )
3396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3398 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003399 }
3400
Simon Butcher99000142016-10-13 17:21:01 +01003401 return( 0 );
3402}
3403
3404void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3405{
3406
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003407 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3408 ssl->handshake != NULL )
3409 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003410 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003411 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003412
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003413 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003415 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003416 ssl->handshake != NULL )
3417 {
3418 ssl->handshake->in_msg_seq++;
3419 }
3420#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003421}
3422
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003423/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003424 * DTLS anti-replay: RFC 6347 4.1.2.6
3425 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003426 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3427 * Bit n is set iff record number in_window_top - n has been seen.
3428 *
3429 * Usually, in_window_top is the last record number seen and the lsb of
3430 * in_window is set. The only exception is the initial state (record number 0
3431 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3434static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003435{
3436 ssl->in_window_top = 0;
3437 ssl->in_window = 0;
3438}
3439
3440static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3441{
3442 return( ( (uint64_t) buf[0] << 40 ) |
3443 ( (uint64_t) buf[1] << 32 ) |
3444 ( (uint64_t) buf[2] << 24 ) |
3445 ( (uint64_t) buf[3] << 16 ) |
3446 ( (uint64_t) buf[4] << 8 ) |
3447 ( (uint64_t) buf[5] ) );
3448}
3449
3450/*
3451 * Return 0 if sequence number is acceptable, -1 otherwise
3452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003454{
3455 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3456 uint64_t bit;
3457
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003458 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003459 return( 0 );
3460
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003461 if( rec_seqnum > ssl->in_window_top )
3462 return( 0 );
3463
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003464 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003465
3466 if( bit >= 64 )
3467 return( -1 );
3468
3469 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3470 return( -1 );
3471
3472 return( 0 );
3473}
3474
3475/*
3476 * Update replay window on new validated record
3477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003479{
3480 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3481
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003482 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003483 return;
3484
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003485 if( rec_seqnum > ssl->in_window_top )
3486 {
3487 /* Update window_top and the contents of the window */
3488 uint64_t shift = rec_seqnum - ssl->in_window_top;
3489
3490 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003491 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003492 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003493 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003494 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003495 ssl->in_window |= 1;
3496 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003497
3498 ssl->in_window_top = rec_seqnum;
3499 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003500 else
3501 {
3502 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003503 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003504
3505 if( bit < 64 ) /* Always true, but be extra sure */
3506 ssl->in_window |= (uint64_t) 1 << bit;
3507 }
3508}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003510
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003511#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003512/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003513static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3514
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003515/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003516 * Without any SSL context, check if a datagram looks like a ClientHello with
3517 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003518 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003519 *
3520 * - if cookie is valid, return 0
3521 * - if ClientHello looks superficially valid but cookie is not,
3522 * fill obuf and set olen, then
3523 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3524 * - otherwise return a specific error code
3525 */
3526static int ssl_check_dtls_clihlo_cookie(
3527 mbedtls_ssl_cookie_write_t *f_cookie_write,
3528 mbedtls_ssl_cookie_check_t *f_cookie_check,
3529 void *p_cookie,
3530 const unsigned char *cli_id, size_t cli_id_len,
3531 const unsigned char *in, size_t in_len,
3532 unsigned char *obuf, size_t buf_len, size_t *olen )
3533{
3534 size_t sid_len, cookie_len;
3535 unsigned char *p;
3536
3537 if( f_cookie_write == NULL || f_cookie_check == NULL )
3538 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3539
3540 /*
3541 * Structure of ClientHello with record and handshake headers,
3542 * and expected values. We don't need to check a lot, more checks will be
3543 * done when actually parsing the ClientHello - skipping those checks
3544 * avoids code duplication and does not make cookie forging any easier.
3545 *
3546 * 0-0 ContentType type; copied, must be handshake
3547 * 1-2 ProtocolVersion version; copied
3548 * 3-4 uint16 epoch; copied, must be 0
3549 * 5-10 uint48 sequence_number; copied
3550 * 11-12 uint16 length; (ignored)
3551 *
3552 * 13-13 HandshakeType msg_type; (ignored)
3553 * 14-16 uint24 length; (ignored)
3554 * 17-18 uint16 message_seq; copied
3555 * 19-21 uint24 fragment_offset; copied, must be 0
3556 * 22-24 uint24 fragment_length; (ignored)
3557 *
3558 * 25-26 ProtocolVersion client_version; (ignored)
3559 * 27-58 Random random; (ignored)
3560 * 59-xx SessionID session_id; 1 byte len + sid_len content
3561 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3562 * ...
3563 *
3564 * Minimum length is 61 bytes.
3565 */
3566 if( in_len < 61 ||
3567 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3568 in[3] != 0 || in[4] != 0 ||
3569 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3570 {
3571 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3572 }
3573
3574 sid_len = in[59];
3575 if( sid_len > in_len - 61 )
3576 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3577
3578 cookie_len = in[60 + sid_len];
3579 if( cookie_len > in_len - 60 )
3580 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3581
3582 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3583 cli_id, cli_id_len ) == 0 )
3584 {
3585 /* Valid cookie */
3586 return( 0 );
3587 }
3588
3589 /*
3590 * If we get here, we've got an invalid cookie, let's prepare HVR.
3591 *
3592 * 0-0 ContentType type; copied
3593 * 1-2 ProtocolVersion version; copied
3594 * 3-4 uint16 epoch; copied
3595 * 5-10 uint48 sequence_number; copied
3596 * 11-12 uint16 length; olen - 13
3597 *
3598 * 13-13 HandshakeType msg_type; hello_verify_request
3599 * 14-16 uint24 length; olen - 25
3600 * 17-18 uint16 message_seq; copied
3601 * 19-21 uint24 fragment_offset; copied
3602 * 22-24 uint24 fragment_length; olen - 25
3603 *
3604 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3605 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3606 *
3607 * Minimum length is 28.
3608 */
3609 if( buf_len < 28 )
3610 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3611
3612 /* Copy most fields and adapt others */
3613 memcpy( obuf, in, 25 );
3614 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3615 obuf[25] = 0xfe;
3616 obuf[26] = 0xff;
3617
3618 /* Generate and write actual cookie */
3619 p = obuf + 28;
3620 if( f_cookie_write( p_cookie,
3621 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3622 {
3623 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3624 }
3625
3626 *olen = p - obuf;
3627
3628 /* Go back and fill length fields */
3629 obuf[27] = (unsigned char)( *olen - 28 );
3630
3631 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3632 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3633 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3634
3635 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3636 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3637
3638 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3639}
3640
3641/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003642 * Handle possible client reconnect with the same UDP quadruplet
3643 * (RFC 6347 Section 4.2.8).
3644 *
3645 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3646 * that looks like a ClientHello.
3647 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003648 * - if the input looks like a ClientHello without cookies,
3649 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003650 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003651 * - if the input looks like a ClientHello with a valid cookie,
3652 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003653 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003654 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003655 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003656 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003657 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3658 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003659 */
3660static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3661{
3662 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003663 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003664
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003665 /* Use out_msg as temporary buffer for writing out HelloVerifyRequest,
3666 * because the output buffer's already around. Don't use out_buf though,
3667 * as we don't want to overwrite out_ctr. */
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003668 ret = ssl_check_dtls_clihlo_cookie(
3669 ssl->conf->f_cookie_write,
3670 ssl->conf->f_cookie_check,
3671 ssl->conf->p_cookie,
3672 ssl->cli_id, ssl->cli_id_len,
3673 ssl->in_buf, ssl->in_left,
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003674 ssl->out_msg, MBEDTLS_SSL_MAX_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003675
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003676 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3677
3678 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003679 {
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003680 int send_ret;
3681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3682 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003683 ssl->out_msg, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003684 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003685 * If the error is permanent we'll catch it later,
3686 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard6062b492020-03-31 12:49:27 +02003687 send_ret = ssl->f_send( ssl->p_bio, ssl->out_msg, len );
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003688 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3689 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003690
3691 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003692 }
3693
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003694 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003695 {
Manuel Pégourié-Gonnard4bbbdc32020-03-31 12:31:24 +02003696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003697 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3698 {
3699 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3700 return( ret );
3701 }
3702
3703 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003704 }
3705
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003706 return( ret );
3707}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003708#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003709
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003710/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003711 * ContentType type;
3712 * ProtocolVersion version;
3713 * uint16 epoch; // DTLS only
3714 * uint48 sequence_number; // DTLS only
3715 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003716 *
3717 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003718 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003719 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3720 *
3721 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003722 * 1. proceed with the record if this function returns 0
3723 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3724 * 3. return CLIENT_RECONNECT if this function return that value
3725 * 4. drop the whole datagram if this function returns anything else.
3726 * Point 2 is needed when the peer is resending, and we have already received
3727 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003730{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003731 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 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 +02003734
Paul Bakker5121ce52009-01-03 21:22:43 +00003735 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003736 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003737 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003740 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003741 ssl->in_msgtype,
3742 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003743
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003744 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3746 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3747 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3748 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003751
3752#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01003753 /* Silently ignore invalid DTLS records as recommended by RFC 6347
3754 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003755 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3756#endif /* MBEDTLS_SSL_PROTO_DTLS */
3757 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3758 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
3759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003761 }
3762
3763 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003764 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3767 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003768 }
3769
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003770 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3773 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003774 }
3775
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003776 /* Check length against the size of our buffer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777 if( ssl->in_msglen > MBEDTLS_SSL_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003778 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3781 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003782 }
3783
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003784 /*
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003785 * DTLS-related tests.
3786 * Check epoch before checking length constraint because
3787 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3788 * message gets duplicated before the corresponding Finished message,
3789 * the second ChangeCipherSpec should be discarded because it belongs
3790 * to an old epoch, but not because its length is shorter than
3791 * the minimum record length for packets using the new record transform.
3792 * Note that these two kinds of failures are handled differently,
3793 * as an unexpected record is silently skipped but an invalid
3794 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003795 */
3796#if defined(MBEDTLS_SSL_PROTO_DTLS)
3797 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3798 {
3799 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3800
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003801 /* Check epoch (and sequence number) with DTLS */
3802 if( rec_epoch != ssl->in_epoch )
3803 {
3804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3805 "expected %d, received %d",
3806 ssl->in_epoch, rec_epoch ) );
3807
3808#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3809 /*
3810 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3811 * access the first byte of record content (handshake type), as we
3812 * have an active transform (possibly iv_len != 0), so use the
3813 * fact that the record header len is 13 instead.
3814 */
3815 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3816 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3817 rec_epoch == 0 &&
3818 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3819 ssl->in_left > 13 &&
3820 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3821 {
3822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3823 "from the same port" ) );
3824 return( ssl_handle_possible_reconnect( ssl ) );
3825 }
3826 else
3827#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3828 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3829 }
3830
3831#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3832 /* Replay detection only works for the current epoch */
3833 if( rec_epoch == ssl->in_epoch &&
3834 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3835 {
3836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3837 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3838 }
3839#endif
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003840
3841 /* Drop unexpected ChangeCipherSpec messages */
3842 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3843 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3844 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3845 {
3846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3847 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3848 }
3849
3850 /* Drop unexpected ApplicationData records,
3851 * except at the beginning of renegotiations */
3852 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
3853 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
3854#if defined(MBEDTLS_SSL_RENEGOTIATION)
3855 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
3856 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
3857#endif
3858 )
3859 {
3860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3861 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3862 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003863 }
3864#endif /* MBEDTLS_SSL_PROTO_DTLS */
3865
Hanno Beckera34cc6b2017-05-26 16:07:36 +01003866
3867 /* Check length against bounds of the current transform and version */
3868 if( ssl->transform_in == NULL )
3869 {
3870 if( ssl->in_msglen < 1 ||
3871 ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
3872 {
3873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3874 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3875 }
3876 }
3877 else
3878 {
3879 if( ssl->in_msglen < ssl->transform_in->minlen )
3880 {
3881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3882 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3883 }
3884
3885#if defined(MBEDTLS_SSL_PROTO_SSL3)
3886 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3887 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_MAX_CONTENT_LEN )
3888 {
3889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3890 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3891 }
3892#endif
3893#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3894 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3895 /*
3896 * TLS encrypted messages can have up to 256 bytes of padding
3897 */
3898 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
3899 ssl->in_msglen > ssl->transform_in->minlen +
3900 MBEDTLS_SSL_MAX_CONTENT_LEN + 256 )
3901 {
3902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3903 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3904 }
3905#endif
3906 }
3907
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003908 return( 0 );
3909}
Paul Bakker5121ce52009-01-03 21:22:43 +00003910
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003911/*
3912 * If applicable, decrypt (and decompress) record content
3913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003915{
3916 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
3919 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3922 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926 ret = mbedtls_ssl_hw_record_read( ssl );
3927 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3930 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003931 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003932
3933 if( ret == 0 )
3934 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003935 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003936#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003937 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003938 {
3939 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003941 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003942 return( ret );
3943 }
3944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003945 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00003946 ssl->in_msg, ssl->in_msglen );
3947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003948 if( ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3951 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003952 }
3953 }
3954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003956 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003957 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003958 {
3959 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003962 return( ret );
3963 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00003964 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003965#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003968 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003970 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003971 }
3972#endif
3973
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003974 return( 0 );
3975}
3976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003978
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003979/*
3980 * Read a record.
3981 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003982 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3983 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3984 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003985 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003987{
3988 int ret;
3989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003991
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003992 if( ssl->keep_current_message == 0 )
3993 {
3994 do {
Simon Butcher99000142016-10-13 17:21:01 +01003995
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003996 if( ( ret = mbedtls_ssl_read_record_layer( ssl ) ) != 0 )
3997 {
3998 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
3999 return( ret );
4000 }
4001
4002 ret = mbedtls_ssl_handle_message_type( ssl );
4003
4004 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret );
4005
4006 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004007 {
4008 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4009 return( ret );
4010 }
4011
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004012 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4013 {
4014 mbedtls_ssl_update_handshake_status( ssl );
4015 }
Simon Butcher99000142016-10-13 17:21:01 +01004016 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004017 else
Simon Butcher99000142016-10-13 17:21:01 +01004018 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4020 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004021 }
4022
4023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4024
4025 return( 0 );
4026}
4027
4028int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4029{
4030 int ret;
4031
Hanno Becker4a810fb2017-05-24 16:27:30 +01004032 /*
4033 * Step A
4034 *
4035 * Consume last content-layer message and potentially
4036 * update in_msglen which keeps track of the contents'
4037 * consumption state.
4038 *
4039 * (1) Handshake messages:
4040 * Remove last handshake message, move content
4041 * and adapt in_msglen.
4042 *
4043 * (2) Alert messages:
4044 * Consume whole record content, in_msglen = 0.
4045 *
4046 * NOTE: This needs to be fixed, since like for
4047 * handshake messages it is allowed to have
4048 * multiple alerts witin a single record.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004049 * Internal reference IOTSSL-1321.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004050 *
4051 * (3) Change cipher spec:
4052 * Consume whole record content, in_msglen = 0.
4053 *
4054 * (4) Application data:
4055 * Don't do anything - the record layer provides
4056 * the application data as a stream transport
4057 * and consumes through mbedtls_ssl_read only.
4058 *
4059 */
4060
4061 /* Case (1): Handshake messages */
4062 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004063 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004064 /* Hard assertion to be sure that no application data
4065 * is in flight, as corrupting ssl->in_msglen during
4066 * ssl->in_offt != NULL is fatal. */
4067 if( ssl->in_offt != NULL )
4068 {
4069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4070 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4071 }
4072
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004073 /*
4074 * Get next Handshake message in the current record
4075 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004076
Hanno Becker4a810fb2017-05-24 16:27:30 +01004077 /* Notes:
4078 * (1) in_hslen is *NOT* necessarily the size of the
4079 * current handshake content: If DTLS handshake
4080 * fragmentation is used, that's the fragment
4081 * size instead. Using the total handshake message
4082 * size here is FAULTY and should be changed at
4083 * some point. Internal reference IOTSSL-1414.
4084 * (2) While it doesn't seem to cause problems, one
4085 * has to be very careful not to assume that in_hslen
4086 * is always <= in_msglen in a sensible communication.
4087 * Again, it's wrong for DTLS handshake fragmentation.
4088 * The following check is therefore mandatory, and
4089 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004090 * Additionally, ssl->in_hslen might be arbitrarily out of
4091 * bounds after handling a DTLS message with an unexpected
4092 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004093 */
4094 if( ssl->in_hslen < ssl->in_msglen )
4095 {
4096 ssl->in_msglen -= ssl->in_hslen;
4097 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4098 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004099
Hanno Becker4a810fb2017-05-24 16:27:30 +01004100 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4101 ssl->in_msg, ssl->in_msglen );
4102 }
4103 else
4104 {
4105 ssl->in_msglen = 0;
4106 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004107
Hanno Becker4a810fb2017-05-24 16:27:30 +01004108 ssl->in_hslen = 0;
4109 }
4110 /* Case (4): Application data */
4111 else if( ssl->in_offt != NULL )
4112 {
4113 return( 0 );
4114 }
4115 /* Everything else (CCS & Alerts) */
4116 else
4117 {
4118 ssl->in_msglen = 0;
4119 }
4120
4121 /*
4122 * Step B
4123 *
4124 * Fetch and decode new record if current one is fully consumed.
4125 *
4126 */
4127
4128 if( ssl->in_msglen > 0 )
4129 {
4130 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004131 return( 0 );
4132 }
4133
Hanno Becker4a810fb2017-05-24 16:27:30 +01004134 /* Need to fetch a new record */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004135
Simon Butcher99000142016-10-13 17:21:01 +01004136#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004137read_record_header:
Simon Butcher99000142016-10-13 17:21:01 +01004138#endif
4139
Hanno Becker4a810fb2017-05-24 16:27:30 +01004140 /* Current record either fully processed or to be discarded. */
4141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004142 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004145 return( ret );
4146 }
4147
4148 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004150#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004151 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4152 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004153 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004154 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4155 {
4156 /* Skip unexpected record (but not whole datagram) */
4157 ssl->next_record_offset = ssl->in_msglen
4158 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004159
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4161 "(header)" ) );
4162 }
4163 else
4164 {
4165 /* Skip invalid record and the rest of the datagram */
4166 ssl->next_record_offset = 0;
4167 ssl->in_left = 0;
4168
4169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4170 "(header)" ) );
4171 }
4172
4173 /* Get next record */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004174 goto read_record_header;
4175 }
4176#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004177 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004178 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004179
4180 /*
4181 * Read and optionally decrypt the message contents
4182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4184 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004187 return( ret );
4188 }
4189
4190 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004192 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004193 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004194 else
4195#endif
4196 ssl->in_left = 0;
4197
4198 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004201 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004202 {
4203 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4205 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004206 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004207 /* Except when waiting for Finished as a bad mac here
4208 * probably means something went wrong in the handshake
4209 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4210 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4211 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4212 {
4213#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4214 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4215 {
4216 mbedtls_ssl_send_alert_message( ssl,
4217 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4218 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4219 }
4220#endif
4221 return( ret );
4222 }
4223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004225 if( ssl->conf->badmac_limit != 0 &&
4226 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4229 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004230 }
4231#endif
4232
Hanno Becker4a810fb2017-05-24 16:27:30 +01004233 /* As above, invalid records cause
4234 * dismissal of the whole datagram. */
4235
4236 ssl->next_record_offset = 0;
4237 ssl->in_left = 0;
4238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004240 goto read_record_header;
4241 }
4242
4243 return( ret );
4244 }
4245 else
4246#endif
4247 {
4248 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4250 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252 mbedtls_ssl_send_alert_message( ssl,
4253 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4254 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004255 }
4256#endif
4257 return( ret );
4258 }
4259 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004260
4261 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004262 * When we sent the last flight of the handshake, we MUST respond to a
4263 * retransmit of the peer's previous flight with a retransmit. (In
4264 * practice, only the Finished message will make it, other messages
4265 * including CCS use the old transform so they're dropped as invalid.)
4266 *
4267 * If the record we received is not a handshake message, however, it
4268 * means the peer received our last flight so we can clean up
4269 * handshake info.
4270 *
4271 * This check needs to be done before prepare_handshake() due to an edge
4272 * case: if the client immediately requests renegotiation, this
4273 * finishes the current handshake first, avoiding the new ClientHello
4274 * being mistaken for an ancient message in the current handshake.
4275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004276#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004277 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004278 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004279 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4282 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004286 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004288 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004289 return( ret );
4290 }
4291
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01004292 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004293 }
4294 else
4295 {
4296 ssl_handshake_wrapup_free_hs_transform( ssl );
4297 }
4298 }
4299#endif
4300
Simon Butcher99000142016-10-13 17:21:01 +01004301 return( 0 );
4302}
4303
4304int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4305{
4306 int ret;
4307
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004308 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004309 * Handle particular types of records
4310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004312 {
Simon Butcher99000142016-10-13 17:21:01 +01004313 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4314 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004315 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004316 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004317 }
4318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004320 {
Angus Gratton8946b0d2018-06-20 15:43:50 +10004321 if( ssl->in_msglen != 2 )
4322 {
4323 /* Note: Standard allows for more than one 2 byte alert
4324 to be packed in a single message, but Mbed TLS doesn't
4325 currently support this. */
4326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4327 ssl->in_msglen ) );
4328 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4329 }
4330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004332 ssl->in_msg[0], ssl->in_msg[1] ) );
4333
4334 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004335 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004337 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004340 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004342 }
4343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004344 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4345 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4348 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004349 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004350
4351#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4352 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4353 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4354 {
4355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4356 /* Will be handled when trying to parse ServerHello */
4357 return( 0 );
4358 }
4359#endif
4360
4361#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4362 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4363 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4364 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4365 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4366 {
4367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4368 /* Will be handled in mbedtls_ssl_parse_certificate() */
4369 return( 0 );
4370 }
4371#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4372
4373 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004374 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004375 }
4376
Paul Bakker5121ce52009-01-03 21:22:43 +00004377 return( 0 );
4378}
4379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004380int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004381{
4382 int ret;
4383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4385 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4386 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004387 {
4388 return( ret );
4389 }
4390
4391 return( 0 );
4392}
4393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004394int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004395 unsigned char level,
4396 unsigned char message )
4397{
4398 int ret;
4399
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004400 if( ssl == NULL || ssl->conf == NULL )
4401 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004407 ssl->out_msglen = 2;
4408 ssl->out_msg[0] = level;
4409 ssl->out_msg[1] = message;
4410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004414 return( ret );
4415 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004417
4418 return( 0 );
4419}
4420
Paul Bakker5121ce52009-01-03 21:22:43 +00004421/*
4422 * Handshake functions
4423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004424#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4425 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4426 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4427 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4428 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4429 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4430 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004431/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004433{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004434 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004438 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4439 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004440 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4441 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004444 ssl->state++;
4445 return( 0 );
4446 }
4447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4449 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004450}
4451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004453{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004458 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4459 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004460 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4461 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004464 ssl->state++;
4465 return( 0 );
4466 }
4467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4469 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004470}
Gilles Peskinef9828522017-05-03 12:28:43 +02004471
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004472#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004473/* Some certificate support -> implement write and parse */
4474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004475int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004476{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004477 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004478 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004479 const mbedtls_x509_crt *crt;
4480 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4485 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004486 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4487 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004490 ssl->state++;
4491 return( 0 );
4492 }
4493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004494#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004495 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004496 {
4497 if( ssl->client_auth == 0 )
4498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004500 ssl->state++;
4501 return( 0 );
4502 }
4503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004504#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004505 /*
4506 * If using SSLv3 and got no cert, send an Alert message
4507 * (otherwise an empty Certificate message will be sent).
4508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004509 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4510 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004511 {
4512 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004513 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4514 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4515 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004518 goto write_msg;
4519 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004520#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004521 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004522#endif /* MBEDTLS_SSL_CLI_C */
4523#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004524 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4529 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004530 }
4531 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004532#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004534 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004535
4536 /*
4537 * 0 . 0 handshake type
4538 * 1 . 3 handshake length
4539 * 4 . 6 length of all certs
4540 * 7 . 9 length of cert. 1
4541 * 10 . n-1 peer certificate
4542 * n . n+2 length of cert. 2
4543 * n+3 . ... upper level cert, etc.
4544 */
4545 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004547
Paul Bakker29087132010-03-21 21:03:34 +00004548 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004549 {
4550 n = crt->raw.len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551 if( n > MBEDTLS_SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
4554 i + 3 + n, MBEDTLS_SSL_MAX_CONTENT_LEN ) );
4555 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004556 }
4557
4558 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4559 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4560 ssl->out_msg[i + 2] = (unsigned char)( n );
4561
4562 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4563 i += n; crt = crt->next;
4564 }
4565
4566 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4567 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4568 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4569
4570 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4572 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004573
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004574#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004575write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004576#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004577
4578 ssl->state++;
4579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004580 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004583 return( ret );
4584 }
4585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004587
Paul Bakkered27a042013-04-18 22:46:23 +02004588 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004589}
4590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004591int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004592{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004594 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004596 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004597 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004601 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4602 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004603 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4604 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004607 ssl->state++;
4608 return( 0 );
4609 }
4610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004611#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004612 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004613 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4614 {
4615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4616 ssl->state++;
4617 return( 0 );
4618 }
4619
4620#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4621 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4622 authmode = ssl->handshake->sni_authmode;
4623#endif
4624
4625 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4626 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004627 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004628 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004630 ssl->state++;
4631 return( 0 );
4632 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004633#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004636 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004637 /* mbedtls_ssl_read_record may have sent an alert already. We
4638 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004640 return( ret );
4641 }
4642
4643 ssl->state++;
4644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004645#if defined(MBEDTLS_SSL_SRV_C)
4646#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004647 /*
4648 * Check if the client sent an empty certificate
4649 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004650 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004651 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004652 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004653 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004654 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4655 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4656 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004659
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004660 /* The client was asked for a certificate but didn't send
4661 one. The client should know what's going on, so we
4662 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004663 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004664 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004665 return( 0 );
4666 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004667 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004668 }
4669 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004670#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004672#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4673 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004674 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004675 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004677 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4678 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4679 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4680 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004683
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004684 /* The client was asked for a certificate but didn't send
4685 one. The client should know what's going on, so we
4686 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004687 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004688 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004689 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004690 else
4691 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004692 }
4693 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004694#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4695 MBEDTLS_SSL_PROTO_TLS1_2 */
4696#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004698 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004701 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4702 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004703 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004704 }
4705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004706 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4707 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004710 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4711 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004713 }
4714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004715 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004716
Paul Bakker5121ce52009-01-03 21:22:43 +00004717 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004718 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004719 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004720 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004721
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004722 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004726 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4727 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004729 }
4730
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004731 /* In case we tried to reuse a session but it failed */
4732 if( ssl->session_negotiate->peer_cert != NULL )
4733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004734 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4735 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004736 }
4737
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004738 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004739 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004740 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004743 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4744 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004745 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004746 }
4747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004749
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004750 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004751
4752 while( i < ssl->in_hslen )
4753 {
Philippe Antoine33e5c322018-07-09 10:39:02 +02004754 if ( i + 3 > ssl->in_hslen ) {
4755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4756 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4757 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4758 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
4759 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004760 if( ssl->in_msg[i] != 0 )
4761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004763 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4764 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004766 }
4767
4768 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4769 | (unsigned int) ssl->in_msg[i + 2];
4770 i += 3;
4771
4772 if( n < 128 || i + n > ssl->in_hslen )
4773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004775 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4776 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004777 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004778 }
4779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004780 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004781 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004782 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004783 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004784 case 0: /*ok*/
4785 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
4786 /* Ignore certificate with an unknown algorithm: maybe a
4787 prior certificate was already trusted. */
4788 break;
4789
4790 case MBEDTLS_ERR_X509_ALLOC_FAILED:
4791 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
4792 goto crt_parse_der_failed;
4793
4794 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
4795 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4796 goto crt_parse_der_failed;
4797
4798 default:
4799 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4800 crt_parse_der_failed:
4801 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004802 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004803 return( ret );
4804 }
4805
4806 i += n;
4807 }
4808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004810
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004811 /*
4812 * On client, make sure the server cert doesn't change during renego to
4813 * avoid "triple handshake" attack: https://secure-resumption.com/
4814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004816 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004818 {
4819 if( ssl->session->peer_cert == NULL )
4820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004822 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4823 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004825 }
4826
4827 if( ssl->session->peer_cert->raw.len !=
4828 ssl->session_negotiate->peer_cert->raw.len ||
4829 memcmp( ssl->session->peer_cert->raw.p,
4830 ssl->session_negotiate->peer_cert->raw.p,
4831 ssl->session->peer_cert->raw.len ) != 0 )
4832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004834 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4835 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004836 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004837 }
4838 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004840
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004841 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004842 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004843 mbedtls_x509_crt *ca_chain;
4844 mbedtls_x509_crl *ca_crl;
4845
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004846#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004847 if( ssl->handshake->sni_ca_chain != NULL )
4848 {
4849 ca_chain = ssl->handshake->sni_ca_chain;
4850 ca_crl = ssl->handshake->sni_ca_crl;
4851 }
4852 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004853#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004854 {
4855 ca_chain = ssl->conf->ca_chain;
4856 ca_crl = ssl->conf->ca_crl;
4857 }
4858
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004859 /*
4860 * Main check: verify certificate
4861 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004862 ret = mbedtls_x509_crt_verify_with_profile(
4863 ssl->session_negotiate->peer_cert,
4864 ca_chain, ca_crl,
4865 ssl->conf->cert_profile,
4866 ssl->hostname,
4867 &ssl->session_negotiate->verify_result,
4868 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004869
4870 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004872 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004873 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004874
4875 /*
4876 * Secondary checks: always done, but change 'ret' only if it was 0
4877 */
4878
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004879#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004881 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004882
4883 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004884 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004885 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004886 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004887 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
4888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004890 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004891 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004892 }
4893 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004894#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004896 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004897 ciphersuite_info,
4898 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004899 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004902 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004903 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004904 }
4905
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004906 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
4907 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
4908 * with details encoded in the verification flags. All other kinds
4909 * of error codes, including those from the user provided f_vrfy
4910 * functions, are treated as fatal and lead to a failure of
4911 * ssl_parse_certificate even if verification was optional. */
4912 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
4913 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
4914 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
4915 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004916 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004917 }
4918
4919 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
4920 {
4921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
4922 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
4923 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004924
4925 if( ret != 0 )
4926 {
4927 /* The certificate may have been rejected for several reasons.
4928 Pick one and send the corresponding alert. Which alert to send
4929 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004930 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
4931 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
4932 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
4933 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4934 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
4935 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4936 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
4937 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4938 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
4939 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4940 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
4941 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4942 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
4943 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4944 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
4945 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
4946 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
4947 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
4948 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
4949 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02004950 else
4951 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004952 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4953 alert );
4954 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01004955
Hanno Beckere6706e62017-05-15 16:05:15 +01004956#if defined(MBEDTLS_DEBUG_C)
4957 if( ssl->session_negotiate->verify_result != 0 )
4958 {
4959 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
4960 ssl->session_negotiate->verify_result ) );
4961 }
4962 else
4963 {
4964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
4965 }
4966#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004967 }
4968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004970
4971 return( ret );
4972}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
4974 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
4975 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
4976 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4977 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4978 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
4979 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004981int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004982{
4983 int ret;
4984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004987 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004988 ssl->out_msglen = 1;
4989 ssl->out_msg[0] = 1;
4990
Paul Bakker5121ce52009-01-03 21:22:43 +00004991 ssl->state++;
4992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004993 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004995 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004996 return( ret );
4997 }
4998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005000
5001 return( 0 );
5002}
5003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005004int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005005{
5006 int ret;
5007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005010 if( ( ret = mbedtls_ssl_read_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_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005013 return( ret );
5014 }
5015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005016 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005019 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5020 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005021 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005022 }
5023
5024 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005027 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5028 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005029 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005030 }
5031
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005032 /*
5033 * Switch to our negotiated transform and session parameters for inbound
5034 * data.
5035 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005036 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005037 ssl->transform_in = ssl->transform_negotiate;
5038 ssl->session_in = ssl->session_negotiate;
5039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005040#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005041 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005044 ssl_dtls_replay_reset( ssl );
5045#endif
5046
5047 /* Increment epoch */
5048 if( ++ssl->in_epoch == 0 )
5049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005051 /* This is highly unlikely to happen for legitimate reasons, so
5052 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005053 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005054 }
5055 }
5056 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005057#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005058 memset( ssl->in_ctr, 0, 8 );
5059
5060 /*
5061 * Set the in_msg pointer to the correct location based on IV length
5062 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005063 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005064 {
5065 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
5066 ssl->transform_negotiate->fixed_ivlen;
5067 }
5068 else
5069 ssl->in_msg = ssl->in_iv;
5070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5072 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005077 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5078 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005079 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005080 }
5081 }
5082#endif
5083
Paul Bakker5121ce52009-01-03 21:22:43 +00005084 ssl->state++;
5085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005087
5088 return( 0 );
5089}
5090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005091void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5092 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005093{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005094 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005096#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5097 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5098 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005099 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005100 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005101#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005102#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5103#if defined(MBEDTLS_SHA512_C)
5104 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005105 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5106 else
5107#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005108#if defined(MBEDTLS_SHA256_C)
5109 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005110 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005111 else
5112#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005113#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005116 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005117 }
Paul Bakker380da532012-04-18 16:10:25 +00005118}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005122#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5123 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005124 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5125 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005126#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005127#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5128#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005129 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005130#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005131#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005132 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005133#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005134#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005135}
5136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005138 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005140#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5141 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005142 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5143 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005144#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005145#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5146#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005147 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005148#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005149#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005150 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005151#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005152#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005153}
5154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005155#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5156 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5157static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005158 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005159{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005160 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5161 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005162}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005163#endif
Paul Bakker380da532012-04-18 16:10:25 +00005164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005165#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5166#if defined(MBEDTLS_SHA256_C)
5167static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005168 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005169{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005170 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005171}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005172#endif
Paul Bakker380da532012-04-18 16:10:25 +00005173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005174#if defined(MBEDTLS_SHA512_C)
5175static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005176 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005177{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005178 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005179}
Paul Bakker769075d2012-11-24 11:26:46 +01005180#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005181#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005183#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005184static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005185 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005186{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005187 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005188 mbedtls_md5_context md5;
5189 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005190
Paul Bakker5121ce52009-01-03 21:22:43 +00005191 unsigned char padbuf[48];
5192 unsigned char md5sum[16];
5193 unsigned char sha1sum[20];
5194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005195 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005196 if( !session )
5197 session = ssl->session;
5198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005200
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005201 mbedtls_md5_init( &md5 );
5202 mbedtls_sha1_init( &sha1 );
5203
5204 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5205 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005206
5207 /*
5208 * SSLv3:
5209 * hash =
5210 * MD5( master + pad2 +
5211 * MD5( handshake + sender + master + pad1 ) )
5212 * + SHA1( master + pad2 +
5213 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005214 */
5215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005216#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005217 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5218 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005219#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005222 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5223 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005224#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005226 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005227 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005228
Paul Bakker1ef83d62012-04-11 12:09:53 +00005229 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005230
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005231 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5232 mbedtls_md5_update_ret( &md5, session->master, 48 );
5233 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5234 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005235
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005236 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5237 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5238 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5239 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005240
Paul Bakker1ef83d62012-04-11 12:09:53 +00005241 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005242
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005243 mbedtls_md5_starts_ret( &md5 );
5244 mbedtls_md5_update_ret( &md5, session->master, 48 );
5245 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5246 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5247 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005248
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005249 mbedtls_sha1_starts_ret( &sha1 );
5250 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5251 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5252 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5253 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005255 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005256
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005257 mbedtls_md5_free( &md5 );
5258 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005260 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
5261 mbedtls_zeroize( md5sum, sizeof( md5sum ) );
5262 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005265}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005266#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005268#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005269static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005270 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005271{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005272 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005273 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005274 mbedtls_md5_context md5;
5275 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005276 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005278 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005279 if( !session )
5280 session = ssl->session;
5281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005283
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005284 mbedtls_md5_init( &md5 );
5285 mbedtls_sha1_init( &sha1 );
5286
5287 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5288 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005289
Paul Bakker1ef83d62012-04-11 12:09:53 +00005290 /*
5291 * TLSv1:
5292 * hash = PRF( master, finished_label,
5293 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5294 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005297 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5298 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005299#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005301#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005302 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5303 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005304#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005306 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005307 ? "client finished"
5308 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005309
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005310 mbedtls_md5_finish_ret( &md5, padbuf );
5311 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005312
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005313 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005314 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005316 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005317
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005318 mbedtls_md5_free( &md5 );
5319 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005321 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005324}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005327#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5328#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005329static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005330 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005331{
5332 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005333 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005334 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005335 unsigned char padbuf[32];
5336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005338 if( !session )
5339 session = ssl->session;
5340
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005341 mbedtls_sha256_init( &sha256 );
5342
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005344
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005345 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005346
5347 /*
5348 * TLSv1.2:
5349 * hash = PRF( master, finished_label,
5350 * Hash( handshake ) )[0.11]
5351 */
5352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353#if !defined(MBEDTLS_SHA256_ALT)
5354 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005355 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005356#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005358 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005359 ? "client finished"
5360 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005361
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005362 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005363
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005364 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005365 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005368
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005369 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005371 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005374}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005375#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005378static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005379 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005380{
5381 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005382 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005383 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005384 unsigned char padbuf[48];
5385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005387 if( !session )
5388 session = ssl->session;
5389
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005390 mbedtls_sha512_init( &sha512 );
5391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005393
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005394 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005395
5396 /*
5397 * TLSv1.2:
5398 * hash = PRF( master, finished_label,
5399 * Hash( handshake ) )[0.11]
5400 */
5401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005403 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5404 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005405#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005408 ? "client finished"
5409 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005410
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005411 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005412
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005413 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005414 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005417
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005418 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005420 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005423}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424#endif /* MBEDTLS_SHA512_C */
5425#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005428{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005429 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005430
5431 /*
5432 * Free our handshake params
5433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005434 mbedtls_ssl_handshake_free( ssl->handshake );
5435 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005436 ssl->handshake = NULL;
5437
5438 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005439 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005440 */
5441 if( ssl->transform )
5442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005443 mbedtls_ssl_transform_free( ssl->transform );
5444 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005445 }
5446 ssl->transform = ssl->transform_negotiate;
5447 ssl->transform_negotiate = NULL;
5448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005449 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005450}
5451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005453{
5454 int resume = ssl->handshake->resume;
5455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005456 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458#if defined(MBEDTLS_SSL_RENEGOTIATION)
5459 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005462 ssl->renego_records_seen = 0;
5463 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005464#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005465
5466 /*
5467 * Free the previous session and switch in the current one
5468 */
Paul Bakker0a597072012-09-25 21:55:46 +00005469 if( ssl->session )
5470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005472 /* RFC 7366 3.1: keep the EtM state */
5473 ssl->session_negotiate->encrypt_then_mac =
5474 ssl->session->encrypt_then_mac;
5475#endif
5476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477 mbedtls_ssl_session_free( ssl->session );
5478 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005479 }
5480 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005481 ssl->session_negotiate = NULL;
5482
Paul Bakker0a597072012-09-25 21:55:46 +00005483 /*
5484 * Add cache entry
5485 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005486 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005487 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005488 resume == 0 )
5489 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005490 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005492 }
Paul Bakker0a597072012-09-25 21:55:46 +00005493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005495 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005496 ssl->handshake->flight != NULL )
5497 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005498 /* Cancel handshake timer */
5499 ssl_set_timer( ssl, 0 );
5500
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005501 /* Keep last flight around in case we need to resend it:
5502 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005503 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005504 }
5505 else
5506#endif
5507 ssl_handshake_wrapup_free_hs_transform( ssl );
5508
Paul Bakker48916f92012-09-16 19:57:18 +00005509 ssl->state++;
5510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005512}
5513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005515{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005516 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005519
Paul Bakker92be97b2013-01-02 17:30:03 +01005520 /*
5521 * Set the out_msg pointer to the correct location based on IV length
5522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005524 {
5525 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5526 ssl->transform_negotiate->fixed_ivlen;
5527 }
5528 else
5529 ssl->out_msg = ssl->out_iv;
5530
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005531 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005532
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005533 /*
5534 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5535 * may define some other value. Currently (early 2016), no defined
5536 * ciphersuite does this (and this is unlikely to change as activity has
5537 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005539 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005541#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005542 ssl->verify_data_len = hash_len;
5543 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005544#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005545
Paul Bakker5121ce52009-01-03 21:22:43 +00005546 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005547 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5548 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005549
5550 /*
5551 * In case of session resuming, invert the client and server
5552 * ChangeCipherSpec messages order.
5553 */
Paul Bakker0a597072012-09-25 21:55:46 +00005554 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005557 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005559#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005560#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005561 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005562 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005563#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005564 }
5565 else
5566 ssl->state++;
5567
Paul Bakker48916f92012-09-16 19:57:18 +00005568 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005569 * Switch to our negotiated transform and session parameters for outbound
5570 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005572 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005575 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005576 {
5577 unsigned char i;
5578
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005579 /* Remember current epoch settings for resending */
5580 ssl->handshake->alt_transform_out = ssl->transform_out;
5581 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5582
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005583 /* Set sequence_number to zero */
5584 memset( ssl->out_ctr + 2, 0, 6 );
5585
5586 /* Increment epoch */
5587 for( i = 2; i > 0; i-- )
5588 if( ++ssl->out_ctr[i - 1] != 0 )
5589 break;
5590
5591 /* The loop goes to its end iff the counter is wrapping */
5592 if( i == 0 )
5593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5595 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005596 }
5597 }
5598 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005600 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005601
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005602 ssl->transform_out = ssl->transform_negotiate;
5603 ssl->session_out = ssl->session_negotiate;
5604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5606 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5611 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005612 }
5613 }
5614#endif
5615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005617 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005619#endif
5620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005624 return( ret );
5625 }
5626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005628
5629 return( 0 );
5630}
5631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005633#define SSL_MAX_HASH_LEN 36
5634#else
5635#define SSL_MAX_HASH_LEN 12
5636#endif
5637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005639{
Paul Bakker23986e52011-04-24 08:57:21 +00005640 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005641 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005642 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005645
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005646 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005650 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005651 return( ret );
5652 }
5653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005657 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5658 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005659 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005660 }
5661
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005662 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005663#if defined(MBEDTLS_SSL_PROTO_SSL3)
5664 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005665 hash_len = 36;
5666 else
5667#endif
5668 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5671 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
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_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005677 }
5678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005680 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005683 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5684 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005686 }
5687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005688#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005689 ssl->verify_data_len = hash_len;
5690 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005691#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005692
Paul Bakker0a597072012-09-25 21:55:46 +00005693 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005696 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005697 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005700 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005702#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005703 }
5704 else
5705 ssl->state++;
5706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005708 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005710#endif
5711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005713
5714 return( 0 );
5715}
5716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005717static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005718{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5722 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5723 mbedtls_md5_init( &handshake->fin_md5 );
5724 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005725 mbedtls_md5_starts_ret( &handshake->fin_md5 );
5726 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005727#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5729#if defined(MBEDTLS_SHA256_C)
5730 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005731 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005732#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733#if defined(MBEDTLS_SHA512_C)
5734 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005735 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005736#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005738
5739 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005740
5741#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5742 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5743 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5744#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746#if defined(MBEDTLS_DHM_C)
5747 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005748#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749#if defined(MBEDTLS_ECDH_C)
5750 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005751#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02005752#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005753 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02005754#if defined(MBEDTLS_SSL_CLI_C)
5755 handshake->ecjpake_cache = NULL;
5756 handshake->ecjpake_cache_len = 0;
5757#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005758#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005759
5760#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5761 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5762#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005763}
5764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005765static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005767 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5770 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005772 mbedtls_md_init( &transform->md_ctx_enc );
5773 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005774}
5775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005776void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005777{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005778 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005779}
5780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005781static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005782{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005783 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005784 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005786 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005787 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005788 if( ssl->handshake )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005789 mbedtls_ssl_handshake_free( ssl->handshake );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005790
5791 /*
5792 * Either the pointers are now NULL or cleared properly and can be freed.
5793 * Now allocate missing structures.
5794 */
5795 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005796 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005797 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005798 }
Paul Bakker48916f92012-09-16 19:57:18 +00005799
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005800 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005801 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005802 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005803 }
Paul Bakker48916f92012-09-16 19:57:18 +00005804
Paul Bakker82788fb2014-10-20 13:59:19 +02005805 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005806 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005807 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005808 }
Paul Bakker48916f92012-09-16 19:57:18 +00005809
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005810 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005811 if( ssl->handshake == NULL ||
5812 ssl->transform_negotiate == NULL ||
5813 ssl->session_negotiate == NULL )
5814 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 mbedtls_free( ssl->handshake );
5818 mbedtls_free( ssl->transform_negotiate );
5819 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005820
5821 ssl->handshake = NULL;
5822 ssl->transform_negotiate = NULL;
5823 ssl->session_negotiate = NULL;
5824
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005825 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005826 }
5827
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005828 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005829 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005830 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005831 ssl_handshake_params_init( ssl->handshake );
5832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005833#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005834 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5835 {
5836 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005837
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005838 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5839 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5840 else
5841 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005842
5843 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005844 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005845#endif
5846
Paul Bakker48916f92012-09-16 19:57:18 +00005847 return( 0 );
5848}
5849
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005850#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005851/* Dummy cookie callbacks for defaults */
5852static int ssl_cookie_write_dummy( void *ctx,
5853 unsigned char **p, unsigned char *end,
5854 const unsigned char *cli_id, size_t cli_id_len )
5855{
5856 ((void) ctx);
5857 ((void) p);
5858 ((void) end);
5859 ((void) cli_id);
5860 ((void) cli_id_len);
5861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005862 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005863}
5864
5865static int ssl_cookie_check_dummy( void *ctx,
5866 const unsigned char *cookie, size_t cookie_len,
5867 const unsigned char *cli_id, size_t cli_id_len )
5868{
5869 ((void) ctx);
5870 ((void) cookie);
5871 ((void) cookie_len);
5872 ((void) cli_id);
5873 ((void) cli_id_len);
5874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005875 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005876}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005877#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005878
Paul Bakker5121ce52009-01-03 21:22:43 +00005879/*
5880 * Initialize an SSL context
5881 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005882void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
5883{
5884 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
5885}
5886
5887/*
5888 * Setup an SSL context
5889 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005890int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02005891 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00005892{
Paul Bakker48916f92012-09-16 19:57:18 +00005893 int ret;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005894 const size_t len = MBEDTLS_SSL_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005895
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005896 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00005897
5898 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005899 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00005900 */
k-stachowiakc2eddee2018-07-09 10:39:20 +02005901 ssl->in_buf = NULL;
5902 ssl->out_buf = NULL;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005903 if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL ||
5904 ( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005905 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", len ) );
k-stachowiak2c161142018-07-31 16:52:32 +02005907 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiakc2eddee2018-07-09 10:39:20 +02005908 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005909 }
5910
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005911#if defined(MBEDTLS_SSL_PROTO_DTLS)
5912 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5913 {
5914 ssl->out_hdr = ssl->out_buf;
5915 ssl->out_ctr = ssl->out_buf + 3;
5916 ssl->out_len = ssl->out_buf + 11;
5917 ssl->out_iv = ssl->out_buf + 13;
5918 ssl->out_msg = ssl->out_buf + 13;
5919
5920 ssl->in_hdr = ssl->in_buf;
5921 ssl->in_ctr = ssl->in_buf + 3;
5922 ssl->in_len = ssl->in_buf + 11;
5923 ssl->in_iv = ssl->in_buf + 13;
5924 ssl->in_msg = ssl->in_buf + 13;
5925 }
5926 else
5927#endif
5928 {
5929 ssl->out_ctr = ssl->out_buf;
5930 ssl->out_hdr = ssl->out_buf + 8;
5931 ssl->out_len = ssl->out_buf + 11;
5932 ssl->out_iv = ssl->out_buf + 13;
5933 ssl->out_msg = ssl->out_buf + 13;
5934
5935 ssl->in_ctr = ssl->in_buf;
5936 ssl->in_hdr = ssl->in_buf + 8;
5937 ssl->in_len = ssl->in_buf + 11;
5938 ssl->in_iv = ssl->in_buf + 13;
5939 ssl->in_msg = ssl->in_buf + 13;
5940 }
5941
Paul Bakker48916f92012-09-16 19:57:18 +00005942 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiakc2eddee2018-07-09 10:39:20 +02005943 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005944
5945 return( 0 );
k-stachowiakc2eddee2018-07-09 10:39:20 +02005946
5947error:
5948 mbedtls_free( ssl->in_buf );
5949 mbedtls_free( ssl->out_buf );
5950
5951 ssl->conf = NULL;
5952
5953 ssl->in_buf = NULL;
5954 ssl->out_buf = NULL;
5955
5956 ssl->in_hdr = NULL;
5957 ssl->in_ctr = NULL;
5958 ssl->in_len = NULL;
5959 ssl->in_iv = NULL;
5960 ssl->in_msg = NULL;
5961
5962 ssl->out_hdr = NULL;
5963 ssl->out_ctr = NULL;
5964 ssl->out_len = NULL;
5965 ssl->out_iv = NULL;
5966 ssl->out_msg = NULL;
5967
k-stachowiak2c161142018-07-31 16:52:32 +02005968 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005969}
5970
5971/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00005972 * Reset an initialized and used SSL context for re-use while retaining
5973 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005974 *
5975 * If partial is non-zero, keep data in the input buffer and client ID.
5976 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00005977 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005978static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00005979{
Paul Bakker48916f92012-09-16 19:57:18 +00005980 int ret;
5981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005983
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005984 /* Cancel any possibly running timer */
5985 ssl_set_timer( ssl, 0 );
5986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987#if defined(MBEDTLS_SSL_RENEGOTIATION)
5988 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005989 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005990
5991 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
5993 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005994#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00005996
Paul Bakker7eb013f2011-10-06 12:37:39 +00005997 ssl->in_offt = NULL;
5998
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005999 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006000 ssl->in_msgtype = 0;
6001 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006002 if( partial == 0 )
6003 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006004#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006005 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006006 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006007#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006009 ssl_dtls_replay_reset( ssl );
6010#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006011
6012 ssl->in_hslen = 0;
6013 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006014
6015 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006016
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006017 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006018 ssl->out_msgtype = 0;
6019 ssl->out_msglen = 0;
6020 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6022 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006023 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006024#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006025
Paul Bakker48916f92012-09-16 19:57:18 +00006026 ssl->transform_in = NULL;
6027 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006028
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006029 ssl->session_in = NULL;
6030 ssl->session_out = NULL;
6031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 memset( ssl->out_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006033
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006034 if( partial == 0 )
6035 memset( ssl->in_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00006036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006037#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6038 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6041 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6044 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006045 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006046 }
6047#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006048
Paul Bakker48916f92012-09-16 19:57:18 +00006049 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051 mbedtls_ssl_transform_free( ssl->transform );
6052 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006053 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006054 }
Paul Bakker48916f92012-09-16 19:57:18 +00006055
Paul Bakkerc0463502013-02-14 11:19:38 +01006056 if( ssl->session )
6057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058 mbedtls_ssl_session_free( ssl->session );
6059 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006060 ssl->session = NULL;
6061 }
6062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006064 ssl->alpn_chosen = NULL;
6065#endif
6066
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006067#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006068 if( partial == 0 )
6069 {
6070 mbedtls_free( ssl->cli_id );
6071 ssl->cli_id = NULL;
6072 ssl->cli_id_len = 0;
6073 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006074#endif
6075
Paul Bakker48916f92012-09-16 19:57:18 +00006076 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6077 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006078
6079 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006080}
6081
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006082/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006083 * Reset an initialized and used SSL context for re-use while retaining
6084 * all application-set variables, function pointers and data.
6085 */
6086int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6087{
6088 return( ssl_session_reset_int( ssl, 0 ) );
6089}
6090
6091/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006092 * SSL set accessors
6093 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006094void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006095{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006096 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006097}
6098
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006099void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006100{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006101 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006102}
6103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006104#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006105void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006106{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006107 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006108}
6109#endif
6110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006112void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006113{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006114 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006115}
6116#endif
6117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006119void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006120{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006121 conf->hs_timeout_min = min;
6122 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006123}
6124#endif
6125
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006126void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006127{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006128 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006129}
6130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006131#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006132void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006133 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006134 void *p_vrfy )
6135{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006136 conf->f_vrfy = f_vrfy;
6137 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006138}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006140
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006141void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006142 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006143 void *p_rng )
6144{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006145 conf->f_rng = f_rng;
6146 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006147}
6148
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006149void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006150 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006151 void *p_dbg )
6152{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006153 conf->f_dbg = f_dbg;
6154 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006155}
6156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006158 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006159 mbedtls_ssl_send_t *f_send,
6160 mbedtls_ssl_recv_t *f_recv,
6161 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006162{
6163 ssl->p_bio = p_bio;
6164 ssl->f_send = f_send;
6165 ssl->f_recv = f_recv;
6166 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006167}
6168
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006169void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006170{
6171 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006172}
6173
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006174void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6175 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006176 mbedtls_ssl_set_timer_t *f_set_timer,
6177 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006178{
6179 ssl->p_timer = p_timer;
6180 ssl->f_set_timer = f_set_timer;
6181 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006182
6183 /* Make sure we start with no timer running */
6184 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006185}
6186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006188void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006189 void *p_cache,
6190 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6191 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006192{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006193 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006194 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006195 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006196}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199#if defined(MBEDTLS_SSL_CLI_C)
6200int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006201{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006202 int ret;
6203
6204 if( ssl == NULL ||
6205 session == NULL ||
6206 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006207 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006210 }
6211
6212 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6213 return( ret );
6214
Paul Bakker0a597072012-09-25 21:55:46 +00006215 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006216
6217 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006218}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006219#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006220
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006221void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006222 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006223{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006224 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6225 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6226 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6227 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006228}
6229
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006230void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006231 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006232 int major, int minor )
6233{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006235 return;
6236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006238 return;
6239
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006240 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006241}
6242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006244void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006245 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006246{
6247 conf->cert_profile = profile;
6248}
6249
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006250/* Append a new keycert entry to a (possibly empty) list */
6251static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6252 mbedtls_x509_crt *cert,
6253 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006254{
niisatoa35dbf12018-06-25 19:05:48 +09006255 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006256
niisatoa35dbf12018-06-25 19:05:48 +09006257 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6258 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006259 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006260
niisatoa35dbf12018-06-25 19:05:48 +09006261 new_cert->cert = cert;
6262 new_cert->key = key;
6263 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006264
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006265 /* Update head is the list was null, else add to the end */
6266 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006267 {
niisatoa35dbf12018-06-25 19:05:48 +09006268 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006269 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006270 else
6271 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006272 mbedtls_ssl_key_cert *cur = *head;
6273 while( cur->next != NULL )
6274 cur = cur->next;
niisatoa35dbf12018-06-25 19:05:48 +09006275 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006276 }
6277
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006278 return( 0 );
6279}
6280
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006281int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006282 mbedtls_x509_crt *own_cert,
6283 mbedtls_pk_context *pk_key )
6284{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006285 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006286}
6287
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006288void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006289 mbedtls_x509_crt *ca_chain,
6290 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006291{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006292 conf->ca_chain = ca_chain;
6293 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006294}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006295#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006296
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006297#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6298int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6299 mbedtls_x509_crt *own_cert,
6300 mbedtls_pk_context *pk_key )
6301{
6302 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6303 own_cert, pk_key ) );
6304}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006305
6306void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6307 mbedtls_x509_crt *ca_chain,
6308 mbedtls_x509_crl *ca_crl )
6309{
6310 ssl->handshake->sni_ca_chain = ca_chain;
6311 ssl->handshake->sni_ca_crl = ca_crl;
6312}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006313
6314void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6315 int authmode )
6316{
6317 ssl->handshake->sni_authmode = authmode;
6318}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006319#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6320
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006321#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006322/*
6323 * Set EC J-PAKE password for current handshake
6324 */
6325int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6326 const unsigned char *pw,
6327 size_t pw_len )
6328{
6329 mbedtls_ecjpake_role role;
6330
Janos Follath8eb64132016-06-03 15:40:57 +01006331 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006332 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6333
6334 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6335 role = MBEDTLS_ECJPAKE_SERVER;
6336 else
6337 role = MBEDTLS_ECJPAKE_CLIENT;
6338
6339 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6340 role,
6341 MBEDTLS_MD_SHA256,
6342 MBEDTLS_ECP_DP_SECP256R1,
6343 pw, pw_len ) );
6344}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006345#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006348int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006349 const unsigned char *psk, size_t psk_len,
6350 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006351{
Paul Bakker6db455e2013-09-18 17:29:31 +02006352 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6356 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006357
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006358 /* Identity len will be encoded on two bytes */
6359 if( ( psk_identity_len >> 16 ) != 0 ||
6360 psk_identity_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
6361 {
6362 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6363 }
6364
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006365 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006366 {
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006367 mbedtls_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006368
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006369 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006370 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006371 conf->psk_len = 0;
6372 }
6373 if( conf->psk_identity != NULL )
6374 {
6375 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006376 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006377 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006378 }
6379
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006380 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6381 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006382 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006383 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006384 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006385 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006386 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006387 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006388 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006389
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006390 conf->psk_len = psk_len;
6391 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006392
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006393 memcpy( conf->psk, psk, conf->psk_len );
6394 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006395
6396 return( 0 );
6397}
6398
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006399int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6400 const unsigned char *psk, size_t psk_len )
6401{
6402 if( psk == NULL || ssl->handshake == NULL )
6403 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6404
6405 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6406 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6407
6408 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006409 {
6410 mbedtls_zeroize( ssl->handshake->psk, ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006411 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006412 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006413 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006414
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006415 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006416 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006417
6418 ssl->handshake->psk_len = psk_len;
6419 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6420
6421 return( 0 );
6422}
6423
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006424void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006425 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006426 size_t),
6427 void *p_psk )
6428{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006429 conf->f_psk = f_psk;
6430 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006431}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006432#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006433
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006434#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006435
6436#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006437int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006438{
6439 int ret;
6440
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006441 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6442 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6443 {
6444 mbedtls_mpi_free( &conf->dhm_P );
6445 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006446 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006447 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006448
6449 return( 0 );
6450}
Hanno Becker470a8c42017-10-04 15:28:46 +01006451#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006452
Hanno Beckera90658f2017-10-04 15:29:08 +01006453int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6454 const unsigned char *dhm_P, size_t P_len,
6455 const unsigned char *dhm_G, size_t G_len )
6456{
6457 int ret;
6458
6459 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6460 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6461 {
6462 mbedtls_mpi_free( &conf->dhm_P );
6463 mbedtls_mpi_free( &conf->dhm_G );
6464 return( ret );
6465 }
6466
6467 return( 0 );
6468}
Paul Bakker5121ce52009-01-03 21:22:43 +00006469
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006470int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006471{
6472 int ret;
6473
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006474 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6475 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6476 {
6477 mbedtls_mpi_free( &conf->dhm_P );
6478 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006479 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006480 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006481
6482 return( 0 );
6483}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006484#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006485
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006486#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6487/*
6488 * Set the minimum length for Diffie-Hellman parameters
6489 */
6490void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6491 unsigned int bitlen )
6492{
6493 conf->dhm_min_bitlen = bitlen;
6494}
6495#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6496
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006497#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006498/*
6499 * Set allowed/preferred hashes for handshake signatures
6500 */
6501void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6502 const int *hashes )
6503{
6504 conf->sig_hashes = hashes;
6505}
Hanno Becker947194e2017-04-07 13:25:49 +01006506#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006507
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006508#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006509/*
6510 * Set the allowed elliptic curves
6511 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006512void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006513 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006514{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006515 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006516}
Hanno Becker947194e2017-04-07 13:25:49 +01006517#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006518
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006519#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006520int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006521{
Hanno Becker947194e2017-04-07 13:25:49 +01006522 /* Initialize to suppress unnecessary compiler warning */
6523 size_t hostname_len = 0;
6524
6525 /* Check if new hostname is valid before
6526 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006527 if( hostname != NULL )
6528 {
6529 hostname_len = strlen( hostname );
6530
6531 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6532 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6533 }
6534
6535 /* Now it's clear that we will overwrite the old hostname,
6536 * so we can free it safely */
6537
6538 if( ssl->hostname != NULL )
6539 {
6540 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
6541 mbedtls_free( ssl->hostname );
6542 }
6543
6544 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006545
Paul Bakker5121ce52009-01-03 21:22:43 +00006546 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006547 {
6548 ssl->hostname = NULL;
6549 }
6550 else
6551 {
6552 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006553 if( ssl->hostname == NULL )
6554 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006555
Hanno Becker947194e2017-04-07 13:25:49 +01006556 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006557
Hanno Becker947194e2017-04-07 13:25:49 +01006558 ssl->hostname[hostname_len] = '\0';
6559 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006560
6561 return( 0 );
6562}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006563#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006564
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006565#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006566void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006568 const unsigned char *, size_t),
6569 void *p_sni )
6570{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006571 conf->f_sni = f_sni;
6572 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006577int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006578{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006579 size_t cur_len, tot_len;
6580 const char **p;
6581
6582 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006583 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6584 * MUST NOT be truncated."
6585 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006586 */
6587 tot_len = 0;
6588 for( p = protos; *p != NULL; p++ )
6589 {
6590 cur_len = strlen( *p );
6591 tot_len += cur_len;
6592
Ronald Crona32236c2020-04-23 16:41:44 +02006593 if( ( cur_len == 0 ) ||
6594 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
6595 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006597 }
6598
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006599 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006600
6601 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006602}
6603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006605{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006606 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006607}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006608#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006609
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006610void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006611{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006612 conf->max_major_ver = major;
6613 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006614}
6615
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006616void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006617{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006618 conf->min_major_ver = major;
6619 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006620}
6621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006622#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006623void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006624{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006625 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006626}
6627#endif
6628
Janos Follath088ce432017-04-10 12:42:31 +01006629#if defined(MBEDTLS_SSL_SRV_C)
6630void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6631 char cert_req_ca_list )
6632{
6633 conf->cert_req_ca_list = cert_req_ca_list;
6634}
6635#endif
6636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006638void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006639{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006640 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006641}
6642#endif
6643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006645void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006646{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006647 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006648}
6649#endif
6650
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006651#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006652void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006653{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006654 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006655}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006656#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006658#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006659int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006660{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006661 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
6662 mfl_code_to_length[mfl_code] > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006665 }
6666
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006667 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006668
6669 return( 0 );
6670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006673#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006674void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006675{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006676 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006680#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006681void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006682{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006683 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006684}
6685#endif
6686
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006687void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006688{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006689 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006690}
6691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006693void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006694{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006695 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006696}
6697
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006698void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006699{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006700 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006701}
6702
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006703void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006704 const unsigned char period[8] )
6705{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006706 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006707}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006708#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006711#if defined(MBEDTLS_SSL_CLI_C)
6712void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006713{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006714 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006715}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006716#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006717
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006718#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006719void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6720 mbedtls_ssl_ticket_write_t *f_ticket_write,
6721 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6722 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006723{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006724 conf->f_ticket_write = f_ticket_write;
6725 conf->f_ticket_parse = f_ticket_parse;
6726 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006727}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006728#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006730
Robert Cragie4feb7ae2015-10-02 13:33:37 +01006731#if defined(MBEDTLS_SSL_EXPORT_KEYS)
6732void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
6733 mbedtls_ssl_export_keys_t *f_export_keys,
6734 void *p_export_keys )
6735{
6736 conf->f_export_keys = f_export_keys;
6737 conf->p_export_keys = p_export_keys;
6738}
6739#endif
6740
Paul Bakker5121ce52009-01-03 21:22:43 +00006741/*
6742 * SSL get accessors
6743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006744size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006745{
6746 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6747}
6748
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006749uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006750{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006751 if( ssl->session != NULL )
6752 return( ssl->session->verify_result );
6753
6754 if( ssl->session_negotiate != NULL )
6755 return( ssl->session_negotiate->verify_result );
6756
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006757 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006758}
6759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006761{
Paul Bakker926c8e42013-03-06 10:23:34 +01006762 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006763 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006766}
6767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006769{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006770#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006771 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006772 {
6773 switch( ssl->minor_ver )
6774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006776 return( "DTLSv1.0" );
6777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006779 return( "DTLSv1.2" );
6780
6781 default:
6782 return( "unknown (DTLS)" );
6783 }
6784 }
6785#endif
6786
Paul Bakker43ca69c2011-01-15 17:35:19 +00006787 switch( ssl->minor_ver )
6788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006790 return( "SSLv3.0" );
6791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006793 return( "TLSv1.0" );
6794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006796 return( "TLSv1.1" );
6797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006798 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00006799 return( "TLSv1.2" );
6800
Paul Bakker43ca69c2011-01-15 17:35:19 +00006801 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006802 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00006803 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00006804}
6805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006806int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006807{
Hanno Becker12f7ede2018-08-17 15:28:19 +01006808 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006810 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006811
Hanno Beckercd6a64a2018-08-13 16:35:15 +01006812 if( transform == NULL )
6813 return( (int) mbedtls_ssl_hdr_len( ssl ) );
6814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006815#if defined(MBEDTLS_ZLIB_SUPPORT)
6816 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
6817 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006818#endif
6819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006822 case MBEDTLS_MODE_GCM:
6823 case MBEDTLS_MODE_CCM:
6824 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006825 transform_expansion = transform->minlen;
6826 break;
6827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 case MBEDTLS_MODE_CBC:
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006829
6830 block_size = mbedtls_cipher_get_block_size(
6831 &transform->cipher_ctx_enc );
6832
Hanno Becker12f7ede2018-08-17 15:28:19 +01006833 /* Expansion due to the addition of the MAC. */
6834 transform_expansion += transform->maclen;
6835
6836 /* Expansion due to the addition of CBC padding;
6837 * Theoretically up to 256 bytes, but we never use
6838 * more than the block size of the underlying cipher. */
6839 transform_expansion += block_size;
6840
6841 /* For TLS 1.1 or higher, an explicit IV is added
6842 * after the record header. */
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006843#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
6844 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker12f7ede2018-08-17 15:28:19 +01006845 transform_expansion += block_size;
Hanno Beckerdbd3e882018-08-03 09:40:07 +01006846#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker12f7ede2018-08-17 15:28:19 +01006847
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006848 break;
6849
6850 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006853 }
6854
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006855 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006856}
6857
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02006858#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
6859size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
6860{
6861 size_t max_len;
6862
6863 /*
6864 * Assume mfl_code is correct since it was checked when set
6865 */
6866 max_len = mfl_code_to_length[ssl->conf->mfl_code];
6867
6868 /*
6869 * Check if a smaller max length was negotiated
6870 */
6871 if( ssl->session_out != NULL &&
6872 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6873 {
6874 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6875 }
6876
6877 return max_len;
6878}
6879#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
6880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881#if defined(MBEDTLS_X509_CRT_PARSE_C)
6882const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00006883{
6884 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006885 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006886
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006887 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006888}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006889#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00006890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891#if defined(MBEDTLS_SSL_CLI_C)
6892int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006893{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006894 if( ssl == NULL ||
6895 dst == NULL ||
6896 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006897 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006900 }
6901
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006902 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006903}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006905
Paul Bakker5121ce52009-01-03 21:22:43 +00006906/*
Paul Bakker1961b702013-01-25 14:49:24 +01006907 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00006908 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006909int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006910{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006912
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006913 if( ssl == NULL || ssl->conf == NULL )
6914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006917 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006918 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006919#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006921 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006923#endif
6924
Paul Bakker1961b702013-01-25 14:49:24 +01006925 return( ret );
6926}
6927
6928/*
6929 * Perform the SSL handshake
6930 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006931int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01006932{
6933 int ret = 0;
6934
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006935 if( ssl == NULL || ssl->conf == NULL )
6936 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01006939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01006941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01006943
6944 if( ret != 0 )
6945 break;
6946 }
6947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006949
6950 return( ret );
6951}
6952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953#if defined(MBEDTLS_SSL_RENEGOTIATION)
6954#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006955/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006956 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00006957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006959{
6960 int ret;
6961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006963
6964 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6966 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006971 return( ret );
6972 }
6973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006975
6976 return( 0 );
6977}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006979
6980/*
6981 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982 * - any side: calling mbedtls_ssl_renegotiate(),
6983 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
6984 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02006985 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006986 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006990{
6991 int ret;
6992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006994
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006995 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6996 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006997
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006998 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
6999 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007001 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007003 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007004 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007005 ssl->handshake->out_msg_seq = 1;
7006 else
7007 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007008 }
7009#endif
7010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7012 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007014 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007017 return( ret );
7018 }
7019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007021
7022 return( 0 );
7023}
7024
7025/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007026 * Renegotiate current connection on client,
7027 * or request renegotiation on server
7028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007029int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007030{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007032
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007033 if( ssl == NULL || ssl->conf == NULL )
7034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007037 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007038 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007040 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7041 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007043 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007044
7045 /* Did we already try/start sending HelloRequest? */
7046 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007048
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007049 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007050 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007054 /*
7055 * On client, either start the renegotiation process or,
7056 * if already in progress, continue the handshake
7057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007060 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007062
7063 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007065 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007066 return( ret );
7067 }
7068 }
7069 else
7070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007074 return( ret );
7075 }
7076 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007078
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007079 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007080}
7081
7082/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007083 * Check record counters and renegotiate if they're above the limit.
7084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007086{
Andres AG2196c7f2016-12-15 17:01:16 +00007087 size_t ep_len = ssl_ep_len( ssl );
7088 int in_ctr_cmp;
7089 int out_ctr_cmp;
7090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7092 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007093 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007094 {
7095 return( 0 );
7096 }
7097
Andres AG2196c7f2016-12-15 17:01:16 +00007098 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7099 ssl->conf->renego_period + ep_len, 8 - ep_len );
7100 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
7101 ssl->conf->renego_period + ep_len, 8 - ep_len );
7102
7103 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007104 {
7105 return( 0 );
7106 }
7107
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007112
7113/*
7114 * Receive application data decrypted from the SSL layer
7115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007117{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007118 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007119 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007120
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007121 if( ssl == NULL || ssl->conf == NULL )
7122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007127 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007130 return( ret );
7131
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007132 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007136 return( ret );
7137 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007138 }
7139#endif
7140
Hanno Becker4a810fb2017-05-24 16:27:30 +01007141 /*
7142 * Check if renegotiation is necessary and/or handshake is
7143 * in process. If yes, perform/continue, and fall through
7144 * if an unexpected packet is received while the client
7145 * is waiting for the ServerHello.
7146 *
7147 * (There is no equivalent to the last condition on
7148 * the server-side as it is not treated as within
7149 * a handshake while waiting for the ClientHello
7150 * after a renegotiation request.)
7151 */
7152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007154 ret = ssl_check_ctr_renegotiate( ssl );
7155 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7156 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007159 return( ret );
7160 }
7161#endif
7162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007166 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7167 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007170 return( ret );
7171 }
7172 }
7173
7174 if( ssl->in_offt == NULL )
7175 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007176 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007177 if( ssl->f_get_timer != NULL &&
7178 ssl->f_get_timer( ssl->p_timer ) == -1 )
7179 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007180 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007181 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007182
Hanno Becker4a810fb2017-05-24 16:27:30 +01007183 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007184 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007185 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7186 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007187
Hanno Becker4a810fb2017-05-24 16:27:30 +01007188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7189 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007190 }
7191
7192 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007194 {
7195 /*
7196 * OpenSSL sends empty messages to randomize the IV
7197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007201 return( 0 );
7202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007204 return( ret );
7205 }
7206 }
7207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007211
Hanno Becker4a810fb2017-05-24 16:27:30 +01007212 /*
7213 * - For client-side, expect SERVER_HELLO_REQUEST.
7214 * - For server-side, expect CLIENT_HELLO.
7215 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7216 */
7217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007219 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007221 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007224
7225 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007226#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007227 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007228 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007229#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007231 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007232#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007233
Hanno Becker4a810fb2017-05-24 16:27:30 +01007234#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007235 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007236 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007239
7240 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007242 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007243 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007244#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007246 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007247#endif /* MBEDTLS_SSL_SRV_C */
7248
Hanno Becker21df7f92017-10-17 11:03:26 +01007249#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007250 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007251 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7252 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7253 ssl->conf->allow_legacy_renegotiation ==
7254 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7255 {
7256 /*
7257 * Accept renegotiation request
7258 */
Paul Bakker48916f92012-09-16 19:57:18 +00007259
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007260 /* DTLS clients need to know renego is server-initiated */
7261#if defined(MBEDTLS_SSL_PROTO_DTLS)
7262 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7263 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7264 {
7265 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7266 }
7267#endif
7268 ret = ssl_start_renegotiation( ssl );
7269 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7270 ret != 0 )
7271 {
7272 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7273 return( ret );
7274 }
7275 }
7276 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007277#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007278 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007279 /*
7280 * Refuse renegotiation
7281 */
7282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285#if defined(MBEDTLS_SSL_PROTO_SSL3)
7286 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007287 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007288 /* SSLv3 does not have a "no_renegotiation" warning, so
7289 we send a fatal alert and abort the connection. */
7290 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7291 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7292 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007293 }
7294 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7296#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7297 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7298 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7301 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7302 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007303 {
7304 return( ret );
7305 }
Paul Bakker48916f92012-09-16 19:57:18 +00007306 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007307 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7309 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7312 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007313 }
Paul Bakker48916f92012-09-16 19:57:18 +00007314 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007315
Hanno Becker4a810fb2017-05-24 16:27:30 +01007316 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00007317 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007318#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007320 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007321 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007322 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007323 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007326 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007328 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007329 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007330 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7334 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007337 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007338 }
7339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7343 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007344 }
7345
7346 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007347
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007348 /* We're going to return something now, cancel timer,
7349 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007351 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007352
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007353#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007354 /* If we requested renego but received AppData, resend HelloRequest.
7355 * Do it now, after setting in_offt, to avoid taking this branch
7356 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007358 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007360 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007361 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007363 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007364 return( ret );
7365 }
7366 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007368#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007369 }
7370
7371 n = ( len < ssl->in_msglen )
7372 ? len : ssl->in_msglen;
7373
7374 memcpy( buf, ssl->in_offt, n );
7375 ssl->in_msglen -= n;
7376
7377 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007378 {
7379 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007380 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007381 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007382 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007383 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007384 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007385 /* more data available */
7386 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007390
Paul Bakker23986e52011-04-24 08:57:21 +00007391 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007392}
7393
7394/*
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007395 * Send application data to be encrypted by the SSL layer, taking care of max
7396 * fragment length and buffer size.
7397 *
7398 * According to RFC 5246 Section 6.2.1:
7399 *
7400 * Zero-length fragments of Application data MAY be sent as they are
7401 * potentially useful as a traffic analysis countermeasure.
7402 *
7403 * Therefore, it is possible that the input message length is 0 and the
7404 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007405 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007406static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007407 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007408{
Paul Bakker23986e52011-04-24 08:57:21 +00007409 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007410#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007411 size_t max_len = mbedtls_ssl_get_max_frag_len( ssl );
Florin0b7b83f2017-07-22 09:01:44 +02007412#else
7413 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
7414#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007415 if( len > max_len )
7416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007418 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007421 "maximum fragment length: %d > %d",
7422 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007424 }
7425 else
7426#endif
7427 len = max_len;
7428 }
Paul Bakker887bd502011-06-08 13:10:54 +00007429
Paul Bakker5121ce52009-01-03 21:22:43 +00007430 if( ssl->out_left != 0 )
7431 {
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007432 /*
7433 * The user has previously tried to send the data and
7434 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7435 * written. In this case, we expect the high-level write function
7436 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7437 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007441 return( ret );
7442 }
7443 }
Paul Bakker887bd502011-06-08 13:10:54 +00007444 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007445 {
Andres Amaya Garcia0fc4e082017-09-28 14:41:17 +01007446 /*
7447 * The user is trying to send a message the first time, so we need to
7448 * copy the data into the internal buffers and setup the data structure
7449 * to keep track of partial writes
7450 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007451 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007453 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007458 return( ret );
7459 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007460 }
7461
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007462 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007463}
7464
7465/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007466 * Write application data, doing 1/n-1 splitting if necessary.
7467 *
7468 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007469 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01007470 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007473static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007474 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007475{
7476 int ret;
7477
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007478 if( ssl->conf->cbc_record_splitting ==
7479 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007480 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7482 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7483 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007484 {
7485 return( ssl_write_real( ssl, buf, len ) );
7486 }
7487
7488 if( ssl->split_done == 0 )
7489 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007490 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007491 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007492 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007493 }
7494
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007495 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7496 return( ret );
7497 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007498
7499 return( ret + 1 );
7500}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007502
7503/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007504 * Write application data (public-facing wrapper)
7505 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007506int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007507{
7508 int ret;
7509
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007511
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007512 if( ssl == NULL || ssl->conf == NULL )
7513 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7514
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007515#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007516 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7517 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007518 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007519 return( ret );
7520 }
7521#endif
7522
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007523 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007524 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007525 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007526 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007528 return( ret );
7529 }
7530 }
7531
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007532#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007533 ret = ssl_write_split( ssl, buf, len );
7534#else
7535 ret = ssl_write_real( ssl, buf, len );
7536#endif
7537
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007539
7540 return( ret );
7541}
7542
7543/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007544 * Notify the peer that the connection is being closed
7545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007547{
7548 int ret;
7549
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007550 if( ssl == NULL || ssl->conf == NULL )
7551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007554
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007555 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7561 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7562 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007565 return( ret );
7566 }
7567 }
7568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007570
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007571 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007572}
7573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00007575{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007576 if( transform == NULL )
7577 return;
7578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00007580 deflateEnd( &transform->ctx_deflate );
7581 inflateEnd( &transform->ctx_inflate );
7582#endif
7583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584 mbedtls_cipher_free( &transform->cipher_ctx_enc );
7585 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02007586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587 mbedtls_md_free( &transform->md_ctx_enc );
7588 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02007589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590 mbedtls_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007591}
7592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593#if defined(MBEDTLS_X509_CRT_PARSE_C)
7594static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007595{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007597
7598 while( cur != NULL )
7599 {
7600 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007602 cur = next;
7603 }
7604}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake )
Paul Bakker48916f92012-09-16 19:57:18 +00007608{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007609 if( handshake == NULL )
7610 return;
7611
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007612#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7613 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7614 mbedtls_md5_free( &handshake->fin_md5 );
7615 mbedtls_sha1_free( &handshake->fin_sha1 );
7616#endif
7617#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7618#if defined(MBEDTLS_SHA256_C)
7619 mbedtls_sha256_free( &handshake->fin_sha256 );
7620#endif
7621#if defined(MBEDTLS_SHA512_C)
7622 mbedtls_sha512_free( &handshake->fin_sha512 );
7623#endif
7624#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626#if defined(MBEDTLS_DHM_C)
7627 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007628#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629#if defined(MBEDTLS_ECDH_C)
7630 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007631#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007632#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007633 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007634#if defined(MBEDTLS_SSL_CLI_C)
7635 mbedtls_free( handshake->ecjpake_cache );
7636 handshake->ecjpake_cache = NULL;
7637 handshake->ecjpake_cache_len = 0;
7638#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007639#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02007640
Janos Follath4ae5c292016-02-10 11:27:43 +00007641#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
7642 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02007643 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007645#endif
7646
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007647#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7648 if( handshake->psk != NULL )
7649 {
7650 mbedtls_zeroize( handshake->psk, handshake->psk_len );
7651 mbedtls_free( handshake->psk );
7652 }
7653#endif
7654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7656 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007657 /*
7658 * Free only the linked list wrapper, not the keys themselves
7659 * since the belong to the SNI callback
7660 */
7661 if( handshake->sni_key_cert != NULL )
7662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007664
7665 while( cur != NULL )
7666 {
7667 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007669 cur = next;
7670 }
7671 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674#if defined(MBEDTLS_SSL_PROTO_DTLS)
7675 mbedtls_free( handshake->verify_cookie );
7676 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007677 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007678#endif
7679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007680 mbedtls_zeroize( handshake, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007681}
7682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007683void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007684{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007685 if( session == NULL )
7686 return;
7687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007688#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007689 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691 mbedtls_x509_crt_free( session->peer_cert );
7692 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007693 }
Paul Bakkered27a042013-04-18 22:46:23 +02007694#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007695
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007696#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007697 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007698#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700 mbedtls_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007701}
7702
Paul Bakker5121ce52009-01-03 21:22:43 +00007703/*
7704 * Free an SSL context
7705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007707{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007708 if( ssl == NULL )
7709 return;
7710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007712
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007713 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007715 mbedtls_zeroize( ssl->out_buf, MBEDTLS_SSL_BUFFER_LEN );
7716 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007717 }
7718
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007719 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721 mbedtls_zeroize( ssl->in_buf, MBEDTLS_SSL_BUFFER_LEN );
7722 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007723 }
7724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02007726 if( ssl->compress_buf != NULL )
7727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728 mbedtls_zeroize( ssl->compress_buf, MBEDTLS_SSL_BUFFER_LEN );
7729 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02007730 }
7731#endif
7732
Paul Bakker48916f92012-09-16 19:57:18 +00007733 if( ssl->transform )
7734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735 mbedtls_ssl_transform_free( ssl->transform );
7736 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007737 }
7738
7739 if( ssl->handshake )
7740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007741 mbedtls_ssl_handshake_free( ssl->handshake );
7742 mbedtls_ssl_transform_free( ssl->transform_negotiate );
7743 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745 mbedtls_free( ssl->handshake );
7746 mbedtls_free( ssl->transform_negotiate );
7747 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007748 }
7749
Paul Bakkerc0463502013-02-14 11:19:38 +01007750 if( ssl->session )
7751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 mbedtls_ssl_session_free( ssl->session );
7753 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007754 }
7755
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02007756#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02007757 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007758 {
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007759 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00007761 }
Paul Bakker0be444a2013-08-27 21:55:01 +02007762#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7765 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
7768 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00007769 }
7770#endif
7771
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007772#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007774#endif
7775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00007777
Paul Bakker86f04f42013-02-14 11:20:09 +01007778 /* Actually clear after last debug message */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007779 mbedtls_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007780}
7781
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007782/*
7783 * Initialze mbedtls_ssl_config
7784 */
7785void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
7786{
7787 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
7788}
7789
Simon Butcherc97b6972015-12-27 23:48:17 +00007790#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007791static int ssl_preset_default_hashes[] = {
7792#if defined(MBEDTLS_SHA512_C)
7793 MBEDTLS_MD_SHA512,
7794 MBEDTLS_MD_SHA384,
7795#endif
7796#if defined(MBEDTLS_SHA256_C)
7797 MBEDTLS_MD_SHA256,
7798 MBEDTLS_MD_SHA224,
7799#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02007800#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007801 MBEDTLS_MD_SHA1,
7802#endif
7803 MBEDTLS_MD_NONE
7804};
Simon Butcherc97b6972015-12-27 23:48:17 +00007805#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007806
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007807static int ssl_preset_suiteb_ciphersuites[] = {
7808 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
7809 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
7810 0
7811};
7812
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007813#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007814static int ssl_preset_suiteb_hashes[] = {
7815 MBEDTLS_MD_SHA256,
7816 MBEDTLS_MD_SHA384,
7817 MBEDTLS_MD_NONE
7818};
7819#endif
7820
7821#if defined(MBEDTLS_ECP_C)
7822static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007823#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007824 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007825#endif
7826#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007827 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Ameroba59f6b2019-06-03 08:27:16 +01007828#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007829 MBEDTLS_ECP_DP_NONE
7830};
7831#endif
7832
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007833/*
Tillmann Karras588ad502015-09-25 04:27:22 +02007834 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007835 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007836int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007837 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007838{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007839#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007840 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007841#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007842
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02007843 /* Use the functions here so that they are covered in tests,
7844 * but otherwise access member directly for efficiency */
7845 mbedtls_ssl_conf_endpoint( conf, endpoint );
7846 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007847
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007848 /*
7849 * Things that are common to all presets
7850 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007851#if defined(MBEDTLS_SSL_CLI_C)
7852 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7853 {
7854 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
7855#if defined(MBEDTLS_SSL_SESSION_TICKETS)
7856 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
7857#endif
7858 }
7859#endif
7860
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007861#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007862 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007863#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007864
7865#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7866 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
7867#endif
7868
7869#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7870 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
7871#endif
7872
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007873#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7874 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7875#endif
7876
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007877#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007878 conf->f_cookie_write = ssl_cookie_write_dummy;
7879 conf->f_cookie_check = ssl_cookie_check_dummy;
7880#endif
7881
7882#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7883 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7884#endif
7885
Janos Follath088ce432017-04-10 12:42:31 +01007886#if defined(MBEDTLS_SSL_SRV_C)
7887 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
7888#endif
7889
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007890#if defined(MBEDTLS_SSL_PROTO_DTLS)
7891 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7892 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7893#endif
7894
7895#if defined(MBEDTLS_SSL_RENEGOTIATION)
7896 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00007897 memset( conf->renego_period, 0x00, 2 );
7898 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007899#endif
7900
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007901#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
7902 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7903 {
Hanno Becker00d0a682017-10-04 13:14:29 +01007904 const unsigned char dhm_p[] =
7905 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
7906 const unsigned char dhm_g[] =
7907 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
7908
Hanno Beckera90658f2017-10-04 15:29:08 +01007909 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
7910 dhm_p, sizeof( dhm_p ),
7911 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007912 {
7913 return( ret );
7914 }
7915 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007916#endif
7917
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007918 /*
7919 * Preset-specific defaults
7920 */
7921 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007922 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007923 /*
7924 * NSA Suite B
7925 */
7926 case MBEDTLS_SSL_PRESET_SUITEB:
7927 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7928 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7929 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7930 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7931
7932 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7933 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7934 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7935 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7936 ssl_preset_suiteb_ciphersuites;
7937
7938#if defined(MBEDTLS_X509_CRT_PARSE_C)
7939 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007940#endif
7941
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007942#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007943 conf->sig_hashes = ssl_preset_suiteb_hashes;
7944#endif
7945
7946#if defined(MBEDTLS_ECP_C)
7947 conf->curve_list = ssl_preset_suiteb_curves;
7948#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007949 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007950
7951 /*
7952 * Default
7953 */
7954 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03007955 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
7956 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
7957 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7958 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7959 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
7960 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
7961 MBEDTLS_SSL_MIN_MINOR_VERSION :
7962 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007963 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7964 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7965
7966#if defined(MBEDTLS_SSL_PROTO_DTLS)
7967 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7968 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
7969#endif
7970
7971 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7972 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7973 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7974 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7975 mbedtls_ssl_list_ciphersuites();
7976
7977#if defined(MBEDTLS_X509_CRT_PARSE_C)
7978 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7979#endif
7980
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007981#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007982 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007983#endif
7984
7985#if defined(MBEDTLS_ECP_C)
7986 conf->curve_list = mbedtls_ecp_grp_id_list();
7987#endif
7988
7989#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7990 conf->dhm_min_bitlen = 1024;
7991#endif
7992 }
7993
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007994 return( 0 );
7995}
7996
7997/*
7998 * Free mbedtls_ssl_config
7999 */
8000void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8001{
8002#if defined(MBEDTLS_DHM_C)
8003 mbedtls_mpi_free( &conf->dhm_P );
8004 mbedtls_mpi_free( &conf->dhm_G );
8005#endif
8006
8007#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8008 if( conf->psk != NULL )
8009 {
8010 mbedtls_zeroize( conf->psk, conf->psk_len );
8011 mbedtls_zeroize( conf->psk_identity, conf->psk_identity_len );
8012 mbedtls_free( conf->psk );
8013 mbedtls_free( conf->psk_identity );
8014 conf->psk_len = 0;
8015 conf->psk_identity_len = 0;
8016 }
8017#endif
8018
8019#if defined(MBEDTLS_X509_CRT_PARSE_C)
8020 ssl_key_cert_free( conf->key_cert );
8021#endif
8022
8023 mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
8024}
8025
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008026#if defined(MBEDTLS_PK_C) && \
8027 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008028/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008029 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008031unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008032{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008033#if defined(MBEDTLS_RSA_C)
8034 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8035 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008036#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037#if defined(MBEDTLS_ECDSA_C)
8038 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8039 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008040#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008041 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008042}
8043
Hanno Becker7e5437a2017-04-28 17:15:26 +01008044unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8045{
8046 switch( type ) {
8047 case MBEDTLS_PK_RSA:
8048 return( MBEDTLS_SSL_SIG_RSA );
8049 case MBEDTLS_PK_ECDSA:
8050 case MBEDTLS_PK_ECKEY:
8051 return( MBEDTLS_SSL_SIG_ECDSA );
8052 default:
8053 return( MBEDTLS_SSL_SIG_ANON );
8054 }
8055}
8056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008057mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008058{
8059 switch( sig )
8060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061#if defined(MBEDTLS_RSA_C)
8062 case MBEDTLS_SSL_SIG_RSA:
8063 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008064#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065#if defined(MBEDTLS_ECDSA_C)
8066 case MBEDTLS_SSL_SIG_ECDSA:
8067 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008068#endif
8069 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008070 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008071 }
8072}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008073#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008074
Hanno Becker7e5437a2017-04-28 17:15:26 +01008075#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8076 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8077
8078/* Find an entry in a signature-hash set matching a given hash algorithm. */
8079mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8080 mbedtls_pk_type_t sig_alg )
8081{
8082 switch( sig_alg )
8083 {
8084 case MBEDTLS_PK_RSA:
8085 return( set->rsa );
8086 case MBEDTLS_PK_ECDSA:
8087 return( set->ecdsa );
8088 default:
8089 return( MBEDTLS_MD_NONE );
8090 }
8091}
8092
8093/* Add a signature-hash-pair to a signature-hash set */
8094void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8095 mbedtls_pk_type_t sig_alg,
8096 mbedtls_md_type_t md_alg )
8097{
8098 switch( sig_alg )
8099 {
8100 case MBEDTLS_PK_RSA:
8101 if( set->rsa == MBEDTLS_MD_NONE )
8102 set->rsa = md_alg;
8103 break;
8104
8105 case MBEDTLS_PK_ECDSA:
8106 if( set->ecdsa == MBEDTLS_MD_NONE )
8107 set->ecdsa = md_alg;
8108 break;
8109
8110 default:
8111 break;
8112 }
8113}
8114
8115/* Allow exactly one hash algorithm for each signature. */
8116void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8117 mbedtls_md_type_t md_alg )
8118{
8119 set->rsa = md_alg;
8120 set->ecdsa = md_alg;
8121}
8122
8123#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8124 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8125
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008126/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008127 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008130{
8131 switch( hash )
8132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133#if defined(MBEDTLS_MD5_C)
8134 case MBEDTLS_SSL_HASH_MD5:
8135 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008136#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137#if defined(MBEDTLS_SHA1_C)
8138 case MBEDTLS_SSL_HASH_SHA1:
8139 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008140#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141#if defined(MBEDTLS_SHA256_C)
8142 case MBEDTLS_SSL_HASH_SHA224:
8143 return( MBEDTLS_MD_SHA224 );
8144 case MBEDTLS_SSL_HASH_SHA256:
8145 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008146#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147#if defined(MBEDTLS_SHA512_C)
8148 case MBEDTLS_SSL_HASH_SHA384:
8149 return( MBEDTLS_MD_SHA384 );
8150 case MBEDTLS_SSL_HASH_SHA512:
8151 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008152#endif
8153 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008155 }
8156}
8157
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008158/*
8159 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8160 */
8161unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8162{
8163 switch( md )
8164 {
8165#if defined(MBEDTLS_MD5_C)
8166 case MBEDTLS_MD_MD5:
8167 return( MBEDTLS_SSL_HASH_MD5 );
8168#endif
8169#if defined(MBEDTLS_SHA1_C)
8170 case MBEDTLS_MD_SHA1:
8171 return( MBEDTLS_SSL_HASH_SHA1 );
8172#endif
8173#if defined(MBEDTLS_SHA256_C)
8174 case MBEDTLS_MD_SHA224:
8175 return( MBEDTLS_SSL_HASH_SHA224 );
8176 case MBEDTLS_MD_SHA256:
8177 return( MBEDTLS_SSL_HASH_SHA256 );
8178#endif
8179#if defined(MBEDTLS_SHA512_C)
8180 case MBEDTLS_MD_SHA384:
8181 return( MBEDTLS_SSL_HASH_SHA384 );
8182 case MBEDTLS_MD_SHA512:
8183 return( MBEDTLS_SSL_HASH_SHA512 );
8184#endif
8185 default:
8186 return( MBEDTLS_SSL_HASH_NONE );
8187 }
8188}
8189
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008190#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008191/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008192 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008193 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008194 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008195int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008196{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008198
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008199 if( ssl->conf->curve_list == NULL )
8200 return( -1 );
8201
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008202 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008203 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008204 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008205
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008206 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008207}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008208#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008209
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008210#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008211/*
8212 * Check if a hash proposed by the peer is in our list.
8213 * Return 0 if we're willing to use it, -1 otherwise.
8214 */
8215int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8216 mbedtls_md_type_t md )
8217{
8218 const int *cur;
8219
8220 if( ssl->conf->sig_hashes == NULL )
8221 return( -1 );
8222
8223 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8224 if( *cur == (int) md )
8225 return( 0 );
8226
8227 return( -1 );
8228}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008229#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008231#if defined(MBEDTLS_X509_CRT_PARSE_C)
8232int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8233 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008234 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008235 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008236{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008237 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008239 int usage = 0;
8240#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008241#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008242 const char *ext_oid;
8243 size_t ext_len;
8244#endif
8245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008246#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8247 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008248 ((void) cert);
8249 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008250 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008251#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8254 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008255 {
8256 /* Server part of the key exchange */
8257 switch( ciphersuite->key_exchange )
8258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008259 case MBEDTLS_KEY_EXCHANGE_RSA:
8260 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008261 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008262 break;
8263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008264 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8265 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8266 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8267 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008268 break;
8269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008270 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8271 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008272 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008273 break;
8274
8275 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276 case MBEDTLS_KEY_EXCHANGE_NONE:
8277 case MBEDTLS_KEY_EXCHANGE_PSK:
8278 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8279 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008280 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008281 usage = 0;
8282 }
8283 }
8284 else
8285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008286 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8287 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008288 }
8289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008291 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008292 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008293 ret = -1;
8294 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008295#else
8296 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008297#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8300 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8303 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008304 }
8305 else
8306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8308 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008309 }
8310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008312 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008313 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008314 ret = -1;
8315 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008317
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008318 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008319}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008321
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008322/*
8323 * Convert version numbers to/from wire format
8324 * and, for DTLS, to/from TLS equivalent.
8325 *
8326 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008327 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008328 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8329 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008332 unsigned char ver[2] )
8333{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334#if defined(MBEDTLS_SSL_PROTO_DTLS)
8335 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008338 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8339
8340 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8341 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8342 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008343 else
8344#else
8345 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008346#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008347 {
8348 ver[0] = (unsigned char) major;
8349 ver[1] = (unsigned char) minor;
8350 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008351}
8352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008354 const unsigned char ver[2] )
8355{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008356#if defined(MBEDTLS_SSL_PROTO_DTLS)
8357 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008358 {
8359 *major = 255 - ver[0] + 2;
8360 *minor = 255 - ver[1] + 1;
8361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008362 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008363 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8364 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008365 else
8366#else
8367 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008368#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008369 {
8370 *major = ver[0];
8371 *minor = ver[1];
8372 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008373}
8374
Simon Butcher99000142016-10-13 17:21:01 +01008375int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8376{
8377#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8378 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8379 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8380
8381 switch( md )
8382 {
8383#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8384#if defined(MBEDTLS_MD5_C)
8385 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008386 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008387#endif
8388#if defined(MBEDTLS_SHA1_C)
8389 case MBEDTLS_SSL_HASH_SHA1:
8390 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8391 break;
8392#endif
8393#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8394#if defined(MBEDTLS_SHA512_C)
8395 case MBEDTLS_SSL_HASH_SHA384:
8396 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8397 break;
8398#endif
8399#if defined(MBEDTLS_SHA256_C)
8400 case MBEDTLS_SSL_HASH_SHA256:
8401 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8402 break;
8403#endif
8404 default:
8405 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8406 }
8407
8408 return 0;
8409#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8410 (void) ssl;
8411 (void) md;
8412
8413 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8414#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8415}
8416
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008417#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8418 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8419int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8420 unsigned char *output,
8421 unsigned char *data, size_t data_len )
8422{
8423 int ret = 0;
8424 mbedtls_md5_context mbedtls_md5;
8425 mbedtls_sha1_context mbedtls_sha1;
8426
8427 mbedtls_md5_init( &mbedtls_md5 );
8428 mbedtls_sha1_init( &mbedtls_sha1 );
8429
8430 /*
8431 * digitally-signed struct {
8432 * opaque md5_hash[16];
8433 * opaque sha_hash[20];
8434 * };
8435 *
8436 * md5_hash
8437 * MD5(ClientHello.random + ServerHello.random
8438 * + ServerParams);
8439 * sha_hash
8440 * SHA(ClientHello.random + ServerHello.random
8441 * + ServerParams);
8442 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008443 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008444 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008446 goto exit;
8447 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008448 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008449 ssl->handshake->randbytes, 64 ) ) != 0 )
8450 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008452 goto exit;
8453 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008454 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008455 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008457 goto exit;
8458 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008459 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008460 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008462 goto exit;
8463 }
8464
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008465 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008466 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008468 goto exit;
8469 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008470 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008471 ssl->handshake->randbytes, 64 ) ) != 0 )
8472 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008474 goto exit;
8475 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008476 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008477 data_len ) ) != 0 )
8478 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008479 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008480 goto exit;
8481 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008482 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008483 output + 16 ) ) != 0 )
8484 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008486 goto exit;
8487 }
8488
8489exit:
8490 mbedtls_md5_free( &mbedtls_md5 );
8491 mbedtls_sha1_free( &mbedtls_sha1 );
8492
8493 if( ret != 0 )
8494 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8495 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8496
8497 return( ret );
8498
8499}
8500#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
8501 MBEDTLS_SSL_PROTO_TLS1_1 */
8502
8503#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8504 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8505int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8506 unsigned char *output,
8507 unsigned char *data, size_t data_len,
8508 mbedtls_md_type_t md_alg )
8509{
8510 int ret = 0;
8511 mbedtls_md_context_t ctx;
8512 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8513
8514 mbedtls_md_init( &ctx );
8515
8516 /*
8517 * digitally-signed struct {
8518 * opaque client_random[32];
8519 * opaque server_random[32];
8520 * ServerDHParams params;
8521 * };
8522 */
8523 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8524 {
8525 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8526 goto exit;
8527 }
8528 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8529 {
8530 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8531 goto exit;
8532 }
8533 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8534 {
8535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8536 goto exit;
8537 }
8538 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8539 {
8540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8541 goto exit;
8542 }
8543 if( ( ret = mbedtls_md_finish( &ctx, output ) ) != 0 )
8544 {
8545 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8546 goto exit;
8547 }
8548
8549exit:
8550 mbedtls_md_free( &ctx );
8551
8552 if( ret != 0 )
8553 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8554 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8555
8556 return( ret );
8557}
8558#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
8559 MBEDTLS_SSL_PROTO_TLS1_2 */
8560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561#endif /* MBEDTLS_SSL_TLS_C */