blob: faa9467e106731b39b4d72940f50830e48855024 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Janos Follath23bdca02016-10-07 14:47:14 +010053#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020055#endif
56
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010057/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020061 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010062 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010063#else
64 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065#endif
66 return( 0 );
67}
68
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020069/*
70 * Start a timer.
71 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020074{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020075 if( ssl->f_set_timer == NULL )
76 return;
77
78 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
79 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020080}
81
82/*
83 * Return -1 is timer is expired, 0 if it isn't.
84 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020088 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020089
90 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020091 {
92 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020093 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020094 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095
96 return( 0 );
97}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020099#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200100/*
101 * Double the retransmit timeout value, within the allowed range,
102 * returning -1 if the maximum value has already been reached.
103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200105{
106 uint32_t new_timeout;
107
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200108 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200109 return( -1 );
110
111 new_timeout = 2 * ssl->handshake->retransmit_timeout;
112
113 /* Avoid arithmetic overflow and range overflow */
114 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200115 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200116 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200117 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200118 }
119
120 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200122 ssl->handshake->retransmit_timeout ) );
123
124 return( 0 );
125}
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200128{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200129 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200131 ssl->handshake->retransmit_timeout ) );
132}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200136/*
137 * Convert max_fragment_length codes to length.
138 * RFC 6066 says:
139 * enum{
140 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
141 * } MaxFragmentLength;
142 * and we add 0 -> extension unused
143 */
Angus Grattond8213d02016-05-25 20:56:48 +1000144static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200145{
Angus Grattond8213d02016-05-25 20:56:48 +1000146 switch( mfl )
147 {
148 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
149 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
150 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
151 return 512;
152 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
153 return 1024;
154 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
155 return 2048;
156 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
157 return 4096;
158 default:
159 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
160 }
161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200163
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200164#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200166{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_ssl_session_free( dst );
168 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200171 if( src->peer_cert != NULL )
172 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200173 int ret;
174
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200175 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200176 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200177 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200182 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200185 dst->peer_cert = NULL;
186 return( ret );
187 }
188 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200190
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200191#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200192 if( src->ticket != NULL )
193 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200194 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200195 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200196 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200197
198 memcpy( dst->ticket, src->ticket, src->ticket_len );
199 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200200#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200201
202 return( 0 );
203}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200204#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
207int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200208 const unsigned char *key_enc, const unsigned char *key_dec,
209 size_t keylen,
210 const unsigned char *iv_enc, const unsigned char *iv_dec,
211 size_t ivlen,
212 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200213 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
215int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
216int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
217int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
218int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
219#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000220
Paul Bakker5121ce52009-01-03 21:22:43 +0000221/*
222 * Key material generation
223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200225static int ssl3_prf( const unsigned char *secret, size_t slen,
226 const char *label,
227 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000228 unsigned char *dstbuf, size_t dlen )
229{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100230 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000231 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200232 mbedtls_md5_context md5;
233 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000234 unsigned char padding[16];
235 unsigned char sha1sum[20];
236 ((void)label);
237
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200238 mbedtls_md5_init( &md5 );
239 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200240
Paul Bakker5f70b252012-09-13 14:23:06 +0000241 /*
242 * SSLv3:
243 * block =
244 * MD5( secret + SHA1( 'A' + secret + random ) ) +
245 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
246 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
247 * ...
248 */
249 for( i = 0; i < dlen / 16; i++ )
250 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200251 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000252
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100253 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100254 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100255 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100256 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100257 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100258 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100259 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100260 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100261 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100262 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000263
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100264 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100265 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100266 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100267 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100268 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100269 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100270 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100271 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000272 }
273
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100274exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200275 mbedtls_md5_free( &md5 );
276 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000277
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500278 mbedtls_platform_zeroize( padding, sizeof( padding ) );
279 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000280
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100281 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000282}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200286static int tls1_prf( const unsigned char *secret, size_t slen,
287 const char *label,
288 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000289 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000290{
Paul Bakker23986e52011-04-24 08:57:21 +0000291 size_t nb, hs;
292 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200293 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 unsigned char tmp[128];
295 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 const mbedtls_md_info_t *md_info;
297 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100298 int ret;
299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
302 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
305 hs = ( slen + 1 ) / 2;
306 S1 = secret;
307 S2 = secret + slen - hs;
308
309 nb = strlen( label );
310 memcpy( tmp + 20, label, nb );
311 memcpy( tmp + 20 + nb, random, rlen );
312 nb += rlen;
313
314 /*
315 * First compute P_md5(secret,label+random)[0..dlen]
316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
318 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100321 return( ret );
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
324 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
325 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
327 for( i = 0; i < dlen; i += 16 )
328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_md_hmac_reset ( &md_ctx );
330 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
331 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_md_hmac_reset ( &md_ctx );
334 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
335 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
338
339 for( j = 0; j < k; j++ )
340 dstbuf[i + j] = h_i[j];
341 }
342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100344
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 /*
346 * XOR out with P_sha1(secret,label+random)[0..dlen]
347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
349 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100352 return( ret );
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
355 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
356 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000357
358 for( i = 0; i < dlen; i += 20 )
359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_md_hmac_reset ( &md_ctx );
361 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
362 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_md_hmac_reset ( &md_ctx );
365 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
366 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000367
368 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
369
370 for( j = 0; j < k; j++ )
371 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
372 }
373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100375
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500376 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
377 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000378
379 return( 0 );
380}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
384static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100385 const unsigned char *secret, size_t slen,
386 const char *label,
387 const unsigned char *random, size_t rlen,
388 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000389{
390 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100391 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000392 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
394 const mbedtls_md_info_t *md_info;
395 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100396 int ret;
397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
401 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100404
405 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000407
408 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100409 memcpy( tmp + md_len, label, nb );
410 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000411 nb += rlen;
412
413 /*
414 * Compute P_<hash>(secret, label + random)[0..dlen]
415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100417 return( ret );
418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
420 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
421 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100422
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100423 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_md_hmac_reset ( &md_ctx );
426 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
427 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_md_hmac_reset ( &md_ctx );
430 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
431 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000432
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100433 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000434
435 for( j = 0; j < k; j++ )
436 dstbuf[i + j] = h_i[j];
437 }
438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100440
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500441 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
442 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000443
444 return( 0 );
445}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100448static int tls_prf_sha256( const unsigned char *secret, size_t slen,
449 const char *label,
450 const unsigned char *random, size_t rlen,
451 unsigned char *dstbuf, size_t dlen )
452{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100454 label, random, rlen, dstbuf, dlen ) );
455}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200459static int tls_prf_sha384( const unsigned char *secret, size_t slen,
460 const char *label,
461 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000462 unsigned char *dstbuf, size_t dlen )
463{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100465 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000466}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#endif /* MBEDTLS_SHA512_C */
468#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
473 defined(MBEDTLS_SSL_PROTO_TLS1_1)
474static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200475#endif
Paul Bakker380da532012-04-18 16:10:25 +0000476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#if defined(MBEDTLS_SSL_PROTO_SSL3)
478static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
479static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200480#endif
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
483static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
484static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200485#endif
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
488#if defined(MBEDTLS_SHA256_C)
489static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
490static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
491static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200492#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#if defined(MBEDTLS_SHA512_C)
495static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
496static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
497static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100498#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000502{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200503 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 unsigned char keyblk[256];
506 unsigned char *key1;
507 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100508 unsigned char *mac_enc;
509 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000510 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200511 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 const mbedtls_cipher_info_t *cipher_info;
513 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_ssl_session *session = ssl->session_negotiate;
516 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
517 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100522 if( cipher_info == NULL )
523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100525 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100527 }
528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100530 if( md_info == NULL )
531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100533 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100535 }
536
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000538 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#if defined(MBEDTLS_SSL_PROTO_SSL3)
541 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000542 {
Paul Bakker48916f92012-09-16 19:57:18 +0000543 handshake->tls_prf = ssl3_prf;
544 handshake->calc_verify = ssl_calc_verify_ssl;
545 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000546 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200547 else
548#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
550 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000551 {
Paul Bakker48916f92012-09-16 19:57:18 +0000552 handshake->tls_prf = tls1_prf;
553 handshake->calc_verify = ssl_calc_verify_tls;
554 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000555 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200556 else
557#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
559#if defined(MBEDTLS_SHA512_C)
560 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
561 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000562 {
Paul Bakker48916f92012-09-16 19:57:18 +0000563 handshake->tls_prf = tls_prf_sha384;
564 handshake->calc_verify = ssl_calc_verify_tls_sha384;
565 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000566 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000567 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200568#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#if defined(MBEDTLS_SHA256_C)
570 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000571 {
Paul Bakker48916f92012-09-16 19:57:18 +0000572 handshake->tls_prf = tls_prf_sha256;
573 handshake->calc_verify = ssl_calc_verify_tls_sha256;
574 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000575 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200576 else
577#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
581 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200582 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000583
584 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 * SSLv3:
586 * master =
587 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
588 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
589 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200590 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200591 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 * master = PRF( premaster, "master secret", randbytes )[0..47]
593 */
Paul Bakker0a597072012-09-25 21:55:46 +0000594 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000597 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
600 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200601 {
602 unsigned char session_hash[48];
603 size_t hash_len;
604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200606
607 ssl->handshake->calc_verify( ssl, session_hash );
608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
610 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200613 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200615 {
616 hash_len = 48;
617 }
618 else
619#endif
620 hash_len = 32;
621 }
622 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200624 hash_len = 36;
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200627
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100628 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
629 "extended master secret",
630 session_hash, hash_len,
631 session->master, 48 );
632 if( ret != 0 )
633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100635 return( ret );
636 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200637
638 }
639 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200640#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100641 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
642 "master secret",
643 handshake->randbytes, 64,
644 session->master, 48 );
645 if( ret != 0 )
646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100648 return( ret );
649 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200650
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500651 mbedtls_platform_zeroize( handshake->premaster,
652 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 }
654 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
657 /*
658 * Swap the client and server random values.
659 */
Paul Bakker48916f92012-09-16 19:57:18 +0000660 memcpy( tmp, handshake->randbytes, 64 );
661 memcpy( handshake->randbytes, tmp + 32, 32 );
662 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500663 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000664
665 /*
666 * SSLv3:
667 * key block =
668 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
669 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
670 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
671 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
672 * ...
673 *
674 * TLSv1:
675 * key block = PRF( master, "key expansion", randbytes )
676 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100677 ret = handshake->tls_prf( session->master, 48, "key expansion",
678 handshake->randbytes, 64, keyblk, 256 );
679 if( ret != 0 )
680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100682 return( ret );
683 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
686 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
687 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
688 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
689 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000690
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500691 mbedtls_platform_zeroize( handshake->randbytes,
692 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000693
694 /*
695 * Determine the appropriate key, IV and MAC length.
696 */
Paul Bakker68884e32013-01-07 18:20:04 +0100697
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200698 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200701 cipher_info->mode == MBEDTLS_MODE_CCM ||
702 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000703 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200704 size_t taglen, explicit_ivlen;
705
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200706 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000707 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200708
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200709 /* All modes haves 96-bit IVs;
710 * GCM and CCM has 4 implicit and 8 explicit bytes
711 * ChachaPoly has all 12 bytes implicit
712 */
Paul Bakker68884e32013-01-07 18:20:04 +0100713 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200714 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
715 transform->fixed_ivlen = 12;
716 else
717 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200718
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200719 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
720 taglen = transform->ciphersuite_info->flags &
721 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
722
723
724 /* Minimum length of encrypted record */
725 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
726 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100727 }
728 else
729 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200730 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
732 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200735 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100736 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000737
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200738 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000739 mac_key_len = mbedtls_md_get_size( md_info );
740 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200743 /*
744 * If HMAC is to be truncated, we shall keep the leftmost bytes,
745 * (rfc 6066 page 13 or rfc 2104 section 4),
746 * so we only need to adjust the length here.
747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000751
752#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
753 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000754 * HMAC implementation which also truncates the key
755 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000756 mac_key_len = transform->maclen;
757#endif
758 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200760
761 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100762 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000763
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200764 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200766 transform->minlen = transform->maclen;
767 else
Paul Bakker68884e32013-01-07 18:20:04 +0100768 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200769 /*
770 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100771 * 1. if EtM is in use: one block plus MAC
772 * otherwise: * first multiple of blocklen greater than maclen
773 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
776 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100777 {
778 transform->minlen = transform->maclen
779 + cipher_info->block_size;
780 }
781 else
782#endif
783 {
784 transform->minlen = transform->maclen
785 + cipher_info->block_size
786 - transform->maclen % cipher_info->block_size;
787 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
790 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
791 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200792 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100793 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200794#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
796 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
797 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200798 {
799 transform->minlen += transform->ivlen;
800 }
801 else
802#endif
803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
805 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200806 }
Paul Bakker68884e32013-01-07 18:20:04 +0100807 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000808 }
809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000811 transform->keylen, transform->minlen, transform->ivlen,
812 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000813
814 /*
815 * Finally setup the cipher contexts, IVs and MAC secrets.
816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200818 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000819 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000820 key1 = keyblk + mac_key_len * 2;
821 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000822
Paul Bakker68884e32013-01-07 18:20:04 +0100823 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000824 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000825
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000826 /*
827 * This is not used in TLS v1.1.
828 */
Paul Bakker48916f92012-09-16 19:57:18 +0000829 iv_copy_len = ( transform->fixed_ivlen ) ?
830 transform->fixed_ivlen : transform->ivlen;
831 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
832 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000833 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000834 }
835 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#endif /* MBEDTLS_SSL_CLI_C */
837#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200838 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000839 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000840 key1 = keyblk + mac_key_len * 2 + transform->keylen;
841 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000842
Hanno Becker81c7b182017-11-09 18:39:33 +0000843 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100844 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000846 /*
847 * This is not used in TLS v1.1.
848 */
Paul Bakker48916f92012-09-16 19:57:18 +0000849 iv_copy_len = ( transform->fixed_ivlen ) ?
850 transform->fixed_ivlen : transform->ivlen;
851 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
852 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000853 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000854 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100855 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
859 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100860 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#if defined(MBEDTLS_SSL_PROTO_SSL3)
863 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100864 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000865 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
868 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100869 }
870
Hanno Becker81c7b182017-11-09 18:39:33 +0000871 memcpy( transform->mac_enc, mac_enc, mac_key_len );
872 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100873 }
874 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#endif /* MBEDTLS_SSL_PROTO_SSL3 */
876#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
877 defined(MBEDTLS_SSL_PROTO_TLS1_2)
878 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100879 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100880 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
881 For AEAD-based ciphersuites, there is nothing to do here. */
882 if( mac_key_len != 0 )
883 {
884 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
885 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
886 }
Paul Bakker68884e32013-01-07 18:20:04 +0100887 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200888 else
889#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
892 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200893 }
Paul Bakker68884e32013-01-07 18:20:04 +0100894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
896 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000897 {
898 int ret = 0;
899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100903 transform->iv_enc, transform->iv_dec,
904 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100905 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +0000906 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
909 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000910 }
911 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000913
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200914#if defined(MBEDTLS_SSL_EXPORT_KEYS)
915 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100916 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200917 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
918 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +0000919 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100920 iv_copy_len );
921 }
922#endif
923
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200924 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200925 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000926 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200928 return( ret );
929 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200930
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200931 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200932 cipher_info ) ) != 0 )
933 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200934 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200935 return( ret );
936 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200939 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200943 return( ret );
944 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200947 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200951 return( ret );
952 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954#if defined(MBEDTLS_CIPHER_MODE_CBC)
955 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
958 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200961 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200962 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
965 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200968 return( ret );
969 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000970 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000972
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500973 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +0000976 // Initialize compression
977 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000979 {
Paul Bakker16770332013-10-11 09:59:44 +0200980 if( ssl->compress_buf == NULL )
981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +1000983 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +0200984 if( ssl->compress_buf == NULL )
985 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +1000987 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200988 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +0200989 }
990 }
991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000993
Paul Bakker48916f92012-09-16 19:57:18 +0000994 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
995 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000996
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200997 if( deflateInit( &transform->ctx_deflate,
998 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000999 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1002 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001003 }
1004 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001008
1009 return( 0 );
1010}
1011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#if defined(MBEDTLS_SSL_PROTO_SSL3)
1013void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001014{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001015 mbedtls_md5_context md5;
1016 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001017 unsigned char pad_1[48];
1018 unsigned char pad_2[48];
1019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001022 mbedtls_md5_init( &md5 );
1023 mbedtls_sha1_init( &sha1 );
1024
1025 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1026 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001027
Paul Bakker380da532012-04-18 16:10:25 +00001028 memset( pad_1, 0x36, 48 );
1029 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001031 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1032 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1033 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001034
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001035 mbedtls_md5_starts_ret( &md5 );
1036 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1037 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1038 mbedtls_md5_update_ret( &md5, hash, 16 );
1039 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001041 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1042 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1043 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001044
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001045 mbedtls_sha1_starts_ret( &sha1 );
1046 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1047 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1048 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1049 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001053
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001054 mbedtls_md5_free( &md5 );
1055 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001056
Paul Bakker380da532012-04-18 16:10:25 +00001057 return;
1058}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1062void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001063{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001064 mbedtls_md5_context md5;
1065 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001068
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001069 mbedtls_md5_init( &md5 );
1070 mbedtls_sha1_init( &sha1 );
1071
1072 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1073 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001074
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001075 mbedtls_md5_finish_ret( &md5, hash );
1076 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001080
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001081 mbedtls_md5_free( &md5 );
1082 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001083
Paul Bakker380da532012-04-18 16:10:25 +00001084 return;
1085}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1089#if defined(MBEDTLS_SHA256_C)
1090void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001091{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001092 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001093
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001094 mbedtls_sha256_init( &sha256 );
1095
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001097
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001098 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001099 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001103
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001104 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001105
Paul Bakker380da532012-04-18 16:10:25 +00001106 return;
1107}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110#if defined(MBEDTLS_SHA512_C)
1111void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001112{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001113 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001114
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001115 mbedtls_sha512_init( &sha512 );
1116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001118
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001119 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001120 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001125 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001126
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 return;
1128}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#endif /* MBEDTLS_SHA512_C */
1130#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1133int 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 +02001134{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001135 unsigned char *p = ssl->handshake->premaster;
1136 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001137 const unsigned char *psk = ssl->conf->psk;
1138 size_t psk_len = ssl->conf->psk_len;
1139
1140 /* If the psk callback was called, use its result */
1141 if( ssl->handshake->psk != NULL )
1142 {
1143 psk = ssl->handshake->psk;
1144 psk_len = ssl->handshake->psk_len;
1145 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001146
1147 /*
1148 * PMS = struct {
1149 * opaque other_secret<0..2^16-1>;
1150 * opaque psk<0..2^16-1>;
1151 * };
1152 * with "other_secret" depending on the particular key exchange
1153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1155 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001156 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001157 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001159
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001160 *(p++) = (unsigned char)( psk_len >> 8 );
1161 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001162
1163 if( end < p || (size_t)( end - p ) < psk_len )
1164 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1165
1166 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001167 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001168 }
1169 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1171#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1172 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001173 {
1174 /*
1175 * other_secret already set by the ClientKeyExchange message,
1176 * and is 48 bytes long
1177 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001178 if( end - p < 2 )
1179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1180
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001181 *p++ = 0;
1182 *p++ = 48;
1183 p += 48;
1184 }
1185 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1187#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1188 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001189 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001190 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001191 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001192
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001193 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001195 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001196 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001199 return( ret );
1200 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001201 *(p++) = (unsigned char)( len >> 8 );
1202 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001203 p += len;
1204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001206 }
1207 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1209#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1210 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001211 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001212 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001213 size_t zlen;
1214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001216 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001217 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001220 return( ret );
1221 }
1222
1223 *(p++) = (unsigned char)( zlen >> 8 );
1224 *(p++) = (unsigned char)( zlen );
1225 p += zlen;
1226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001228 }
1229 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1233 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001234 }
1235
1236 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001237 if( end - p < 2 )
1238 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001239
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001240 *(p++) = (unsigned char)( psk_len >> 8 );
1241 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001242
1243 if( end < p || (size_t)( end - p ) < psk_len )
1244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1245
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001246 memcpy( p, psk, psk_len );
1247 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001248
1249 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1250
1251 return( 0 );
1252}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001256/*
1257 * SSLv3.0 MAC functions
1258 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001259#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001260static void ssl_mac( mbedtls_md_context_t *md_ctx,
1261 const unsigned char *secret,
1262 const unsigned char *buf, size_t len,
1263 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001264 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001265{
1266 unsigned char header[11];
1267 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001268 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1270 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001271
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001272 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001274 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001275 else
Paul Bakker68884e32013-01-07 18:20:04 +01001276 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001277
1278 memcpy( header, ctr, 8 );
1279 header[ 8] = (unsigned char) type;
1280 header[ 9] = (unsigned char)( len >> 8 );
1281 header[10] = (unsigned char)( len );
1282
Paul Bakker68884e32013-01-07 18:20:04 +01001283 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 mbedtls_md_starts( md_ctx );
1285 mbedtls_md_update( md_ctx, secret, md_size );
1286 mbedtls_md_update( md_ctx, padding, padlen );
1287 mbedtls_md_update( md_ctx, header, 11 );
1288 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001289 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001290
Paul Bakker68884e32013-01-07 18:20:04 +01001291 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 mbedtls_md_starts( md_ctx );
1293 mbedtls_md_update( md_ctx, secret, md_size );
1294 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001295 mbedtls_md_update( md_ctx, out, md_size );
1296 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1301 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001302 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001303#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001304#endif
1305
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001306/* The function below is only used in the Lucky 13 counter-measure in
1307 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1308#if defined(SSL_SOME_MODES_USE_MAC) && \
1309 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1310 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1311 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1312/* This function makes sure every byte in the memory region is accessed
1313 * (in ascending addresses order) */
1314static void ssl_read_memory( unsigned char *p, size_t len )
1315{
1316 unsigned char acc = 0;
1317 volatile unsigned char force;
1318
1319 for( ; len != 0; p++, len-- )
1320 acc ^= *p;
1321
1322 force = acc;
1323 (void) force;
1324}
1325#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1326
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001327/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001331{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001333 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001337 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1340 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001341 }
1342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001346 ssl->out_msg, ssl->out_msglen );
1347
Angus Grattond8213d02016-05-25 20:56:48 +10001348 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
Hanno Beckerd33f1ca2017-09-18 10:55:31 +01001349 {
Hanno Becker184f6752017-10-04 13:47:33 +01001350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1351 (unsigned) ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10001352 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Beckerd33f1ca2017-09-18 10:55:31 +01001353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1354 }
1355
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001357 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001359#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 if( mode == MBEDTLS_MODE_STREAM ||
1361 ( mode == MBEDTLS_MODE_CBC
1362#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1363 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001364#endif
1365 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_SSL_PROTO_SSL3)
1368 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001369 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001370 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001371
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001372 ssl_mac( &ssl->transform_out->md_ctx_enc,
1373 ssl->transform_out->mac_enc,
1374 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001375 ssl->out_ctr, ssl->out_msgtype,
1376 mac );
1377
1378 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001379 }
1380 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001381#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1383 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1384 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001385 {
Hanno Becker992b6872017-11-09 18:57:39 +00001386 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1389 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1390 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1391 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001392 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001393 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001395
1396 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001397 }
1398 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001399#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1402 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001403 }
1404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001406 ssl->out_msg + ssl->out_msglen,
1407 ssl->transform_out->maclen );
1408
1409 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001410 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001411 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001412#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001413
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001414 /*
1415 * Encrypt
1416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1418 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001420 int ret;
1421 size_t olen = 0;
1422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 "including %d bytes of padding",
1425 ssl->out_msglen, 0 ) );
1426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001428 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001429 ssl->transform_out->ivlen,
1430 ssl->out_msg, ssl->out_msglen,
1431 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001434 return( ret );
1435 }
1436
1437 if( ssl->out_msglen != olen )
1438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1440 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001441 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 }
Paul Bakker68884e32013-01-07 18:20:04 +01001443 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001445#if defined(MBEDTLS_GCM_C) || \
1446 defined(MBEDTLS_CCM_C) || \
1447 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001449 mode == MBEDTLS_MODE_CCM ||
1450 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001451 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001452 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001453 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001454 unsigned char *enc_msg;
1455 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001456 unsigned char iv[12];
1457 mbedtls_ssl_transform *transform = ssl->transform_out;
1458 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001460 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001461
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001462 /*
1463 * Prepare additional authenticated data
1464 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001465 memcpy( add_data, ssl->out_ctr, 8 );
1466 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001468 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001469 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1470 add_data[12] = ssl->out_msglen & 0xFF;
1471
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001472 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001473
Paul Bakker68884e32013-01-07 18:20:04 +01001474 /*
1475 * Generate IV
1476 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001477 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1478 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001479 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001480 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1481 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1482 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1483
1484 }
1485 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1486 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001487 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001488 unsigned char i;
1489
1490 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1491
1492 for( i = 0; i < 8; i++ )
1493 iv[i+4] ^= ssl->out_ctr[i];
1494 }
1495 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001496 {
1497 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1499 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001500 }
1501
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001502 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1503 iv, transform->ivlen );
1504 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1505 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001506
Paul Bakker68884e32013-01-07 18:20:04 +01001507 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001508 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001509 */
1510 enc_msg = ssl->out_msg;
1511 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001512 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001515 "including 0 bytes of padding",
1516 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001517
Paul Bakker68884e32013-01-07 18:20:04 +01001518 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001519 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001520 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001521 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1522 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001523 add_data, 13,
1524 enc_msg, enc_msglen,
1525 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001526 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001529 return( ret );
1530 }
1531
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001532 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001536 }
1537
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001538 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001539 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001543 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1545#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001546 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001548 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001549 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001550 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001551 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001552
Paul Bakker48916f92012-09-16 19:57:18 +00001553 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1554 ssl->transform_out->ivlen;
1555 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001556 padlen = 0;
1557
1558 for( i = 0; i <= padlen; i++ )
1559 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1560
1561 ssl->out_msglen += padlen + 1;
1562
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001563 enc_msglen = ssl->out_msglen;
1564 enc_msg = ssl->out_msg;
1565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001567 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001568 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1569 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001572 {
1573 /*
1574 * Generate IV
1575 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001576 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001577 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001578 if( ret != 0 )
1579 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001580
Paul Bakker92be97b2013-01-02 17:30:03 +01001581 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001582 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001583
1584 /*
1585 * Fix pointer positions and message length with added IV
1586 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001587 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001588 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001589 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001590 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001594 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001595 ssl->out_msglen, ssl->transform_out->ivlen,
1596 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001599 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001600 ssl->transform_out->ivlen,
1601 enc_msg, enc_msglen,
1602 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001605 return( ret );
1606 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001607
Paul Bakkercca5b812013-08-31 17:40:26 +02001608 if( enc_msglen != olen )
1609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1611 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001612 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1615 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001616 {
1617 /*
1618 * Save IV in SSL3 and TLS1
1619 */
1620 memcpy( ssl->transform_out->iv_enc,
1621 ssl->transform_out->cipher_ctx_enc.iv,
1622 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001623 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001624#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001627 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001628 {
1629 /*
1630 * MAC(MAC_write_key, seq_num +
1631 * TLSCipherText.type +
1632 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001633 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001634 * IV + // except for TLS 1.0
1635 * ENC(content + padding + padding_length));
1636 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001637 unsigned char pseudo_hdr[13];
1638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001640
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001641 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1642 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001643 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1644 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1649 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001650 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001652 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001654
1655 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001656 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001657 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001659 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001660 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001662 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1665 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001666 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001668 /* Make extra sure authentication was performed, exactly once */
1669 if( auth_done != 1 )
1670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1672 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001673 }
1674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
1677 return( 0 );
1678}
1679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001681{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001683 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001684#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001685 size_t padlen = 0, correct = 1;
1686#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001689
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001690 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1693 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001694 }
1695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001697
Paul Bakker48916f92012-09-16 19:57:18 +00001698 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001701 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001703 }
1704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1706 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001707 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001708 int ret;
1709 size_t olen = 0;
1710
Paul Bakker68884e32013-01-07 18:20:04 +01001711 padlen = 0;
1712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001714 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001715 ssl->transform_in->ivlen,
1716 ssl->in_msg, ssl->in_msglen,
1717 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001720 return( ret );
1721 }
1722
1723 if( ssl->in_msglen != olen )
1724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1726 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001727 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001728 }
Paul Bakker68884e32013-01-07 18:20:04 +01001729 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001731#if defined(MBEDTLS_GCM_C) || \
1732 defined(MBEDTLS_CCM_C) || \
1733 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001735 mode == MBEDTLS_MODE_CCM ||
1736 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001737 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001738 int ret;
1739 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001740 unsigned char *dec_msg;
1741 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001742 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001743 unsigned char iv[12];
1744 mbedtls_ssl_transform *transform = ssl->transform_in;
1745 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001747 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001748
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001749 /*
1750 * Compute and update sizes
1751 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001752 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001755 "+ taglen (%d)", ssl->in_msglen,
1756 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001758 }
1759 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1760
Paul Bakker68884e32013-01-07 18:20:04 +01001761 dec_msg = ssl->in_msg;
1762 dec_msg_result = ssl->in_msg;
1763 ssl->in_msglen = dec_msglen;
1764
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001765 /*
1766 * Prepare additional authenticated data
1767 */
Paul Bakker68884e32013-01-07 18:20:04 +01001768 memcpy( add_data, ssl->in_ctr, 8 );
1769 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001771 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001772 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1773 add_data[12] = ssl->in_msglen & 0xFF;
1774
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001775 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001776
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001777 /*
1778 * Prepare IV
1779 */
1780 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1781 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001782 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001783 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1784 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001785
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001786 }
1787 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1788 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001789 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001790 unsigned char i;
1791
1792 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1793
1794 for( i = 0; i < 8; i++ )
1795 iv[i+4] ^= ssl->in_ctr[i];
1796 }
1797 else
1798 {
1799 /* Reminder if we ever add an AEAD mode with a different size */
1800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1801 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1802 }
1803
1804 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001806
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001807 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001808 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001811 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001812 add_data, 13,
1813 dec_msg, dec_msglen,
1814 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001815 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1820 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001821
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001822 return( ret );
1823 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001824 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001825
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001826 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1829 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001830 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001831 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001832 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1834#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001835 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 {
Paul Bakker45829992013-01-03 14:52:21 +01001838 /*
1839 * Decrypt and check the padding
1840 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001841 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001842 unsigned char *dec_msg;
1843 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001844 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001845 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001846 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001847
Paul Bakker5121ce52009-01-03 21:22:43 +00001848 /*
Paul Bakker45829992013-01-03 14:52:21 +01001849 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1852 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001853 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001854#endif
Paul Bakker45829992013-01-03 14:52:21 +01001855
1856 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1857 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001860 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1861 ssl->transform_in->ivlen,
1862 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001864 }
1865
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001866 dec_msglen = ssl->in_msglen;
1867 dec_msg = ssl->in_msg;
1868 dec_msg_result = ssl->in_msg;
1869
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001870 /*
1871 * Authenticate before decrypt if enabled
1872 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1874 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001875 {
Hanno Becker992b6872017-11-09 18:57:39 +00001876 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001877 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001880
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001881 dec_msglen -= ssl->transform_in->maclen;
1882 ssl->in_msglen -= ssl->transform_in->maclen;
1883
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001884 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1885 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1886 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1887 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1892 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001893 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001894 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001898 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001899 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001900 ssl->transform_in->maclen );
1901
Hanno Becker992b6872017-11-09 18:57:39 +00001902 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1903 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001908 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001909 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001910 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001912
1913 /*
1914 * Check length sanity
1915 */
1916 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001919 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001921 }
1922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001924 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001925 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001928 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001929 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00001930 dec_msglen -= ssl->transform_in->ivlen;
1931 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001932
Paul Bakker48916f92012-09-16 19:57:18 +00001933 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001934 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001935 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001939 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001940 ssl->transform_in->ivlen,
1941 dec_msg, dec_msglen,
1942 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001945 return( ret );
1946 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001947
Paul Bakkercca5b812013-08-31 17:40:26 +02001948 if( dec_msglen != olen )
1949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1951 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001952 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1955 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001956 {
1957 /*
1958 * Save IV in SSL3 and TLS1
1959 */
1960 memcpy( ssl->transform_in->iv_dec,
1961 ssl->transform_in->cipher_ctx_dec.iv,
1962 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001964#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001965
1966 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001967
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001968 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001969 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971#if defined(MBEDTLS_SSL_DEBUG_ALL)
1972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01001973 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001974#endif
Paul Bakker45829992013-01-03 14:52:21 +01001975 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001976 correct = 0;
1977 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979#if defined(MBEDTLS_SSL_PROTO_SSL3)
1980 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001981 {
Paul Bakker48916f92012-09-16 19:57:18 +00001982 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984#if defined(MBEDTLS_SSL_DEBUG_ALL)
1985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001986 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001987 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001988#endif
Paul Bakker45829992013-01-03 14:52:21 +01001989 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001990 }
1991 }
1992 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1994#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1995 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1996 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001997 {
1998 /*
Paul Bakker45829992013-01-03 14:52:21 +01001999 * TLSv1+: always check the padding up to the first failure
2000 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002002 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002003 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002004 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002005
Paul Bakker956c9e02013-12-19 14:42:28 +01002006 /*
2007 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002008 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002009 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002010 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002011 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002012 *
2013 * In both cases we reset padding_idx to a safe value (0) to
2014 * prevent out-of-buffer reads.
2015 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002016 correct &= ( padlen <= ssl->in_msglen );
2017 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002018 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002019
2020 padding_idx *= correct;
2021
Angus Grattonb512bc12018-06-19 15:57:50 +10002022 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002023 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002024 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002025 pad_count += real_count *
2026 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2027 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002028
2029 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002032 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002034#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002035 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002037 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2039 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2042 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002043 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002044
2045 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002047 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002049 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2052 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002053 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002054
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002055#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002059
2060 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002061 * Authenticate if not done yet.
2062 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002064#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002065 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002066 {
Hanno Becker992b6872017-11-09 18:57:39 +00002067 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002068
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002069 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002070
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002071 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2072 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074#if defined(MBEDTLS_SSL_PROTO_SSL3)
2075 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002076 {
2077 ssl_mac( &ssl->transform_in->md_ctx_dec,
2078 ssl->transform_in->mac_dec,
2079 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002080 ssl->in_ctr, ssl->in_msgtype,
2081 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002082 }
2083 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2085#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2086 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2087 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002088 {
2089 /*
2090 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002091 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002092 *
2093 * Known timing attacks:
2094 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2095 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002096 * To compensate for different timings for the MAC calculation
2097 * depending on how much padding was removed (which is determined
2098 * by padlen), process extra_run more blocks through the hash
2099 * function.
2100 *
2101 * The formula in the paper is
2102 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2103 * where L1 is the size of the header plus the decrypted message
2104 * plus CBC padding and L2 is the size of the header plus the
2105 * decrypted message. This is for an underlying hash function
2106 * with 64-byte blocks.
2107 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2108 * correctly. We round down instead of up, so -56 is the correct
2109 * value for our calculations instead of -55.
2110 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002111 * Repeat the formula rather than defining a block_size variable.
2112 * This avoids requiring division by a variable at runtime
2113 * (which would be marginally less efficient and would require
2114 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002115 */
2116 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002117
2118 /*
2119 * The next two sizes are the minimum and maximum values of
2120 * in_msglen over all padlen values.
2121 *
2122 * They're independent of padlen, since we previously did
2123 * in_msglen -= padlen.
2124 *
2125 * Note that max_len + maclen is never more than the buffer
2126 * length, as we previously did in_msglen -= maclen too.
2127 */
2128 const size_t max_len = ssl->in_msglen + padlen;
2129 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2130
Gilles Peskine20b44082018-05-29 14:06:49 +02002131 switch( ssl->transform_in->ciphersuite_info->mac )
2132 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002133#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2134 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002135 case MBEDTLS_MD_MD5:
2136 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002137 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002138 /* 8 bytes of message size, 64-byte compression blocks */
2139 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2140 ( 13 + ssl->in_msglen + 8 ) / 64;
2141 break;
2142#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002143#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002144 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002145 /* 16 bytes of message size, 128-byte compression blocks */
2146 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2147 ( 13 + ssl->in_msglen + 16 ) / 128;
2148 break;
2149#endif
2150 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002152 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2153 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002154
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002155 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2158 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2159 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2160 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002161 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002162 /* Make sure we access everything even when padlen > 0. This
2163 * makes the synchronisation requirements for just-in-time
2164 * Prime+Probe attacks much tighter and hopefully impractical. */
2165 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002166 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002167
2168 /* Call mbedtls_md_process at least once due to cache attacks
2169 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002170 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002174
2175 /* Make sure we access all the memory that could contain the MAC,
2176 * before we check it in the next code block. This makes the
2177 * synchronisation requirements for just-in-time Prime+Probe
2178 * attacks much tighter and hopefully impractical. */
2179 ssl_read_memory( ssl->in_msg + min_len,
2180 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002181 }
2182 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2184 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2187 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002188 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002189
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002190#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002191 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2192 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2193 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002194#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002195
Hanno Becker992b6872017-11-09 18:57:39 +00002196 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2197 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199#if defined(MBEDTLS_SSL_DEBUG_ALL)
2200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002201#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002202 correct = 0;
2203 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002204 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002205
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002206 /*
2207 * Finally check the correct flag
2208 */
2209 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002211 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002212#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002213
2214 /* Make extra sure authentication was performed, exactly once */
2215 if( auth_done != 1 )
2216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2218 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002219 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002220
2221 if( ssl->in_msglen == 0 )
2222 {
Angus Gratton34817922018-06-19 15:58:22 +10002223#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2224 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2225 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2226 {
2227 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2229 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2230 }
2231#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2232
Paul Bakker5121ce52009-01-03 21:22:43 +00002233 ssl->nb_zero++;
2234
2235 /*
2236 * Three or more empty messages may be a DoS attack
2237 * (excessive CPU consumption).
2238 */
2239 if( ssl->nb_zero > 3 )
2240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 }
2245 }
2246 else
2247 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002250 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002251 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002252 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002253 }
2254 else
2255#endif
2256 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002257 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002258 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2259 if( ++ssl->in_ctr[i - 1] != 0 )
2260 break;
2261
2262 /* The loop goes to its end iff the counter is wrapping */
2263 if( i == ssl_ep_len( ssl ) )
2264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2266 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002267 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002268 }
2269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002271
2272 return( 0 );
2273}
2274
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002275#undef MAC_NONE
2276#undef MAC_PLAINTEXT
2277#undef MAC_CIPHERTEXT
2278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002280/*
2281 * Compression/decompression functions
2282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002284{
2285 int ret;
2286 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002287 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002288 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002289 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002292
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002293 if( len_pre == 0 )
2294 return( 0 );
2295
Paul Bakker2770fbd2012-07-03 13:30:23 +00002296 memcpy( msg_pre, ssl->out_msg, len_pre );
2297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002299 ssl->out_msglen ) );
2300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002302 ssl->out_msg, ssl->out_msglen );
2303
Paul Bakker48916f92012-09-16 19:57:18 +00002304 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2305 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2306 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002307 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002308
Paul Bakker48916f92012-09-16 19:57:18 +00002309 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002310 if( ret != Z_OK )
2311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2313 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002314 }
2315
Angus Grattond8213d02016-05-25 20:56:48 +10002316 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002317 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002320 ssl->out_msglen ) );
2321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002323 ssl->out_msg, ssl->out_msglen );
2324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002326
2327 return( 0 );
2328}
2329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002331{
2332 int ret;
2333 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002334 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002335 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002336 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002339
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002340 if( len_pre == 0 )
2341 return( 0 );
2342
Paul Bakker2770fbd2012-07-03 13:30:23 +00002343 memcpy( msg_pre, ssl->in_msg, len_pre );
2344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002346 ssl->in_msglen ) );
2347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002349 ssl->in_msg, ssl->in_msglen );
2350
Paul Bakker48916f92012-09-16 19:57:18 +00002351 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2352 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2353 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002354 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002355 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002356
Paul Bakker48916f92012-09-16 19:57:18 +00002357 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002358 if( ret != Z_OK )
2359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2361 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002362 }
2363
Angus Grattond8213d02016-05-25 20:56:48 +10002364 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002365 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002368 ssl->in_msglen ) );
2369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002371 ssl->in_msg, ssl->in_msglen );
2372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002374
2375 return( 0 );
2376}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2380static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382#if defined(MBEDTLS_SSL_PROTO_DTLS)
2383static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002384{
2385 /* If renegotiation is not enforced, retransmit until we would reach max
2386 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002387 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002388 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002389 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002390 unsigned char doublings = 1;
2391
2392 while( ratio != 0 )
2393 {
2394 ++doublings;
2395 ratio >>= 1;
2396 }
2397
2398 if( ++ssl->renego_records_seen > doublings )
2399 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002401 return( 0 );
2402 }
2403 }
2404
2405 return( ssl_write_hello_request( ssl ) );
2406}
2407#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002409
Paul Bakker5121ce52009-01-03 21:22:43 +00002410/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002411 * Fill the input message buffer by appending data to it.
2412 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002413 *
2414 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2415 * available (from this read and/or a previous one). Otherwise, an error code
2416 * is returned (possibly EOF or WANT_READ).
2417 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002418 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2419 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2420 * since we always read a whole datagram at once.
2421 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002422 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002423 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002426{
Paul Bakker23986e52011-04-24 08:57:21 +00002427 int ret;
2428 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002431
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002432 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002435 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002437 }
2438
Angus Grattond8213d02016-05-25 20:56:48 +10002439 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2442 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002443 }
2444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002446 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002448 uint32_t timeout;
2449
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002450 /* Just to be sure */
2451 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2452 {
2453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2454 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2455 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2456 }
2457
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002458 /*
2459 * The point is, we need to always read a full datagram at once, so we
2460 * sometimes read more then requested, and handle the additional data.
2461 * It could be the rest of the current record (while fetching the
2462 * header) and/or some other records in the same datagram.
2463 */
2464
2465 /*
2466 * Move to the next record in the already read datagram if applicable
2467 */
2468 if( ssl->next_record_offset != 0 )
2469 {
2470 if( ssl->in_left < ssl->next_record_offset )
2471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2473 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002474 }
2475
2476 ssl->in_left -= ssl->next_record_offset;
2477
2478 if( ssl->in_left != 0 )
2479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002481 ssl->next_record_offset ) );
2482 memmove( ssl->in_hdr,
2483 ssl->in_hdr + ssl->next_record_offset,
2484 ssl->in_left );
2485 }
2486
2487 ssl->next_record_offset = 0;
2488 }
2489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002491 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002492
2493 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002494 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002495 */
2496 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002499 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002500 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002501
2502 /*
2503 * A record can't be split accross datagrams. If we need to read but
2504 * are not at the beginning of a new record, the caller did something
2505 * wrong.
2506 */
2507 if( ssl->in_left != 0 )
2508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2510 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002511 }
2512
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002513 /*
2514 * Don't even try to read if time's out already.
2515 * This avoids by-passing the timer when repeatedly receiving messages
2516 * that will end up being dropped.
2517 */
2518 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002519 {
2520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002521 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002522 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002523 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002524 {
Angus Grattond8213d02016-05-25 20:56:48 +10002525 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002528 timeout = ssl->handshake->retransmit_timeout;
2529 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002530 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002533
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002534 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002535 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2536 timeout );
2537 else
2538 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002541
2542 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002544 }
2545
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002546 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002549 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002552 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002553 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002556 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002557 }
2558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002562 return( ret );
2563 }
2564
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002565 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002566 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002568 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002570 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002571 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002574 return( ret );
2575 }
2576
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002577 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002578 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002580 }
2581
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 if( ret < 0 )
2583 return( ret );
2584
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002585 ssl->in_left = ret;
2586 }
2587 else
2588#endif
2589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002591 ssl->in_left, nb_want ) );
2592
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002593 while( ssl->in_left < nb_want )
2594 {
2595 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002596
2597 if( ssl_check_timer( ssl ) != 0 )
2598 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2599 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002600 {
2601 if( ssl->f_recv_timeout != NULL )
2602 {
2603 ret = ssl->f_recv_timeout( ssl->p_bio,
2604 ssl->in_hdr + ssl->in_left, len,
2605 ssl->conf->read_timeout );
2606 }
2607 else
2608 {
2609 ret = ssl->f_recv( ssl->p_bio,
2610 ssl->in_hdr + ssl->in_left, len );
2611 }
2612 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002615 ssl->in_left, nb_want ) );
2616 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002617
2618 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002620
2621 if( ret < 0 )
2622 return( ret );
2623
mohammad160352aecb92018-03-28 23:41:40 -07002624 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002625 {
Darryl Green11999bb2018-03-13 15:22:58 +00002626 MBEDTLS_SSL_DEBUG_MSG( 1,
2627 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002628 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002629 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2630 }
2631
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002632 ssl->in_left += ret;
2633 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 }
2635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
2638 return( 0 );
2639}
2640
2641/*
2642 * Flush any data not yet written
2643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002645{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002646 int ret;
2647 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002651 if( ssl->f_send == NULL )
2652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002654 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002656 }
2657
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002658 /* Avoid incrementing counter if data is flushed */
2659 if( ssl->out_left == 0 )
2660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002662 return( 0 );
2663 }
2664
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 while( ssl->out_left > 0 )
2666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2668 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) +
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002671 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002672 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
2676 if( ret <= 0 )
2677 return( ret );
2678
mohammad160352aecb92018-03-28 23:41:40 -07002679 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002680 {
Darryl Green11999bb2018-03-13 15:22:58 +00002681 MBEDTLS_SSL_DEBUG_MSG( 1,
2682 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002683 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002684 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2685 }
2686
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 ssl->out_left -= ret;
2688 }
2689
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002690 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002691 if( ++ssl->out_ctr[i - 1] != 0 )
2692 break;
2693
2694 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002695 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2698 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002699 }
2700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
2703 return( 0 );
2704}
2705
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002706/*
2707 * Functions to handle the DTLS retransmission state machine
2708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002710/*
2711 * Append current handshake message to current outgoing flight
2712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002714{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 mbedtls_ssl_flight_item *msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002716
2717 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002718 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002719 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002722 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002723 }
2724
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002725 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002726 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002729 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002730 }
2731
2732 /* Copy current handshake message with headers */
2733 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2734 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002735 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002736 msg->next = NULL;
2737
2738 /* Append to the current flight */
2739 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002740 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002741 else
2742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002744 while( cur->next != NULL )
2745 cur = cur->next;
2746 cur->next = msg;
2747 }
2748
2749 return( 0 );
2750}
2751
2752/*
2753 * Free the current flight of handshake messages
2754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002756{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757 mbedtls_ssl_flight_item *cur = flight;
2758 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002759
2760 while( cur != NULL )
2761 {
2762 next = cur->next;
2763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 mbedtls_free( cur->p );
2765 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002766
2767 cur = next;
2768 }
2769}
2770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2772static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002773#endif
2774
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002775/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002776 * Swap transform_out and out_ctr with the alternative ones
2777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002779{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002781 unsigned char tmp_out_ctr[8];
2782
2783 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002786 return;
2787 }
2788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002790
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002791 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002792 tmp_transform = ssl->transform_out;
2793 ssl->transform_out = ssl->handshake->alt_transform_out;
2794 ssl->handshake->alt_transform_out = tmp_transform;
2795
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002796 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002797 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2798 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2799 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002800
2801 /* Adjust to the newly activated transform */
2802 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002804 {
2805 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2806 ssl->transform_out->fixed_ivlen;
2807 }
2808 else
2809 ssl->out_msg = ssl->out_iv;
2810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2812 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2817 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002818 }
2819 }
2820#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002821}
2822
2823/*
2824 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002825 */
2826int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2827{
2828 int ret = 0;
2829
2830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2831
2832 ret = mbedtls_ssl_flight_transmit( ssl );
2833
2834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2835
2836 return( ret );
2837}
2838
2839/*
2840 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002841 *
2842 * Need to remember the current message in case flush_output returns
2843 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002844 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002845 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002846int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002847{
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002851 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002853
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002854 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002855 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002856 ssl_swap_epochs( ssl );
2857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002859 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002860
2861 while( ssl->handshake->cur_msg != NULL )
2862 {
2863 int ret;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002864 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002865 /* Swap epochs before sending Finished: we can't do it after
2866 * sending ChangeCipherSpec, in case write returns WANT_READ.
2867 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2869 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002870 {
2871 ssl_swap_epochs( ssl );
2872 }
2873
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002874 /* CCS is copied as is, while HS messages may need fragmentation */
2875 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2876 {
2877 memcpy( ssl->out_msg, cur->p, cur->len );
2878 ssl->out_msglen = cur->len;
2879 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002880
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002881 /* Update position inside current message */
2882 ssl->handshake->cur_msg_p += cur->len;
2883 }
2884 else
2885 {
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002886 const int ret_payload = mbedtls_ssl_get_max_out_record_payload( ssl );
2887 const size_t max_record_payload = (size_t) ret_payload;
2888 /* DTLS handshake headers are 12 bytes */
2889 const size_t max_hs_fragment_len = max_record_payload - 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002890 const unsigned char * const p = ssl->handshake->cur_msg_p;
2891 const size_t hs_len = cur->len - 12;
2892 const size_t frag_off = p - ( cur->p + 12 );
2893 const size_t rem_len = hs_len - frag_off;
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002894 const size_t frag_len = rem_len > max_hs_fragment_len
2895 ? max_hs_fragment_len : rem_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002896
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002897 if( ret_payload < 0 )
2898 {
2899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload",
2900 ret_payload );
2901 return( ret_payload );
2902 }
2903
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002904 if( frag_off == 0 && frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002905 {
2906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
2907 (unsigned) hs_len,
2908 (unsigned) max_hs_fragment_len ) );
2909 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002910
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002911 /* Messages are stored with handshake headers as if not fragmented,
2912 * copy beginning of headers then fill fragmentation fields.
2913 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2914 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002915
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002916 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2917 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2918 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2919
2920 ssl->out_msg[ 9] = ( ( frag_len >> 16 ) & 0xff );
2921 ssl->out_msg[10] = ( ( frag_len >> 8 ) & 0xff );
2922 ssl->out_msg[11] = ( ( frag_len ) & 0xff );
2923
2924 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2925
Manuel Pégourié-Gonnard7e89c172018-08-13 12:45:26 +02002926 /* Copy the handshake message content and set records fields */
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002927 memcpy( ssl->out_msg + 12, p, frag_len );
2928 ssl->out_msglen = frag_len + 12;
2929 ssl->out_msgtype = cur->type;
2930
2931 /* Update position inside current message */
2932 ssl->handshake->cur_msg_p += frag_len;
2933 }
2934
2935 /* If done with the current message move to the next one if any */
2936 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2937 {
2938 if( cur->next != NULL )
2939 {
2940 ssl->handshake->cur_msg = cur->next;
2941 ssl->handshake->cur_msg_p = cur->next->p + 12;
2942 }
2943 else
2944 {
2945 ssl->handshake->cur_msg = NULL;
2946 ssl->handshake->cur_msg_p = NULL;
2947 }
2948 }
2949
2950 /* Actually send the message out */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002954 return( ret );
2955 }
2956 }
2957
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002958 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2960 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002961 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002964 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2965 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002966
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002968
2969 return( 0 );
2970}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002971
2972/*
2973 * To be called when the last message of an incoming flight is received.
2974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002976{
2977 /* We won't need to resend that one any more */
2978 ssl_flight_free( ssl->handshake->flight );
2979 ssl->handshake->flight = NULL;
2980 ssl->handshake->cur_msg = NULL;
2981
2982 /* The next incoming flight will start with this msg_seq */
2983 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2984
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002985 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002986 ssl_set_timer( ssl, 0 );
2987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2989 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002992 }
2993 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002995}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002996
2997/*
2998 * To be called when the last message of an outgoing flight is send.
2999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003001{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003002 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003003 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3006 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003009 }
3010 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003012}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003014
Paul Bakker5121ce52009-01-03 21:22:43 +00003015/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003016 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003017 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003018
3019/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003020 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003021 *
3022 * - fill in handshake headers
3023 * - update handshake checksum
3024 * - DTLS: save message for resending
3025 * - then pass to the record layer
3026 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003027 * DTLS: except for HelloRequest, messages are only queued, and will only be
3028 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003029 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003030 * Inputs:
3031 * - ssl->out_msglen: 4 + actual handshake message len
3032 * (4 is the size of handshake headers for TLS)
3033 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3034 * - ssl->out_msg + 4: the handshake message body
3035 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003036 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003037 * - ssl->out_msglen: the length of the record contents
3038 * (including handshake headers but excluding record headers)
3039 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003040 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003041int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003042{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003043 int ret;
3044 const size_t hs_len = ssl->out_msglen - 4;
3045 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003046
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3048
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003049 /*
3050 * Sanity checks
3051 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003052 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3053 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3054 {
3055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3056 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3057 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003059 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3060 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3061 ssl->handshake == NULL )
3062 {
3063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3064 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3065 }
3066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003068 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003069 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003071 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003074 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003075#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003076
3077 /*
3078 * Fill handshake headers
3079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003081 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003082 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3083 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3084 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003085
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003086 /*
3087 * DTLS has additional fields in the Handshake layer,
3088 * between the length field and the actual payload:
3089 * uint16 message_seq;
3090 * uint24 fragment_offset;
3091 * uint24 fragment_length;
3092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003094 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003095 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003096 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003097 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003098 {
3099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3100 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003101 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003102 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003103 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3104 }
3105
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003106 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003107 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003108
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003109 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003110 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003111 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003112 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3113 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3114 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003115 }
3116 else
3117 {
3118 ssl->out_msg[4] = 0;
3119 ssl->out_msg[5] = 0;
3120 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003121
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003122 /* Handshake hashes are computed without fragmentation,
3123 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003124 memset( ssl->out_msg + 6, 0x00, 3 );
3125 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003126 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003128
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003129 /* Update running hashes of hanshake messages seen */
3130 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3131 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003132 }
3133
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003134 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003136 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003137 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003138 {
3139 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003142 return( ret );
3143 }
3144 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003145 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003146#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003147 {
3148 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
3149 {
3150 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3151 return( ret );
3152 }
3153 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003154
3155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3156
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003157 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003158}
3159
3160/*
3161 * Record layer functions
3162 */
3163
3164/*
3165 * Write current record.
3166 *
3167 * Uses:
3168 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3169 * - ssl->out_msglen: length of the record content (excl headers)
3170 * - ssl->out_msg: record content
3171 */
3172int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
3173{
3174 int ret, done = 0;
3175 size_t len = ssl->out_msglen;
3176
3177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003180 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003182 {
3183 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003186 return( ret );
3187 }
3188
3189 len = ssl->out_msglen;
3190 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3194 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 ret = mbedtls_ssl_hw_record_write( ssl );
3199 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3202 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003203 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003204
3205 if( ret == 0 )
3206 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003207 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003209 if( !done )
3210 {
3211 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003212 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003213 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003214
3215 ssl->out_len[0] = (unsigned char)( len >> 8 );
3216 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003217
Paul Bakker48916f92012-09-16 19:57:18 +00003218 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003219 {
3220 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003223 return( ret );
3224 }
3225
3226 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003227 ssl->out_len[0] = (unsigned char)( len >> 8 );
3228 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003229 }
3230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00003232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00003234 "version = [%d:%d], msglen = %d",
3235 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003236 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3239 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003240 }
3241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 return( ret );
3246 }
3247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003249
3250 return( 0 );
3251}
3252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003254/*
3255 * Mark bits in bitmask (used for DTLS HS reassembly)
3256 */
3257static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3258{
3259 unsigned int start_bits, end_bits;
3260
3261 start_bits = 8 - ( offset % 8 );
3262 if( start_bits != 8 )
3263 {
3264 size_t first_byte_idx = offset / 8;
3265
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003266 /* Special case */
3267 if( len <= start_bits )
3268 {
3269 for( ; len != 0; len-- )
3270 mask[first_byte_idx] |= 1 << ( start_bits - len );
3271
3272 /* Avoid potential issues with offset or len becoming invalid */
3273 return;
3274 }
3275
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003276 offset += start_bits; /* Now offset % 8 == 0 */
3277 len -= start_bits;
3278
3279 for( ; start_bits != 0; start_bits-- )
3280 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3281 }
3282
3283 end_bits = len % 8;
3284 if( end_bits != 0 )
3285 {
3286 size_t last_byte_idx = ( offset + len ) / 8;
3287
3288 len -= end_bits; /* Now len % 8 == 0 */
3289
3290 for( ; end_bits != 0; end_bits-- )
3291 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3292 }
3293
3294 memset( mask + offset / 8, 0xFF, len / 8 );
3295}
3296
3297/*
3298 * Check that bitmask is full
3299 */
3300static int ssl_bitmask_check( unsigned char *mask, size_t len )
3301{
3302 size_t i;
3303
3304 for( i = 0; i < len / 8; i++ )
3305 if( mask[i] != 0xFF )
3306 return( -1 );
3307
3308 for( i = 0; i < len % 8; i++ )
3309 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3310 return( -1 );
3311
3312 return( 0 );
3313}
3314
3315/*
3316 * Reassemble fragmented DTLS handshake messages.
3317 *
3318 * Use a temporary buffer for reassembly, divided in two parts:
3319 * - the first holds the reassembled message (including handshake header),
3320 * - the second holds a bitmask indicating which parts of the message
3321 * (excluding headers) have been received so far.
3322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003324{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003325 unsigned char *msg, *bitmask;
3326 size_t frag_len, frag_off;
3327 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3328
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003329 if( ssl->handshake == NULL )
3330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3332 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003333 }
3334
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003335 /*
3336 * For first fragment, check size and allocate buffer
3337 */
3338 if( ssl->handshake->hs_msg == NULL )
3339 {
3340 size_t alloc_len;
3341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003343 msg_len ) );
3344
Angus Grattond8213d02016-05-25 20:56:48 +10003345 if( ssl->in_hslen > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3348 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003349 }
3350
3351 /* The bitmask needs one bit per byte of message excluding header */
3352 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3353
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003354 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003355 if( ssl->handshake->hs_msg == NULL )
3356 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003358 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003359 }
3360
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003361 /* Prepare final header: copy msg_type, length and message_seq,
3362 * then add standardised fragment_offset and fragment_length */
3363 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3364 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3365 memcpy( ssl->handshake->hs_msg + 9,
3366 ssl->handshake->hs_msg + 1, 3 );
3367 }
3368 else
3369 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003370 /* Make sure msg_type and length are consistent */
3371 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3374 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003375 }
3376 }
3377
3378 msg = ssl->handshake->hs_msg + 12;
3379 bitmask = msg + msg_len;
3380
3381 /*
3382 * Check and copy current fragment
3383 */
3384 frag_off = ( ssl->in_msg[6] << 16 ) |
3385 ( ssl->in_msg[7] << 8 ) |
3386 ssl->in_msg[8];
3387 frag_len = ( ssl->in_msg[9] << 16 ) |
3388 ( ssl->in_msg[10] << 8 ) |
3389 ssl->in_msg[11];
3390
3391 if( frag_off + frag_len > msg_len )
3392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003394 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003396 }
3397
3398 if( frag_len + 12 > ssl->in_msglen )
3399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003401 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003403 }
3404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003406 frag_off, frag_len ) );
3407
3408 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3409 ssl_bitmask_set( bitmask, frag_off, frag_len );
3410
3411 /*
3412 * Do we have the complete message by now?
3413 * If yes, finalize it, else ask to read the next record.
3414 */
3415 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01003418 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003419 }
3420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003422
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003423 if( frag_len + 12 < ssl->in_msglen )
3424 {
3425 /*
3426 * We'got more handshake messages in the same record.
3427 * This case is not handled now because no know implementation does
3428 * that and it's hard to test, so we prefer to fail cleanly for now.
3429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3431 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003432 }
3433
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003434 if( ssl->in_left > ssl->next_record_offset )
3435 {
3436 /*
3437 * We've got more data in the buffer after the current record,
3438 * that we don't want to overwrite. Move it before writing the
3439 * reassembled message, and adjust in_left and next_record_offset.
3440 */
3441 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3442 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3443 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3444
3445 /* First compute and check new lengths */
3446 ssl->next_record_offset = new_remain - ssl->in_hdr;
3447 ssl->in_left = ssl->next_record_offset + remain_len;
3448
Angus Grattond8213d02016-05-25 20:56:48 +10003449 if( ssl->in_left > MBEDTLS_SSL_IN_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003450 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3453 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003454 }
3455
3456 memmove( new_remain, cur_remain, remain_len );
3457 }
3458
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003459 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003462 ssl->handshake->hs_msg = NULL;
3463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003465 ssl->in_msg, ssl->in_hslen );
3466
3467 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003468}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003470
Simon Butcher99000142016-10-13 17:21:01 +01003471int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003472{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003476 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003478 }
3479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003481 ( ssl->in_msg[1] << 16 ) |
3482 ( ssl->in_msg[2] << 8 ) |
3483 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003486 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003487 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003490 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003491 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003492 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003493 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003494
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003495 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003496 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3497 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3498 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3499 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003500 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003501 /* Retransmit only on last message from previous flight, to avoid
3502 * too many retransmissions.
3503 * Besides, No sane server ever retransmits HelloVerifyRequest */
3504 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003508 "message_seq = %d, start_of_flight = %d",
3509 recv_msg_seq,
3510 ssl->handshake->in_flight_start_seq ) );
3511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003515 return( ret );
3516 }
3517 }
3518 else
3519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003521 "message_seq = %d, expected = %d",
3522 recv_msg_seq,
3523 ssl->handshake->in_msg_seq ) );
3524 }
3525
Hanno Becker90333da2017-10-10 11:27:13 +01003526 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003527 }
3528 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003529
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003530 /* Reassemble if current message is fragmented or reassembly is
3531 * already in progress */
3532 if( ssl->in_msglen < ssl->in_hslen ||
3533 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3534 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3535 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003538
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003539 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003542 return( ret );
3543 }
3544 }
3545 }
3546 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003548 /* With TLS we don't handle fragmentation (for now) */
3549 if( ssl->in_msglen < ssl->in_hslen )
3550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3552 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003553 }
3554
Simon Butcher99000142016-10-13 17:21:01 +01003555 return( 0 );
3556}
3557
3558void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3559{
3560
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003561 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3562 ssl->handshake != NULL )
3563 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003564 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003565 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003566
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003567 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003569 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003570 ssl->handshake != NULL )
3571 {
3572 ssl->handshake->in_msg_seq++;
3573 }
3574#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003575}
3576
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003577/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003578 * DTLS anti-replay: RFC 6347 4.1.2.6
3579 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003580 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3581 * Bit n is set iff record number in_window_top - n has been seen.
3582 *
3583 * Usually, in_window_top is the last record number seen and the lsb of
3584 * in_window is set. The only exception is the initial state (record number 0
3585 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3588static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003589{
3590 ssl->in_window_top = 0;
3591 ssl->in_window = 0;
3592}
3593
3594static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3595{
3596 return( ( (uint64_t) buf[0] << 40 ) |
3597 ( (uint64_t) buf[1] << 32 ) |
3598 ( (uint64_t) buf[2] << 24 ) |
3599 ( (uint64_t) buf[3] << 16 ) |
3600 ( (uint64_t) buf[4] << 8 ) |
3601 ( (uint64_t) buf[5] ) );
3602}
3603
3604/*
3605 * Return 0 if sequence number is acceptable, -1 otherwise
3606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003608{
3609 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3610 uint64_t bit;
3611
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003612 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003613 return( 0 );
3614
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003615 if( rec_seqnum > ssl->in_window_top )
3616 return( 0 );
3617
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003618 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003619
3620 if( bit >= 64 )
3621 return( -1 );
3622
3623 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3624 return( -1 );
3625
3626 return( 0 );
3627}
3628
3629/*
3630 * Update replay window on new validated record
3631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003633{
3634 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3635
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003636 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003637 return;
3638
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003639 if( rec_seqnum > ssl->in_window_top )
3640 {
3641 /* Update window_top and the contents of the window */
3642 uint64_t shift = rec_seqnum - ssl->in_window_top;
3643
3644 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003645 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003646 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003647 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003648 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003649 ssl->in_window |= 1;
3650 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003651
3652 ssl->in_window_top = rec_seqnum;
3653 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003654 else
3655 {
3656 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003657 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003658
3659 if( bit < 64 ) /* Always true, but be extra sure */
3660 ssl->in_window |= (uint64_t) 1 << bit;
3661 }
3662}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003664
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003665#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003666/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003667static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3668
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003669/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003670 * Without any SSL context, check if a datagram looks like a ClientHello with
3671 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003672 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003673 *
3674 * - if cookie is valid, return 0
3675 * - if ClientHello looks superficially valid but cookie is not,
3676 * fill obuf and set olen, then
3677 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3678 * - otherwise return a specific error code
3679 */
3680static int ssl_check_dtls_clihlo_cookie(
3681 mbedtls_ssl_cookie_write_t *f_cookie_write,
3682 mbedtls_ssl_cookie_check_t *f_cookie_check,
3683 void *p_cookie,
3684 const unsigned char *cli_id, size_t cli_id_len,
3685 const unsigned char *in, size_t in_len,
3686 unsigned char *obuf, size_t buf_len, size_t *olen )
3687{
3688 size_t sid_len, cookie_len;
3689 unsigned char *p;
3690
3691 if( f_cookie_write == NULL || f_cookie_check == NULL )
3692 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3693
3694 /*
3695 * Structure of ClientHello with record and handshake headers,
3696 * and expected values. We don't need to check a lot, more checks will be
3697 * done when actually parsing the ClientHello - skipping those checks
3698 * avoids code duplication and does not make cookie forging any easier.
3699 *
3700 * 0-0 ContentType type; copied, must be handshake
3701 * 1-2 ProtocolVersion version; copied
3702 * 3-4 uint16 epoch; copied, must be 0
3703 * 5-10 uint48 sequence_number; copied
3704 * 11-12 uint16 length; (ignored)
3705 *
3706 * 13-13 HandshakeType msg_type; (ignored)
3707 * 14-16 uint24 length; (ignored)
3708 * 17-18 uint16 message_seq; copied
3709 * 19-21 uint24 fragment_offset; copied, must be 0
3710 * 22-24 uint24 fragment_length; (ignored)
3711 *
3712 * 25-26 ProtocolVersion client_version; (ignored)
3713 * 27-58 Random random; (ignored)
3714 * 59-xx SessionID session_id; 1 byte len + sid_len content
3715 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3716 * ...
3717 *
3718 * Minimum length is 61 bytes.
3719 */
3720 if( in_len < 61 ||
3721 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3722 in[3] != 0 || in[4] != 0 ||
3723 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3724 {
3725 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3726 }
3727
3728 sid_len = in[59];
3729 if( sid_len > in_len - 61 )
3730 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3731
3732 cookie_len = in[60 + sid_len];
3733 if( cookie_len > in_len - 60 )
3734 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3735
3736 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3737 cli_id, cli_id_len ) == 0 )
3738 {
3739 /* Valid cookie */
3740 return( 0 );
3741 }
3742
3743 /*
3744 * If we get here, we've got an invalid cookie, let's prepare HVR.
3745 *
3746 * 0-0 ContentType type; copied
3747 * 1-2 ProtocolVersion version; copied
3748 * 3-4 uint16 epoch; copied
3749 * 5-10 uint48 sequence_number; copied
3750 * 11-12 uint16 length; olen - 13
3751 *
3752 * 13-13 HandshakeType msg_type; hello_verify_request
3753 * 14-16 uint24 length; olen - 25
3754 * 17-18 uint16 message_seq; copied
3755 * 19-21 uint24 fragment_offset; copied
3756 * 22-24 uint24 fragment_length; olen - 25
3757 *
3758 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3759 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3760 *
3761 * Minimum length is 28.
3762 */
3763 if( buf_len < 28 )
3764 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3765
3766 /* Copy most fields and adapt others */
3767 memcpy( obuf, in, 25 );
3768 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3769 obuf[25] = 0xfe;
3770 obuf[26] = 0xff;
3771
3772 /* Generate and write actual cookie */
3773 p = obuf + 28;
3774 if( f_cookie_write( p_cookie,
3775 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3776 {
3777 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3778 }
3779
3780 *olen = p - obuf;
3781
3782 /* Go back and fill length fields */
3783 obuf[27] = (unsigned char)( *olen - 28 );
3784
3785 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3786 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3787 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3788
3789 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3790 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3791
3792 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3793}
3794
3795/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003796 * Handle possible client reconnect with the same UDP quadruplet
3797 * (RFC 6347 Section 4.2.8).
3798 *
3799 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3800 * that looks like a ClientHello.
3801 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003802 * - if the input looks like a ClientHello without cookies,
3803 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003804 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003805 * - if the input looks like a ClientHello with a valid cookie,
3806 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003807 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003808 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003809 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003810 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003811 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3812 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003813 */
3814static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3815{
3816 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003817 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003818
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003819 ret = ssl_check_dtls_clihlo_cookie(
3820 ssl->conf->f_cookie_write,
3821 ssl->conf->f_cookie_check,
3822 ssl->conf->p_cookie,
3823 ssl->cli_id, ssl->cli_id_len,
3824 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003825 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003826
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003827 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3828
3829 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003830 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003831 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003832 * If the error is permanent we'll catch it later,
3833 * if it's not, then hopefully it'll work next time. */
3834 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3835
3836 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003837 }
3838
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003839 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003840 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003841 /* Got a valid cookie, partially reset context */
3842 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3843 {
3844 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3845 return( ret );
3846 }
3847
3848 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003849 }
3850
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003851 return( ret );
3852}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003853#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003854
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003855/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003856 * ContentType type;
3857 * ProtocolVersion version;
3858 * uint16 epoch; // DTLS only
3859 * uint48 sequence_number; // DTLS only
3860 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003861 *
3862 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003863 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003864 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3865 *
3866 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003867 * 1. proceed with the record if this function returns 0
3868 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3869 * 3. return CLIENT_RECONNECT if this function return that value
3870 * 4. drop the whole datagram if this function returns anything else.
3871 * Point 2 is needed when the peer is resending, and we have already received
3872 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003873 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003875{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003876 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878 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 +02003879
Paul Bakker5121ce52009-01-03 21:22:43 +00003880 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003881 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003882 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003885 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003886 ssl->in_msgtype,
3887 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003888
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003889 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3891 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3892 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3893 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003896
3897#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01003898 /* Silently ignore invalid DTLS records as recommended by RFC 6347
3899 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003900 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3901#endif /* MBEDTLS_SSL_PROTO_DTLS */
3902 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3903 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
3904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003906 }
3907
3908 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003909 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3912 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003913 }
3914
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003915 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3918 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003919 }
3920
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003921 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10003922 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003923 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3926 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003927 }
3928
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003929 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003930 * DTLS-related tests.
3931 * Check epoch before checking length constraint because
3932 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3933 * message gets duplicated before the corresponding Finished message,
3934 * the second ChangeCipherSpec should be discarded because it belongs
3935 * to an old epoch, but not because its length is shorter than
3936 * the minimum record length for packets using the new record transform.
3937 * Note that these two kinds of failures are handled differently,
3938 * as an unexpected record is silently skipped but an invalid
3939 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003940 */
3941#if defined(MBEDTLS_SSL_PROTO_DTLS)
3942 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3943 {
3944 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3945
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003946 /* Check epoch (and sequence number) with DTLS */
3947 if( rec_epoch != ssl->in_epoch )
3948 {
3949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3950 "expected %d, received %d",
3951 ssl->in_epoch, rec_epoch ) );
3952
3953#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3954 /*
3955 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3956 * access the first byte of record content (handshake type), as we
3957 * have an active transform (possibly iv_len != 0), so use the
3958 * fact that the record header len is 13 instead.
3959 */
3960 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3961 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3962 rec_epoch == 0 &&
3963 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3964 ssl->in_left > 13 &&
3965 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3966 {
3967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3968 "from the same port" ) );
3969 return( ssl_handle_possible_reconnect( ssl ) );
3970 }
3971 else
3972#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3973 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3974 }
3975
3976#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3977 /* Replay detection only works for the current epoch */
3978 if( rec_epoch == ssl->in_epoch &&
3979 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3980 {
3981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3982 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3983 }
3984#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01003985
3986 /* Drop unexpected ChangeCipherSpec messages */
3987 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3988 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3989 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3990 {
3991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3992 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3993 }
3994
3995 /* Drop unexpected ApplicationData records,
3996 * except at the beginning of renegotiations */
3997 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
3998 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
3999#if defined(MBEDTLS_SSL_RENEGOTIATION)
4000 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4001 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4002#endif
4003 )
4004 {
4005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4006 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4007 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004008 }
4009#endif /* MBEDTLS_SSL_PROTO_DTLS */
4010
Hanno Becker52c6dc62017-05-26 16:07:36 +01004011
4012 /* Check length against bounds of the current transform and version */
4013 if( ssl->transform_in == NULL )
4014 {
4015 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004016 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004017 {
4018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4019 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4020 }
4021 }
4022 else
4023 {
4024 if( ssl->in_msglen < ssl->transform_in->minlen )
4025 {
4026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4027 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4028 }
4029
4030#if defined(MBEDTLS_SSL_PROTO_SSL3)
4031 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004032 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004033 {
4034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4035 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4036 }
4037#endif
4038#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4039 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4040 /*
4041 * TLS encrypted messages can have up to 256 bytes of padding
4042 */
4043 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4044 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004045 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004046 {
4047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4048 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4049 }
4050#endif
4051 }
4052
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004053 return( 0 );
4054}
Paul Bakker5121ce52009-01-03 21:22:43 +00004055
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004056/*
4057 * If applicable, decrypt (and decompress) record content
4058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004059static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004060{
4061 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4064 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4067 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071 ret = mbedtls_ssl_hw_record_read( ssl );
4072 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4075 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004076 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004077
4078 if( ret == 0 )
4079 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004080 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004081#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004082 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004083 {
4084 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004086 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004087 return( ret );
4088 }
4089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004091 ssl->in_msg, ssl->in_msglen );
4092
Angus Grattond8213d02016-05-25 20:56:48 +10004093 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4096 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004097 }
4098 }
4099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004100#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004101 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004102 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004103 {
4104 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004107 return( ret );
4108 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004109 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004113 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004115 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004116 }
4117#endif
4118
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004119 return( 0 );
4120}
4121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004123
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004124/*
4125 * Read a record.
4126 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004127 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4128 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4129 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004131int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004132{
4133 int ret;
4134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004136
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004137 if( ssl->keep_current_message == 0 )
4138 {
4139 do {
Simon Butcher99000142016-10-13 17:21:01 +01004140
Hanno Becker90333da2017-10-10 11:27:13 +01004141 do ret = mbedtls_ssl_read_record_layer( ssl );
4142 while( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
4143
4144 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004145 {
4146 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4147 return( ret );
4148 }
4149
4150 ret = mbedtls_ssl_handle_message_type( ssl );
4151
Hanno Becker90333da2017-10-10 11:27:13 +01004152 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4153 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004154
4155 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004156 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004157 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004158 return( ret );
4159 }
4160
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004161 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4162 {
4163 mbedtls_ssl_update_handshake_status( ssl );
4164 }
Simon Butcher99000142016-10-13 17:21:01 +01004165 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004166 else
Simon Butcher99000142016-10-13 17:21:01 +01004167 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4169 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004170 }
4171
4172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4173
4174 return( 0 );
4175}
4176
4177int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4178{
4179 int ret;
4180
Hanno Becker4a810fb2017-05-24 16:27:30 +01004181 /*
4182 * Step A
4183 *
4184 * Consume last content-layer message and potentially
4185 * update in_msglen which keeps track of the contents'
4186 * consumption state.
4187 *
4188 * (1) Handshake messages:
4189 * Remove last handshake message, move content
4190 * and adapt in_msglen.
4191 *
4192 * (2) Alert messages:
4193 * Consume whole record content, in_msglen = 0.
4194 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004195 * (3) Change cipher spec:
4196 * Consume whole record content, in_msglen = 0.
4197 *
4198 * (4) Application data:
4199 * Don't do anything - the record layer provides
4200 * the application data as a stream transport
4201 * and consumes through mbedtls_ssl_read only.
4202 *
4203 */
4204
4205 /* Case (1): Handshake messages */
4206 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004207 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004208 /* Hard assertion to be sure that no application data
4209 * is in flight, as corrupting ssl->in_msglen during
4210 * ssl->in_offt != NULL is fatal. */
4211 if( ssl->in_offt != NULL )
4212 {
4213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4214 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4215 }
4216
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004217 /*
4218 * Get next Handshake message in the current record
4219 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004220
Hanno Becker4a810fb2017-05-24 16:27:30 +01004221 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004222 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004223 * current handshake content: If DTLS handshake
4224 * fragmentation is used, that's the fragment
4225 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004226 * size here is faulty and should be changed at
4227 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004228 * (2) While it doesn't seem to cause problems, one
4229 * has to be very careful not to assume that in_hslen
4230 * is always <= in_msglen in a sensible communication.
4231 * Again, it's wrong for DTLS handshake fragmentation.
4232 * The following check is therefore mandatory, and
4233 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004234 * Additionally, ssl->in_hslen might be arbitrarily out of
4235 * bounds after handling a DTLS message with an unexpected
4236 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004237 */
4238 if( ssl->in_hslen < ssl->in_msglen )
4239 {
4240 ssl->in_msglen -= ssl->in_hslen;
4241 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4242 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004243
Hanno Becker4a810fb2017-05-24 16:27:30 +01004244 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4245 ssl->in_msg, ssl->in_msglen );
4246 }
4247 else
4248 {
4249 ssl->in_msglen = 0;
4250 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004251
Hanno Becker4a810fb2017-05-24 16:27:30 +01004252 ssl->in_hslen = 0;
4253 }
4254 /* Case (4): Application data */
4255 else if( ssl->in_offt != NULL )
4256 {
4257 return( 0 );
4258 }
4259 /* Everything else (CCS & Alerts) */
4260 else
4261 {
4262 ssl->in_msglen = 0;
4263 }
4264
4265 /*
4266 * Step B
4267 *
4268 * Fetch and decode new record if current one is fully consumed.
4269 *
4270 */
4271
4272 if( ssl->in_msglen > 0 )
4273 {
4274 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004275 return( 0 );
4276 }
4277
Hanno Becker4a810fb2017-05-24 16:27:30 +01004278 /* Current record either fully processed or to be discarded. */
4279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004283 return( ret );
4284 }
4285
4286 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004288#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004289 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4290 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004291 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004292 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4293 {
4294 /* Skip unexpected record (but not whole datagram) */
4295 ssl->next_record_offset = ssl->in_msglen
4296 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004297
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4299 "(header)" ) );
4300 }
4301 else
4302 {
4303 /* Skip invalid record and the rest of the datagram */
4304 ssl->next_record_offset = 0;
4305 ssl->in_left = 0;
4306
4307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4308 "(header)" ) );
4309 }
4310
4311 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004312 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004313 }
4314#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004315 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004316 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004317
4318 /*
4319 * Read and optionally decrypt the message contents
4320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004321 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4322 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004325 return( ret );
4326 }
4327
4328 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004330 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004333 if( ssl->next_record_offset < ssl->in_left )
4334 {
4335 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4336 }
4337 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004338 else
4339#endif
4340 ssl->in_left = 0;
4341
4342 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004344#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004345 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004346 {
4347 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004348 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4349 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004350 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004351 /* Except when waiting for Finished as a bad mac here
4352 * probably means something went wrong in the handshake
4353 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4354 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4355 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4356 {
4357#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4358 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4359 {
4360 mbedtls_ssl_send_alert_message( ssl,
4361 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4362 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4363 }
4364#endif
4365 return( ret );
4366 }
4367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004368#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004369 if( ssl->conf->badmac_limit != 0 &&
4370 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4373 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004374 }
4375#endif
4376
Hanno Becker4a810fb2017-05-24 16:27:30 +01004377 /* As above, invalid records cause
4378 * dismissal of the whole datagram. */
4379
4380 ssl->next_record_offset = 0;
4381 ssl->in_left = 0;
4382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004384 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004385 }
4386
4387 return( ret );
4388 }
4389 else
4390#endif
4391 {
4392 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4394 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396 mbedtls_ssl_send_alert_message( ssl,
4397 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4398 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004399 }
4400#endif
4401 return( ret );
4402 }
4403 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004404
Simon Butcher99000142016-10-13 17:21:01 +01004405 return( 0 );
4406}
4407
4408int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4409{
4410 int ret;
4411
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004412 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004413 * Handle particular types of records
4414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004415 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004416 {
Simon Butcher99000142016-10-13 17:21:01 +01004417 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4418 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004419 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004420 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004421 }
4422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004424 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004425 if( ssl->in_msglen != 2 )
4426 {
4427 /* Note: Standard allows for more than one 2 byte alert
4428 to be packed in a single message, but Mbed TLS doesn't
4429 currently support this. */
4430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4431 ssl->in_msglen ) );
4432 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4433 }
4434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004436 ssl->in_msg[0], ssl->in_msg[1] ) );
4437
4438 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004439 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004441 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004444 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004445 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004446 }
4447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004448 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4449 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4452 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004453 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004454
4455#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4456 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4457 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4458 {
Hanno Becker90333da2017-10-10 11:27:13 +01004459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004460 /* Will be handled when trying to parse ServerHello */
4461 return( 0 );
4462 }
4463#endif
4464
4465#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4466 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4467 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4468 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4469 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4470 {
4471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4472 /* Will be handled in mbedtls_ssl_parse_certificate() */
4473 return( 0 );
4474 }
4475#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4476
4477 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004478 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004479 }
4480
Hanno Beckerc76c6192017-06-06 10:03:17 +01004481#if defined(MBEDTLS_SSL_PROTO_DTLS)
4482 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4483 ssl->handshake != NULL &&
4484 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4485 {
4486 ssl_handshake_wrapup_free_hs_transform( ssl );
4487 }
4488#endif
4489
Paul Bakker5121ce52009-01-03 21:22:43 +00004490 return( 0 );
4491}
4492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004494{
4495 int ret;
4496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004497 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4498 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4499 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004500 {
4501 return( ret );
4502 }
4503
4504 return( 0 );
4505}
4506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004508 unsigned char level,
4509 unsigned char message )
4510{
4511 int ret;
4512
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004513 if( ssl == NULL || ssl->conf == NULL )
4514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004517 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004519 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004520 ssl->out_msglen = 2;
4521 ssl->out_msg[0] = level;
4522 ssl->out_msg[1] = message;
4523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004524 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004527 return( ret );
4528 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004530
4531 return( 0 );
4532}
4533
Paul Bakker5121ce52009-01-03 21:22:43 +00004534/*
4535 * Handshake functions
4536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004537#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4538 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4539 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4540 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4541 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4542 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4543 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004544/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004545int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004546{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4552 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004553 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4554 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004557 ssl->state++;
4558 return( 0 );
4559 }
4560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004563}
4564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004566{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004567 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4572 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004573 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4574 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004577 ssl->state++;
4578 return( 0 );
4579 }
4580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4582 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004583}
Gilles Peskinef9828522017-05-03 12:28:43 +02004584
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004585#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004586/* Some certificate support -> implement write and parse */
4587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004588int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004589{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004591 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004592 const mbedtls_x509_crt *crt;
4593 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4598 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004599 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4600 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004603 ssl->state++;
4604 return( 0 );
4605 }
4606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004607#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004608 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004609 {
4610 if( ssl->client_auth == 0 )
4611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004613 ssl->state++;
4614 return( 0 );
4615 }
4616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004617#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004618 /*
4619 * If using SSLv3 and got no cert, send an Alert message
4620 * (otherwise an empty Certificate message will be sent).
4621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004622 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4623 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004624 {
4625 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004626 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4627 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4628 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004631 goto write_msg;
4632 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004633#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004634 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635#endif /* MBEDTLS_SSL_CLI_C */
4636#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004637 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4642 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004643 }
4644 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004645#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004647 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004648
4649 /*
4650 * 0 . 0 handshake type
4651 * 1 . 3 handshake length
4652 * 4 . 6 length of all certs
4653 * 7 . 9 length of cert. 1
4654 * 10 . n-1 peer certificate
4655 * n . n+2 length of cert. 2
4656 * n+3 . ... upper level cert, etc.
4657 */
4658 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004660
Paul Bakker29087132010-03-21 21:03:34 +00004661 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004662 {
4663 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10004664 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10004667 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004668 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004669 }
4670
4671 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4672 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4673 ssl->out_msg[i + 2] = (unsigned char)( n );
4674
4675 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4676 i += n; crt = crt->next;
4677 }
4678
4679 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4680 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4681 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4682
4683 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004684 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4685 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004686
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004687#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004688write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004689#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004690
4691 ssl->state++;
4692
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004693 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004694 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004695 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004696 return( ret );
4697 }
4698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004700
Paul Bakkered27a042013-04-18 22:46:23 +02004701 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004702}
4703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004704int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004705{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004706 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004707 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004708 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004709 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004710 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004714 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4715 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004716 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4717 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004720 ssl->state++;
4721 return( 0 );
4722 }
4723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004724#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004725 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004726 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4727 {
4728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4729 ssl->state++;
4730 return( 0 );
4731 }
4732
4733#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4734 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4735 authmode = ssl->handshake->sni_authmode;
4736#endif
4737
4738 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4739 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004740 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004741 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004743 ssl->state++;
4744 return( 0 );
4745 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004746#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004749 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004750 /* mbedtls_ssl_read_record may have sent an alert already. We
4751 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004753 return( ret );
4754 }
4755
4756 ssl->state++;
4757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004758#if defined(MBEDTLS_SSL_SRV_C)
4759#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004760 /*
4761 * Check if the client sent an empty certificate
4762 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004763 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004764 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004765 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004766 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004767 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4768 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4769 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004772
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004773 /* The client was asked for a certificate but didn't send
4774 one. The client should know what's going on, so we
4775 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004776 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004777 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004778 return( 0 );
4779 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004780 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004781 }
4782 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004783#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004785#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4786 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004787 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004788 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4791 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4792 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4793 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004796
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004797 /* The client was asked for a certificate but didn't send
4798 one. The client should know what's going on, so we
4799 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004800 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004801 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004802 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004803 else
4804 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004805 }
4806 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004807#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4808 MBEDTLS_SSL_PROTO_TLS1_2 */
4809#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004814 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4815 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004817 }
4818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4820 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004823 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4824 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004825 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004826 }
4827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004829
Paul Bakker5121ce52009-01-03 21:22:43 +00004830 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004832 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004833 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004834
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004835 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004836 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004839 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4840 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004842 }
4843
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004844 /* In case we tried to reuse a session but it failed */
4845 if( ssl->session_negotiate->peer_cert != NULL )
4846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4848 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004849 }
4850
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004851 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004853 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004855 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004856 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4857 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004858 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004859 }
4860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004861 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004862
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004863 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004864
4865 while( i < ssl->in_hslen )
4866 {
Philippe Antoine747fd532018-05-30 09:13:21 +02004867 if ( i + 3 > ssl->in_hslen ) {
4868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4869 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4870 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4871 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
4872 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004873 if( ssl->in_msg[i] != 0 )
4874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004876 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4877 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004878 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004879 }
4880
4881 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4882 | (unsigned int) ssl->in_msg[i + 2];
4883 i += 3;
4884
4885 if( n < 128 || i + n > ssl->in_hslen )
4886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004888 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4889 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004890 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004891 }
4892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004893 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004894 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004895 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004896 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004897 case 0: /*ok*/
4898 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
4899 /* Ignore certificate with an unknown algorithm: maybe a
4900 prior certificate was already trusted. */
4901 break;
4902
4903 case MBEDTLS_ERR_X509_ALLOC_FAILED:
4904 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
4905 goto crt_parse_der_failed;
4906
4907 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
4908 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4909 goto crt_parse_der_failed;
4910
4911 default:
4912 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4913 crt_parse_der_failed:
4914 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004915 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004916 return( ret );
4917 }
4918
4919 i += n;
4920 }
4921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004922 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004923
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004924 /*
4925 * On client, make sure the server cert doesn't change during renego to
4926 * avoid "triple handshake" attack: https://secure-resumption.com/
4927 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004929 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004930 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004931 {
4932 if( ssl->session->peer_cert == NULL )
4933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004935 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4936 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004937 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004938 }
4939
4940 if( ssl->session->peer_cert->raw.len !=
4941 ssl->session_negotiate->peer_cert->raw.len ||
4942 memcmp( ssl->session->peer_cert->raw.p,
4943 ssl->session_negotiate->peer_cert->raw.p,
4944 ssl->session->peer_cert->raw.len ) != 0 )
4945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004947 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4948 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004949 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004950 }
4951 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004952#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004953
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004954 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004955 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004956 mbedtls_x509_crt *ca_chain;
4957 mbedtls_x509_crl *ca_crl;
4958
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004959#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004960 if( ssl->handshake->sni_ca_chain != NULL )
4961 {
4962 ca_chain = ssl->handshake->sni_ca_chain;
4963 ca_crl = ssl->handshake->sni_ca_crl;
4964 }
4965 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004966#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004967 {
4968 ca_chain = ssl->conf->ca_chain;
4969 ca_crl = ssl->conf->ca_crl;
4970 }
4971
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004972 /*
4973 * Main check: verify certificate
4974 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004975 ret = mbedtls_x509_crt_verify_with_profile(
4976 ssl->session_negotiate->peer_cert,
4977 ca_chain, ca_crl,
4978 ssl->conf->cert_profile,
4979 ssl->hostname,
4980 &ssl->session_negotiate->verify_result,
4981 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004982
4983 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004986 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004987
4988 /*
4989 * Secondary checks: always done, but change 'ret' only if it was 0
4990 */
4991
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004992#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004994 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004995
4996 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004997 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004998 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004999 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005000 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005003 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005004 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005005 }
5006 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005007#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005009 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005010 ciphersuite_info,
5011 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005012 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005015 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005016 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005017 }
5018
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005019 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5020 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5021 * with details encoded in the verification flags. All other kinds
5022 * of error codes, including those from the user provided f_vrfy
5023 * functions, are treated as fatal and lead to a failure of
5024 * ssl_parse_certificate even if verification was optional. */
5025 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5026 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5027 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5028 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005029 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005030 }
5031
5032 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5033 {
5034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5035 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5036 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005037
5038 if( ret != 0 )
5039 {
5040 /* The certificate may have been rejected for several reasons.
5041 Pick one and send the corresponding alert. Which alert to send
5042 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005043 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5044 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5045 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5046 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5047 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5048 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5049 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5050 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5051 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5052 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5053 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5054 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5055 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5056 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5057 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5058 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5059 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5060 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5061 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5062 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005063 else
5064 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005065 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5066 alert );
5067 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005068
Hanno Beckere6706e62017-05-15 16:05:15 +01005069#if defined(MBEDTLS_DEBUG_C)
5070 if( ssl->session_negotiate->verify_result != 0 )
5071 {
5072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5073 ssl->session_negotiate->verify_result ) );
5074 }
5075 else
5076 {
5077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5078 }
5079#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005080 }
5081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005083
5084 return( ret );
5085}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005086#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5087 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5088 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5089 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5090 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5091 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5092 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005094int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005095{
5096 int ret;
5097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005100 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005101 ssl->out_msglen = 1;
5102 ssl->out_msg[0] = 1;
5103
Paul Bakker5121ce52009-01-03 21:22:43 +00005104 ssl->state++;
5105
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005106 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005107 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005109 return( ret );
5110 }
5111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005113
5114 return( 0 );
5115}
5116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005117int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005118{
5119 int ret;
5120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005123 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005126 return( ret );
5127 }
5128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005129 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005132 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5133 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005134 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005135 }
5136
5137 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005140 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5141 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005142 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005143 }
5144
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005145 /*
5146 * Switch to our negotiated transform and session parameters for inbound
5147 * data.
5148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005150 ssl->transform_in = ssl->transform_negotiate;
5151 ssl->session_in = ssl->session_negotiate;
5152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005153#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005154 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005156#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005157 ssl_dtls_replay_reset( ssl );
5158#endif
5159
5160 /* Increment epoch */
5161 if( ++ssl->in_epoch == 0 )
5162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005164 /* This is highly unlikely to happen for legitimate reasons, so
5165 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005166 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005167 }
5168 }
5169 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005170#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005171 memset( ssl->in_ctr, 0, 8 );
5172
5173 /*
5174 * Set the in_msg pointer to the correct location based on IV length
5175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005176 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005177 {
5178 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
5179 ssl->transform_negotiate->fixed_ivlen;
5180 }
5181 else
5182 ssl->in_msg = ssl->in_iv;
5183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005184#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5185 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005187 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005189 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005190 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5191 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005192 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005193 }
5194 }
5195#endif
5196
Paul Bakker5121ce52009-01-03 21:22:43 +00005197 ssl->state++;
5198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005200
5201 return( 0 );
5202}
5203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005204void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5205 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005206{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005207 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5210 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5211 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005212 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005213 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005214#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5216#if defined(MBEDTLS_SHA512_C)
5217 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005218 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5219 else
5220#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221#if defined(MBEDTLS_SHA256_C)
5222 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005223 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005224 else
5225#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005226#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005229 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005230 }
Paul Bakker380da532012-04-18 16:10:25 +00005231}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005233void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005235#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5236 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005237 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5238 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005239#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005240#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5241#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005242 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005243#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005244#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005245 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005246#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005247#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005248}
5249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005250static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005251 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005252{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005253#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5254 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005255 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5256 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005257#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005258#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5259#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005260 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005261#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005262#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005263 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005264#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005265#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005266}
5267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005268#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5269 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5270static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005271 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005272{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005273 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5274 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005275}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005276#endif
Paul Bakker380da532012-04-18 16:10:25 +00005277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005278#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5279#if defined(MBEDTLS_SHA256_C)
5280static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005281 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005282{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005283 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005284}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005285#endif
Paul Bakker380da532012-04-18 16:10:25 +00005286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005287#if defined(MBEDTLS_SHA512_C)
5288static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005289 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005290{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005291 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005292}
Paul Bakker769075d2012-11-24 11:26:46 +01005293#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005294#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005297static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005298 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005299{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005300 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005301 mbedtls_md5_context md5;
5302 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005303
Paul Bakker5121ce52009-01-03 21:22:43 +00005304 unsigned char padbuf[48];
5305 unsigned char md5sum[16];
5306 unsigned char sha1sum[20];
5307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005309 if( !session )
5310 session = ssl->session;
5311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005313
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005314 mbedtls_md5_init( &md5 );
5315 mbedtls_sha1_init( &sha1 );
5316
5317 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5318 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005319
5320 /*
5321 * SSLv3:
5322 * hash =
5323 * MD5( master + pad2 +
5324 * MD5( handshake + sender + master + pad1 ) )
5325 * + SHA1( master + pad2 +
5326 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005327 */
5328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005329#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005330 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5331 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005332#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005335 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5336 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005337#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005340 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005341
Paul Bakker1ef83d62012-04-11 12:09:53 +00005342 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005343
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005344 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5345 mbedtls_md5_update_ret( &md5, session->master, 48 );
5346 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5347 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005348
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005349 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5350 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5351 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5352 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005353
Paul Bakker1ef83d62012-04-11 12:09:53 +00005354 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005355
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005356 mbedtls_md5_starts_ret( &md5 );
5357 mbedtls_md5_update_ret( &md5, session->master, 48 );
5358 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5359 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5360 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005361
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005362 mbedtls_sha1_starts_ret( &sha1 );
5363 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5364 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5365 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5366 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005369
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005370 mbedtls_md5_free( &md5 );
5371 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005372
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005373 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5374 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
5375 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005378}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005379#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005382static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005384{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005385 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005386 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005387 mbedtls_md5_context md5;
5388 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005389 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005392 if( !session )
5393 session = ssl->session;
5394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005396
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005397 mbedtls_md5_init( &md5 );
5398 mbedtls_sha1_init( &sha1 );
5399
5400 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5401 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005402
Paul Bakker1ef83d62012-04-11 12:09:53 +00005403 /*
5404 * TLSv1:
5405 * hash = PRF( master, finished_label,
5406 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5407 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005409#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005410 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5411 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005412#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005414#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005415 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5416 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005417#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005420 ? "client finished"
5421 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005422
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005423 mbedtls_md5_finish_ret( &md5, padbuf );
5424 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005425
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005426 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005427 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005429 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005430
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005431 mbedtls_md5_free( &md5 );
5432 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005433
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005434 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005437}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005438#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005440#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5441#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005442static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005443 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005444{
5445 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005446 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005447 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005448 unsigned char padbuf[32];
5449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005450 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005451 if( !session )
5452 session = ssl->session;
5453
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005454 mbedtls_sha256_init( &sha256 );
5455
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005457
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005458 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005459
5460 /*
5461 * TLSv1.2:
5462 * hash = PRF( master, finished_label,
5463 * Hash( handshake ) )[0.11]
5464 */
5465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466#if !defined(MBEDTLS_SHA256_ALT)
5467 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005468 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005469#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005472 ? "client finished"
5473 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005474
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005475 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005476
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005477 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005478 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005481
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005482 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005483
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005484 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005491static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005493{
5494 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005495 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005496 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005497 unsigned char padbuf[48];
5498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005500 if( !session )
5501 session = ssl->session;
5502
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005503 mbedtls_sha512_init( &sha512 );
5504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005506
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005507 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005508
5509 /*
5510 * TLSv1.2:
5511 * hash = PRF( master, finished_label,
5512 * Hash( handshake ) )[0.11]
5513 */
5514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005515#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005516 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5517 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005518#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005520 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005521 ? "client finished"
5522 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005523
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005524 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005525
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005526 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005527 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005530
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005531 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005532
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005533 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005536}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537#endif /* MBEDTLS_SHA512_C */
5538#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005540static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005541{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005543
5544 /*
5545 * Free our handshake params
5546 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02005547 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005549 ssl->handshake = NULL;
5550
5551 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005552 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005553 */
5554 if( ssl->transform )
5555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556 mbedtls_ssl_transform_free( ssl->transform );
5557 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005558 }
5559 ssl->transform = ssl->transform_negotiate;
5560 ssl->transform_negotiate = NULL;
5561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005562 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005563}
5564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005566{
5567 int resume = ssl->handshake->resume;
5568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571#if defined(MBEDTLS_SSL_RENEGOTIATION)
5572 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005575 ssl->renego_records_seen = 0;
5576 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005577#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005578
5579 /*
5580 * Free the previous session and switch in the current one
5581 */
Paul Bakker0a597072012-09-25 21:55:46 +00005582 if( ssl->session )
5583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005584#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005585 /* RFC 7366 3.1: keep the EtM state */
5586 ssl->session_negotiate->encrypt_then_mac =
5587 ssl->session->encrypt_then_mac;
5588#endif
5589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590 mbedtls_ssl_session_free( ssl->session );
5591 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005592 }
5593 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005594 ssl->session_negotiate = NULL;
5595
Paul Bakker0a597072012-09-25 21:55:46 +00005596 /*
5597 * Add cache entry
5598 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005599 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005600 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005601 resume == 0 )
5602 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005603 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005605 }
Paul Bakker0a597072012-09-25 21:55:46 +00005606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005608 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005609 ssl->handshake->flight != NULL )
5610 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005611 /* Cancel handshake timer */
5612 ssl_set_timer( ssl, 0 );
5613
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005614 /* Keep last flight around in case we need to resend it:
5615 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005617 }
5618 else
5619#endif
5620 ssl_handshake_wrapup_free_hs_transform( ssl );
5621
Paul Bakker48916f92012-09-16 19:57:18 +00005622 ssl->state++;
5623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005625}
5626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005628{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005629 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005632
Paul Bakker92be97b2013-01-02 17:30:03 +01005633 /*
5634 * Set the out_msg pointer to the correct location based on IV length
5635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005637 {
5638 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5639 ssl->transform_negotiate->fixed_ivlen;
5640 }
5641 else
5642 ssl->out_msg = ssl->out_iv;
5643
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005644 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005645
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005646 /*
5647 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5648 * may define some other value. Currently (early 2016), no defined
5649 * ciphersuite does this (and this is unlikely to change as activity has
5650 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005652 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005655 ssl->verify_data_len = hash_len;
5656 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005657#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005658
Paul Bakker5121ce52009-01-03 21:22:43 +00005659 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005660 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5661 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005662
5663 /*
5664 * In case of session resuming, invert the client and server
5665 * ChangeCipherSpec messages order.
5666 */
Paul Bakker0a597072012-09-25 21:55:46 +00005667 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005669#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005670 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005671 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005672#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005674 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005675 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005676#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005677 }
5678 else
5679 ssl->state++;
5680
Paul Bakker48916f92012-09-16 19:57:18 +00005681 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005682 * Switch to our negotiated transform and session parameters for outbound
5683 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005684 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005688 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005689 {
5690 unsigned char i;
5691
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005692 /* Remember current epoch settings for resending */
5693 ssl->handshake->alt_transform_out = ssl->transform_out;
5694 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5695
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005696 /* Set sequence_number to zero */
5697 memset( ssl->out_ctr + 2, 0, 6 );
5698
5699 /* Increment epoch */
5700 for( i = 2; i > 0; i-- )
5701 if( ++ssl->out_ctr[i - 1] != 0 )
5702 break;
5703
5704 /* The loop goes to its end iff the counter is wrapping */
5705 if( i == 0 )
5706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5708 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005709 }
5710 }
5711 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005713 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005714
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005715 ssl->transform_out = ssl->transform_negotiate;
5716 ssl->session_out = ssl->session_negotiate;
5717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005718#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5719 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005723 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5724 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005725 }
5726 }
5727#endif
5728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005730 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005732#endif
5733
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005734 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005735 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005736 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005737 return( ret );
5738 }
5739
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005740#if defined(MBEDTLS_SSL_PROTO_DTLS)
5741 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5742 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
5743 {
5744 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
5745 return( ret );
5746 }
5747#endif
5748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005750
5751 return( 0 );
5752}
5753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005755#define SSL_MAX_HASH_LEN 36
5756#else
5757#define SSL_MAX_HASH_LEN 12
5758#endif
5759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005760int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005761{
Paul Bakker23986e52011-04-24 08:57:21 +00005762 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005763 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005764 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005767
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005768 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005772 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005773 return( ret );
5774 }
5775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005776 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005779 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5780 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005781 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005782 }
5783
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005784 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785#if defined(MBEDTLS_SSL_PROTO_SSL3)
5786 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005787 hash_len = 36;
5788 else
5789#endif
5790 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005792 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5793 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005796 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5797 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005799 }
5800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005801 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005802 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005805 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5806 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005808 }
5809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005811 ssl->verify_data_len = hash_len;
5812 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005813#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005814
Paul Bakker0a597072012-09-25 21:55:46 +00005815 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005818 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005820#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005822 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005823 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005824#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 }
5826 else
5827 ssl->state++;
5828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005829#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005830 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005832#endif
5833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005835
5836 return( 0 );
5837}
5838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005839static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005840{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005841 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005843#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5844 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5845 mbedtls_md5_init( &handshake->fin_md5 );
5846 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005847 mbedtls_md5_starts_ret( &handshake->fin_md5 );
5848 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005849#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005850#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5851#if defined(MBEDTLS_SHA256_C)
5852 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005853 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005854#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005855#if defined(MBEDTLS_SHA512_C)
5856 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005857 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005858#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005860
5861 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005862
5863#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5864 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5865 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5866#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868#if defined(MBEDTLS_DHM_C)
5869 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005870#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005871#if defined(MBEDTLS_ECDH_C)
5872 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005873#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02005874#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005875 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02005876#if defined(MBEDTLS_SSL_CLI_C)
5877 handshake->ecjpake_cache = NULL;
5878 handshake->ecjpake_cache_len = 0;
5879#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005880#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005881
5882#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5883 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5884#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005885}
5886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005888{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005889 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005891 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5892 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894 mbedtls_md_init( &transform->md_ctx_enc );
5895 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005896}
5897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005898void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005899{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005901}
5902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005903static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005904{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005905 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005906 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005907 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005908 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005910 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02005911 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005912
5913 /*
5914 * Either the pointers are now NULL or cleared properly and can be freed.
5915 * Now allocate missing structures.
5916 */
5917 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005918 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005919 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005920 }
Paul Bakker48916f92012-09-16 19:57:18 +00005921
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005922 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005923 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005924 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005925 }
Paul Bakker48916f92012-09-16 19:57:18 +00005926
Paul Bakker82788fb2014-10-20 13:59:19 +02005927 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005928 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005929 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005930 }
Paul Bakker48916f92012-09-16 19:57:18 +00005931
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005932 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005933 if( ssl->handshake == NULL ||
5934 ssl->transform_negotiate == NULL ||
5935 ssl->session_negotiate == NULL )
5936 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005939 mbedtls_free( ssl->handshake );
5940 mbedtls_free( ssl->transform_negotiate );
5941 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005942
5943 ssl->handshake = NULL;
5944 ssl->transform_negotiate = NULL;
5945 ssl->session_negotiate = NULL;
5946
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005947 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005948 }
5949
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005950 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005951 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005952 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005953 ssl_handshake_params_init( ssl->handshake );
5954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005955#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005956 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5957 {
5958 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005959
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005960 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5961 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5962 else
5963 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005964
5965 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005966 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005967#endif
5968
Paul Bakker48916f92012-09-16 19:57:18 +00005969 return( 0 );
5970}
5971
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005972#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005973/* Dummy cookie callbacks for defaults */
5974static int ssl_cookie_write_dummy( void *ctx,
5975 unsigned char **p, unsigned char *end,
5976 const unsigned char *cli_id, size_t cli_id_len )
5977{
5978 ((void) ctx);
5979 ((void) p);
5980 ((void) end);
5981 ((void) cli_id);
5982 ((void) cli_id_len);
5983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005985}
5986
5987static int ssl_cookie_check_dummy( void *ctx,
5988 const unsigned char *cookie, size_t cookie_len,
5989 const unsigned char *cli_id, size_t cli_id_len )
5990{
5991 ((void) ctx);
5992 ((void) cookie);
5993 ((void) cookie_len);
5994 ((void) cli_id);
5995 ((void) cli_id_len);
5996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005997 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005998}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005999#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006000
Paul Bakker5121ce52009-01-03 21:22:43 +00006001/*
6002 * Initialize an SSL context
6003 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006004void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6005{
6006 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6007}
6008
6009/*
6010 * Setup an SSL context
6011 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006012int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006013 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006014{
Paul Bakker48916f92012-09-16 19:57:18 +00006015 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006016
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006017 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006018
6019 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006020 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006021 */
Angus Grattond8213d02016-05-25 20:56:48 +10006022 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6023 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006024 {
Angus Grattond8213d02016-05-25 20:56:48 +10006025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6026 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6027 }
6028
6029 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6030 if( ssl->out_buf == NULL )
6031 {
6032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006034 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006035 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006036 }
6037
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006038#if defined(MBEDTLS_SSL_PROTO_DTLS)
6039 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6040 {
6041 ssl->out_hdr = ssl->out_buf;
6042 ssl->out_ctr = ssl->out_buf + 3;
6043 ssl->out_len = ssl->out_buf + 11;
6044 ssl->out_iv = ssl->out_buf + 13;
6045 ssl->out_msg = ssl->out_buf + 13;
6046
6047 ssl->in_hdr = ssl->in_buf;
6048 ssl->in_ctr = ssl->in_buf + 3;
6049 ssl->in_len = ssl->in_buf + 11;
6050 ssl->in_iv = ssl->in_buf + 13;
6051 ssl->in_msg = ssl->in_buf + 13;
6052 }
6053 else
6054#endif
6055 {
6056 ssl->out_ctr = ssl->out_buf;
6057 ssl->out_hdr = ssl->out_buf + 8;
6058 ssl->out_len = ssl->out_buf + 11;
6059 ssl->out_iv = ssl->out_buf + 13;
6060 ssl->out_msg = ssl->out_buf + 13;
6061
6062 ssl->in_ctr = ssl->in_buf;
6063 ssl->in_hdr = ssl->in_buf + 8;
6064 ssl->in_len = ssl->in_buf + 11;
6065 ssl->in_iv = ssl->in_buf + 13;
6066 ssl->in_msg = ssl->in_buf + 13;
6067 }
6068
Paul Bakker48916f92012-09-16 19:57:18 +00006069 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6070 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006071
6072 return( 0 );
6073}
6074
6075/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006076 * Reset an initialized and used SSL context for re-use while retaining
6077 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006078 *
6079 * If partial is non-zero, keep data in the input buffer and client ID.
6080 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006081 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006082static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006083{
Paul Bakker48916f92012-09-16 19:57:18 +00006084 int ret;
6085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006086 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006087
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006088 /* Cancel any possibly running timer */
6089 ssl_set_timer( ssl, 0 );
6090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006091#if defined(MBEDTLS_SSL_RENEGOTIATION)
6092 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006093 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006094
6095 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6097 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006098#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006099 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006100
Paul Bakker7eb013f2011-10-06 12:37:39 +00006101 ssl->in_offt = NULL;
6102
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006103 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006104 ssl->in_msgtype = 0;
6105 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006106 if( partial == 0 )
6107 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006109 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006110 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006111#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006113 ssl_dtls_replay_reset( ssl );
6114#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006115
6116 ssl->in_hslen = 0;
6117 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006118
6119 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006120
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006121 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006122 ssl->out_msgtype = 0;
6123 ssl->out_msglen = 0;
6124 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006125#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6126 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006127 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006128#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006129
Paul Bakker48916f92012-09-16 19:57:18 +00006130 ssl->transform_in = NULL;
6131 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006132
Hanno Becker78640902018-08-13 16:35:15 +01006133 ssl->session_in = NULL;
6134 ssl->session_out = NULL;
6135
Angus Grattond8213d02016-05-25 20:56:48 +10006136 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006137 if( partial == 0 )
Angus Grattond8213d02016-05-25 20:56:48 +10006138 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00006139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6141 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6144 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006146 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6147 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006148 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006149 }
6150#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006151
Paul Bakker48916f92012-09-16 19:57:18 +00006152 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154 mbedtls_ssl_transform_free( ssl->transform );
6155 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006156 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006157 }
Paul Bakker48916f92012-09-16 19:57:18 +00006158
Paul Bakkerc0463502013-02-14 11:19:38 +01006159 if( ssl->session )
6160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161 mbedtls_ssl_session_free( ssl->session );
6162 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006163 ssl->session = NULL;
6164 }
6165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006166#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006167 ssl->alpn_chosen = NULL;
6168#endif
6169
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006170#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006171 if( partial == 0 )
6172 {
6173 mbedtls_free( ssl->cli_id );
6174 ssl->cli_id = NULL;
6175 ssl->cli_id_len = 0;
6176 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006177#endif
6178
Paul Bakker48916f92012-09-16 19:57:18 +00006179 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6180 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006181
6182 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006183}
6184
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006185/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006186 * Reset an initialized and used SSL context for re-use while retaining
6187 * all application-set variables, function pointers and data.
6188 */
6189int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6190{
6191 return( ssl_session_reset_int( ssl, 0 ) );
6192}
6193
6194/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006195 * SSL set accessors
6196 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006197void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006198{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006199 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006200}
6201
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006202void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006203{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006204 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006205}
6206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006207#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006208void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006209{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006210 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006211}
6212#endif
6213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006214#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006215void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006216{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006217 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006218}
6219#endif
6220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006222void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006223{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006224 conf->hs_timeout_min = min;
6225 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006226}
6227#endif
6228
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006229void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006230{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006231 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006232}
6233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006235void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006236 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006237 void *p_vrfy )
6238{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006239 conf->f_vrfy = f_vrfy;
6240 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006243
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006244void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006245 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006246 void *p_rng )
6247{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006248 conf->f_rng = f_rng;
6249 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006250}
6251
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006252void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006253 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006254 void *p_dbg )
6255{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006256 conf->f_dbg = f_dbg;
6257 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006258}
6259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006260void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006261 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006262 mbedtls_ssl_send_t *f_send,
6263 mbedtls_ssl_recv_t *f_recv,
6264 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006265{
6266 ssl->p_bio = p_bio;
6267 ssl->f_send = f_send;
6268 ssl->f_recv = f_recv;
6269 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006270}
6271
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02006272#if defined(MBEDTLS_SSL_PROTO_DTLS)
6273void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
6274{
6275 ssl->mtu = mtu;
6276}
6277#endif
6278
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006279void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006280{
6281 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006282}
6283
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006284void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6285 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006286 mbedtls_ssl_set_timer_t *f_set_timer,
6287 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006288{
6289 ssl->p_timer = p_timer;
6290 ssl->f_set_timer = f_set_timer;
6291 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006292
6293 /* Make sure we start with no timer running */
6294 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006295}
6296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006298void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006299 void *p_cache,
6300 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6301 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006302{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006303 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006304 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006305 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006306}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006307#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006309#if defined(MBEDTLS_SSL_CLI_C)
6310int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006311{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006312 int ret;
6313
6314 if( ssl == NULL ||
6315 session == NULL ||
6316 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006317 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006320 }
6321
6322 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6323 return( ret );
6324
Paul Bakker0a597072012-09-25 21:55:46 +00006325 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006326
6327 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006328}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006330
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006331void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006332 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006333{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006334 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6335 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6336 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6337 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006338}
6339
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006340void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006341 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006342 int major, int minor )
6343{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006344 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006345 return;
6346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006348 return;
6349
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006350 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006351}
6352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006353#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006354void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006355 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006356{
6357 conf->cert_profile = profile;
6358}
6359
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006360/* Append a new keycert entry to a (possibly empty) list */
6361static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6362 mbedtls_x509_crt *cert,
6363 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006364{
niisato8ee24222018-06-25 19:05:48 +09006365 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006366
niisato8ee24222018-06-25 19:05:48 +09006367 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6368 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006369 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006370
niisato8ee24222018-06-25 19:05:48 +09006371 new_cert->cert = cert;
6372 new_cert->key = key;
6373 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006374
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006375 /* Update head is the list was null, else add to the end */
6376 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006377 {
niisato8ee24222018-06-25 19:05:48 +09006378 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006379 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006380 else
6381 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006382 mbedtls_ssl_key_cert *cur = *head;
6383 while( cur->next != NULL )
6384 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09006385 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006386 }
6387
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006388 return( 0 );
6389}
6390
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006391int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006392 mbedtls_x509_crt *own_cert,
6393 mbedtls_pk_context *pk_key )
6394{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006395 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006396}
6397
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006398void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006399 mbedtls_x509_crt *ca_chain,
6400 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006401{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006402 conf->ca_chain = ca_chain;
6403 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006405#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006406
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006407#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6408int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6409 mbedtls_x509_crt *own_cert,
6410 mbedtls_pk_context *pk_key )
6411{
6412 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6413 own_cert, pk_key ) );
6414}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006415
6416void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6417 mbedtls_x509_crt *ca_chain,
6418 mbedtls_x509_crl *ca_crl )
6419{
6420 ssl->handshake->sni_ca_chain = ca_chain;
6421 ssl->handshake->sni_ca_crl = ca_crl;
6422}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006423
6424void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6425 int authmode )
6426{
6427 ssl->handshake->sni_authmode = authmode;
6428}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006429#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6430
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006431#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006432/*
6433 * Set EC J-PAKE password for current handshake
6434 */
6435int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6436 const unsigned char *pw,
6437 size_t pw_len )
6438{
6439 mbedtls_ecjpake_role role;
6440
Janos Follath8eb64132016-06-03 15:40:57 +01006441 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006442 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6443
6444 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6445 role = MBEDTLS_ECJPAKE_SERVER;
6446 else
6447 role = MBEDTLS_ECJPAKE_CLIENT;
6448
6449 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6450 role,
6451 MBEDTLS_MD_SHA256,
6452 MBEDTLS_ECP_DP_SECP256R1,
6453 pw, pw_len ) );
6454}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006455#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006458int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006459 const unsigned char *psk, size_t psk_len,
6460 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006461{
Paul Bakker6db455e2013-09-18 17:29:31 +02006462 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006463 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6466 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006467
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006468 /* Identity len will be encoded on two bytes */
6469 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10006470 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006471 {
6472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6473 }
6474
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006475 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006476 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006477 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006478
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006479 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006480 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006481 conf->psk_len = 0;
6482 }
6483 if( conf->psk_identity != NULL )
6484 {
6485 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006486 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006487 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006488 }
6489
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006490 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6491 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006492 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006493 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006494 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006495 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006496 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006497 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006498 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006499
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006500 conf->psk_len = psk_len;
6501 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006502
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006503 memcpy( conf->psk, psk, conf->psk_len );
6504 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006505
6506 return( 0 );
6507}
6508
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006509int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6510 const unsigned char *psk, size_t psk_len )
6511{
6512 if( psk == NULL || ssl->handshake == NULL )
6513 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6514
6515 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6516 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6517
6518 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006519 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006520 mbedtls_platform_zeroize( ssl->handshake->psk,
6521 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006522 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006523 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006524 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006525
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006526 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006527 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006528
6529 ssl->handshake->psk_len = psk_len;
6530 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6531
6532 return( 0 );
6533}
6534
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006535void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006537 size_t),
6538 void *p_psk )
6539{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006540 conf->f_psk = f_psk;
6541 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006542}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006544
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006545#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006546
6547#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006548int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006549{
6550 int ret;
6551
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006552 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6553 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6554 {
6555 mbedtls_mpi_free( &conf->dhm_P );
6556 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006557 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006558 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006559
6560 return( 0 );
6561}
Hanno Becker470a8c42017-10-04 15:28:46 +01006562#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006563
Hanno Beckera90658f2017-10-04 15:29:08 +01006564int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6565 const unsigned char *dhm_P, size_t P_len,
6566 const unsigned char *dhm_G, size_t G_len )
6567{
6568 int ret;
6569
6570 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6571 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6572 {
6573 mbedtls_mpi_free( &conf->dhm_P );
6574 mbedtls_mpi_free( &conf->dhm_G );
6575 return( ret );
6576 }
6577
6578 return( 0 );
6579}
Paul Bakker5121ce52009-01-03 21:22:43 +00006580
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006581int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006582{
6583 int ret;
6584
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006585 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6586 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6587 {
6588 mbedtls_mpi_free( &conf->dhm_P );
6589 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006590 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006591 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006592
6593 return( 0 );
6594}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006595#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006596
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006597#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6598/*
6599 * Set the minimum length for Diffie-Hellman parameters
6600 */
6601void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6602 unsigned int bitlen )
6603{
6604 conf->dhm_min_bitlen = bitlen;
6605}
6606#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6607
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006608#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006609/*
6610 * Set allowed/preferred hashes for handshake signatures
6611 */
6612void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6613 const int *hashes )
6614{
6615 conf->sig_hashes = hashes;
6616}
Hanno Becker947194e2017-04-07 13:25:49 +01006617#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006618
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006619#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006620/*
6621 * Set the allowed elliptic curves
6622 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006623void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006624 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006625{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006626 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006627}
Hanno Becker947194e2017-04-07 13:25:49 +01006628#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006629
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006630#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006632{
Hanno Becker947194e2017-04-07 13:25:49 +01006633 /* Initialize to suppress unnecessary compiler warning */
6634 size_t hostname_len = 0;
6635
6636 /* Check if new hostname is valid before
6637 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006638 if( hostname != NULL )
6639 {
6640 hostname_len = strlen( hostname );
6641
6642 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6644 }
6645
6646 /* Now it's clear that we will overwrite the old hostname,
6647 * so we can free it safely */
6648
6649 if( ssl->hostname != NULL )
6650 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006651 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01006652 mbedtls_free( ssl->hostname );
6653 }
6654
6655 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006656
Paul Bakker5121ce52009-01-03 21:22:43 +00006657 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006658 {
6659 ssl->hostname = NULL;
6660 }
6661 else
6662 {
6663 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006664 if( ssl->hostname == NULL )
6665 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006666
Hanno Becker947194e2017-04-07 13:25:49 +01006667 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006668
Hanno Becker947194e2017-04-07 13:25:49 +01006669 ssl->hostname[hostname_len] = '\0';
6670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006671
6672 return( 0 );
6673}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006674#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006675
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006676#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006677void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006679 const unsigned char *, size_t),
6680 void *p_sni )
6681{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006682 conf->f_sni = f_sni;
6683 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006684}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006688int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006689{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006690 size_t cur_len, tot_len;
6691 const char **p;
6692
6693 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006694 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6695 * MUST NOT be truncated."
6696 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006697 */
6698 tot_len = 0;
6699 for( p = protos; *p != NULL; p++ )
6700 {
6701 cur_len = strlen( *p );
6702 tot_len += cur_len;
6703
6704 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006706 }
6707
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006708 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006709
6710 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006711}
6712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006714{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006715 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006716}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006718
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006719void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006720{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006721 conf->max_major_ver = major;
6722 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006723}
6724
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006725void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006726{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006727 conf->min_major_ver = major;
6728 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006729}
6730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006731#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006732void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006733{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006734 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006735}
6736#endif
6737
Janos Follath088ce432017-04-10 12:42:31 +01006738#if defined(MBEDTLS_SSL_SRV_C)
6739void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6740 char cert_req_ca_list )
6741{
6742 conf->cert_req_ca_list = cert_req_ca_list;
6743}
6744#endif
6745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006747void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006748{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006749 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006750}
6751#endif
6752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006754void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006755{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006756 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006757}
6758#endif
6759
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006760#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006761void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006762{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006763 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006764}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006765#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006768int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006769{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006770 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10006771 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006774 }
6775
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006776 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006777
6778 return( 0 );
6779}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006783void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006784{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006785 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006786}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006790void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006791{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006792 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006793}
6794#endif
6795
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006796void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006797{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006798 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006799}
6800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006802void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006803{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006804 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006805}
6806
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006807void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006808{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006809 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006810}
6811
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006812void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006813 const unsigned char period[8] )
6814{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006815 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006816}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006820#if defined(MBEDTLS_SSL_CLI_C)
6821void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006822{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006823 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006824}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006825#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006826
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006827#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006828void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6829 mbedtls_ssl_ticket_write_t *f_ticket_write,
6830 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6831 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006832{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006833 conf->f_ticket_write = f_ticket_write;
6834 conf->f_ticket_parse = f_ticket_parse;
6835 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006836}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006837#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006839
Robert Cragie4feb7ae2015-10-02 13:33:37 +01006840#if defined(MBEDTLS_SSL_EXPORT_KEYS)
6841void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
6842 mbedtls_ssl_export_keys_t *f_export_keys,
6843 void *p_export_keys )
6844{
6845 conf->f_export_keys = f_export_keys;
6846 conf->p_export_keys = p_export_keys;
6847}
6848#endif
6849
Gilles Peskineb74a1c72018-04-24 13:09:22 +02006850#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006851void mbedtls_ssl_conf_async_private_cb(
6852 mbedtls_ssl_config *conf,
6853 mbedtls_ssl_async_sign_t *f_async_sign,
6854 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
6855 mbedtls_ssl_async_resume_t *f_async_resume,
6856 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006857 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006858{
6859 conf->f_async_sign_start = f_async_sign;
6860 conf->f_async_decrypt_start = f_async_decrypt;
6861 conf->f_async_resume = f_async_resume;
6862 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006863 conf->p_async_config_data = async_config_data;
6864}
6865
Gilles Peskine8f97af72018-04-26 11:46:10 +02006866void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
6867{
6868 return( conf->p_async_config_data );
6869}
6870
Gilles Peskine1febfef2018-04-30 11:54:39 +02006871void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006872{
6873 if( ssl->handshake == NULL )
6874 return( NULL );
6875 else
6876 return( ssl->handshake->user_async_ctx );
6877}
6878
Gilles Peskine1febfef2018-04-30 11:54:39 +02006879void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006880 void *ctx )
6881{
6882 if( ssl->handshake != NULL )
6883 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006884}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02006885#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006886
Paul Bakker5121ce52009-01-03 21:22:43 +00006887/*
6888 * SSL get accessors
6889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006891{
6892 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6893}
6894
Hanno Becker8b170a02017-10-10 11:51:19 +01006895int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
6896{
6897 /*
6898 * Case A: We're currently holding back
6899 * a message for further processing.
6900 */
6901
6902 if( ssl->keep_current_message == 1 )
6903 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006905 return( 1 );
6906 }
6907
6908 /*
6909 * Case B: Further records are pending in the current datagram.
6910 */
6911
6912#if defined(MBEDTLS_SSL_PROTO_DTLS)
6913 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6914 ssl->in_left > ssl->next_record_offset )
6915 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006916 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006917 return( 1 );
6918 }
6919#endif /* MBEDTLS_SSL_PROTO_DTLS */
6920
6921 /*
6922 * Case C: A handshake message is being processed.
6923 */
6924
Hanno Becker8b170a02017-10-10 11:51:19 +01006925 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
6926 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006928 return( 1 );
6929 }
6930
6931 /*
6932 * Case D: An application data message is being processed
6933 */
6934 if( ssl->in_offt != NULL )
6935 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006936 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006937 return( 1 );
6938 }
6939
6940 /*
6941 * In all other cases, the rest of the message can be dropped.
6942 * As in ssl_read_record_layer, this needs to be adapted if
6943 * we implement support for multiple alerts in single records.
6944 */
6945
6946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
6947 return( 0 );
6948}
6949
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006950uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006951{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006952 if( ssl->session != NULL )
6953 return( ssl->session->verify_result );
6954
6955 if( ssl->session_negotiate != NULL )
6956 return( ssl->session_negotiate->verify_result );
6957
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006958 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006959}
6960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006962{
Paul Bakker926c8e42013-03-06 10:23:34 +01006963 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006964 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006967}
6968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006970{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006972 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006973 {
6974 switch( ssl->minor_ver )
6975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006977 return( "DTLSv1.0" );
6978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006980 return( "DTLSv1.2" );
6981
6982 default:
6983 return( "unknown (DTLS)" );
6984 }
6985 }
6986#endif
6987
Paul Bakker43ca69c2011-01-15 17:35:19 +00006988 switch( ssl->minor_ver )
6989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006991 return( "SSLv3.0" );
6992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006994 return( "TLSv1.0" );
6995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006997 return( "TLSv1.1" );
6998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007000 return( "TLSv1.2" );
7001
Paul Bakker43ca69c2011-01-15 17:35:19 +00007002 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007003 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007004 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007005}
7006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007008{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007009 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007011 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007012
Hanno Becker78640902018-08-13 16:35:15 +01007013 if( transform == NULL )
7014 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016#if defined(MBEDTLS_ZLIB_SUPPORT)
7017 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7018 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007019 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007020#endif
7021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024 case MBEDTLS_MODE_GCM:
7025 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007026 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007027 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007028 transform_expansion = transform->minlen;
7029 break;
7030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007032
7033 block_size = mbedtls_cipher_get_block_size(
7034 &transform->cipher_ctx_enc );
7035
7036#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7037 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7038 {
7039 /* Expansion due to addition of
7040 * - MAC
7041 * - CBC padding (theoretically up to 256 bytes, but
7042 * we never use more than block_size)
7043 * - explicit IV
7044 */
7045 transform_expansion = transform->maclen + 2 * block_size;
7046 }
7047 else
7048#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7049 {
7050 /* No explicit IV prior to TLS 1.1. */
7051 transform_expansion = transform->maclen + block_size;
7052 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007053 break;
7054
7055 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007058 }
7059
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007060 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007061}
7062
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007063#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7064size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7065{
7066 size_t max_len;
7067
7068 /*
7069 * Assume mfl_code is correct since it was checked when set
7070 */
Angus Grattond8213d02016-05-25 20:56:48 +10007071 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007072
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007073 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007074 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007075 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007076 {
Angus Grattond8213d02016-05-25 20:56:48 +10007077 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007078 }
7079
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007080 /* During a handshake, use the value being negotiated */
7081 if( ssl->session_negotiate != NULL &&
7082 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7083 {
7084 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7085 }
7086
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007087 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007088}
7089#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7090
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007091int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7092{
7093 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7094
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02007095#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7096 !defined(MBEDTLS_SSL_PROTO_DTLS)
7097 (void) ssl;
7098#endif
7099
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007100#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7101 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7102
7103 if( max_len > mfl )
7104 max_len = mfl;
7105#endif
7106
7107#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007108 if( ssl->mtu != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007109 {
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007110 const size_t mtu = ssl->mtu;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007111 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7112 const size_t overhead = (size_t) ret;
7113
7114 if( ret < 0 )
7115 return( ret );
7116
7117 if( mtu <= overhead )
7118 {
7119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7120 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7121 }
7122
7123 if( max_len > mtu - overhead )
7124 max_len = mtu - overhead;
7125 }
7126#endif
7127
7128 return( (int) max_len );
7129}
7130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131#if defined(MBEDTLS_X509_CRT_PARSE_C)
7132const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007133{
7134 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007135 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007136
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007137 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007138}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141#if defined(MBEDTLS_SSL_CLI_C)
7142int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007143{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007144 if( ssl == NULL ||
7145 dst == NULL ||
7146 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007147 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007150 }
7151
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007152 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007153}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007155
Paul Bakker5121ce52009-01-03 21:22:43 +00007156/*
Paul Bakker1961b702013-01-25 14:49:24 +01007157 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007162
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007163 if( ssl == NULL || ssl->conf == NULL )
7164 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007167 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007169#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007171 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007173#endif
7174
Paul Bakker1961b702013-01-25 14:49:24 +01007175 return( ret );
7176}
7177
7178/*
7179 * Perform the SSL handshake
7180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007181int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007182{
7183 int ret = 0;
7184
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007185 if( ssl == NULL || ssl->conf == NULL )
7186 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007190 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007193
7194 if( ret != 0 )
7195 break;
7196 }
7197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007199
7200 return( ret );
7201}
7202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203#if defined(MBEDTLS_SSL_RENEGOTIATION)
7204#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007205/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007206 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007209{
7210 int ret;
7211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007213
7214 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7216 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007217
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007218 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007219 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007221 return( ret );
7222 }
7223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007225
7226 return( 0 );
7227}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007229
7230/*
7231 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232 * - any side: calling mbedtls_ssl_renegotiate(),
7233 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7234 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007235 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007236 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007237 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007239static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007240{
7241 int ret;
7242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007244
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007245 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7246 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007247
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007248 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7249 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007251 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007253 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007254 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007255 ssl->handshake->out_msg_seq = 1;
7256 else
7257 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007258 }
7259#endif
7260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7262 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007267 return( ret );
7268 }
7269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007271
7272 return( 0 );
7273}
7274
7275/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007276 * Renegotiate current connection on client,
7277 * or request renegotiation on server
7278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007282
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007283 if( ssl == NULL || ssl->conf == NULL )
7284 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007287 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007288 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007294
7295 /* Did we already try/start sending HelloRequest? */
7296 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007298
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007299 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007300 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007304 /*
7305 * On client, either start the renegotiation process or,
7306 * if already in progress, continue the handshake
7307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007312
7313 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007315 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007316 return( ret );
7317 }
7318 }
7319 else
7320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007324 return( ret );
7325 }
7326 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007328
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007329 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007330}
7331
7332/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007333 * Check record counters and renegotiate if they're above the limit.
7334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007335static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007336{
Andres AG2196c7f2016-12-15 17:01:16 +00007337 size_t ep_len = ssl_ep_len( ssl );
7338 int in_ctr_cmp;
7339 int out_ctr_cmp;
7340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7342 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007343 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007344 {
7345 return( 0 );
7346 }
7347
Andres AG2196c7f2016-12-15 17:01:16 +00007348 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7349 ssl->conf->renego_period + ep_len, 8 - ep_len );
7350 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
7351 ssl->conf->renego_period + ep_len, 8 - ep_len );
7352
7353 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007354 {
7355 return( 0 );
7356 }
7357
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007360}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007361#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007362
7363/*
7364 * Receive application data decrypted from the SSL layer
7365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007367{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007368 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007369 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007370
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007371 if( ssl == NULL || ssl->conf == NULL )
7372 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007377 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007380 return( ret );
7381
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007382 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007384 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007385 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007386 return( ret );
7387 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007388 }
7389#endif
7390
Hanno Becker4a810fb2017-05-24 16:27:30 +01007391 /*
7392 * Check if renegotiation is necessary and/or handshake is
7393 * in process. If yes, perform/continue, and fall through
7394 * if an unexpected packet is received while the client
7395 * is waiting for the ServerHello.
7396 *
7397 * (There is no equivalent to the last condition on
7398 * the server-side as it is not treated as within
7399 * a handshake while waiting for the ClientHello
7400 * after a renegotiation request.)
7401 */
7402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007404 ret = ssl_check_ctr_renegotiate( ssl );
7405 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7406 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007409 return( ret );
7410 }
7411#endif
7412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007416 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7417 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007420 return( ret );
7421 }
7422 }
7423
Hanno Beckere41158b2017-10-23 13:30:32 +01007424 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01007425 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007426 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007427 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007428 if( ssl->f_get_timer != NULL &&
7429 ssl->f_get_timer( ssl->p_timer ) == -1 )
7430 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007431 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007432 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007433
Hanno Becker4a810fb2017-05-24 16:27:30 +01007434 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007435 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007436 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7437 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007438
Hanno Becker4a810fb2017-05-24 16:27:30 +01007439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7440 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007441 }
7442
7443 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007445 {
7446 /*
7447 * OpenSSL sends empty messages to randomize the IV
7448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007452 return( 0 );
7453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007455 return( ret );
7456 }
7457 }
7458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007462
Hanno Becker4a810fb2017-05-24 16:27:30 +01007463 /*
7464 * - For client-side, expect SERVER_HELLO_REQUEST.
7465 * - For server-side, expect CLIENT_HELLO.
7466 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7467 */
7468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007470 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007472 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007475
7476 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007478 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007479 {
7480 continue;
7481 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007482#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007484 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007485#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007486
Hanno Becker4a810fb2017-05-24 16:27:30 +01007487#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007488 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007492
7493 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007494#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007495 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007496 {
7497 continue;
7498 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007499#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007501 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007502#endif /* MBEDTLS_SSL_SRV_C */
7503
Hanno Becker21df7f92017-10-17 11:03:26 +01007504#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007505 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007506 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7507 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7508 ssl->conf->allow_legacy_renegotiation ==
7509 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7510 {
7511 /*
7512 * Accept renegotiation request
7513 */
Paul Bakker48916f92012-09-16 19:57:18 +00007514
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007515 /* DTLS clients need to know renego is server-initiated */
7516#if defined(MBEDTLS_SSL_PROTO_DTLS)
7517 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7518 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7519 {
7520 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7521 }
7522#endif
7523 ret = ssl_start_renegotiation( ssl );
7524 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7525 ret != 0 )
7526 {
7527 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7528 return( ret );
7529 }
7530 }
7531 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007532#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007533 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007534 /*
7535 * Refuse renegotiation
7536 */
7537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540#if defined(MBEDTLS_SSL_PROTO_SSL3)
7541 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007542 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007543 /* SSLv3 does not have a "no_renegotiation" warning, so
7544 we send a fatal alert and abort the connection. */
7545 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7546 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7547 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007548 }
7549 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007550#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7551#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7552 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7553 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007555 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7556 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7557 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007558 {
7559 return( ret );
7560 }
Paul Bakker48916f92012-09-16 19:57:18 +00007561 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007562 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7564 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7567 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007568 }
Paul Bakker48916f92012-09-16 19:57:18 +00007569 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007570
Hanno Becker90333da2017-10-10 11:27:13 +01007571 /* At this point, we don't know whether the renegotiation has been
7572 * completed or not. The cases to consider are the following:
7573 * 1) The renegotiation is complete. In this case, no new record
7574 * has been read yet.
7575 * 2) The renegotiation is incomplete because the client received
7576 * an application data record while awaiting the ServerHello.
7577 * 3) The renegotiation is incomplete because the client received
7578 * a non-handshake, non-application data message while awaiting
7579 * the ServerHello.
7580 * In each of these case, looping will be the proper action:
7581 * - For 1), the next iteration will read a new record and check
7582 * if it's application data.
7583 * - For 2), the loop condition isn't satisfied as application data
7584 * is present, hence continue is the same as break
7585 * - For 3), the loop condition is satisfied and read_record
7586 * will re-deliver the message that was held back by the client
7587 * when expecting the ServerHello.
7588 */
7589 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00007590 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007591#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007593 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007594 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007595 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007596 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007599 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007601 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007602 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007603 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7607 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007610 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007611 }
7612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7616 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007617 }
7618
7619 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007620
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007621 /* We're going to return something now, cancel timer,
7622 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007624 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007625
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007626#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007627 /* If we requested renego but received AppData, resend HelloRequest.
7628 * Do it now, after setting in_offt, to avoid taking this branch
7629 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007631 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007633 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007634 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007637 return( ret );
7638 }
7639 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007641#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007642 }
7643
7644 n = ( len < ssl->in_msglen )
7645 ? len : ssl->in_msglen;
7646
7647 memcpy( buf, ssl->in_offt, n );
7648 ssl->in_msglen -= n;
7649
7650 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007651 {
7652 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007653 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007654 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007656 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007657 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007658 /* more data available */
7659 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007660 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007663
Paul Bakker23986e52011-04-24 08:57:21 +00007664 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007665}
7666
7667/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007668 * Send application data to be encrypted by the SSL layer, taking care of max
7669 * fragment length and buffer size.
7670 *
7671 * According to RFC 5246 Section 6.2.1:
7672 *
7673 * Zero-length fragments of Application data MAY be sent as they are
7674 * potentially useful as a traffic analysis countermeasure.
7675 *
7676 * Therefore, it is possible that the input message length is 0 and the
7677 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007678 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007679static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007680 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007681{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007682 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
7683 const size_t max_len = (size_t) ret;
7684
7685 if( ret < 0 )
7686 {
7687 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
7688 return( ret );
7689 }
7690
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007691 if( len > max_len )
7692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007694 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007697 "maximum fragment length: %d > %d",
7698 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007699 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007700 }
7701 else
7702#endif
7703 len = max_len;
7704 }
Paul Bakker887bd502011-06-08 13:10:54 +00007705
Paul Bakker5121ce52009-01-03 21:22:43 +00007706 if( ssl->out_left != 0 )
7707 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007708 /*
7709 * The user has previously tried to send the data and
7710 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7711 * written. In this case, we expect the high-level write function
7712 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007717 return( ret );
7718 }
7719 }
Paul Bakker887bd502011-06-08 13:10:54 +00007720 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007721 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007722 /*
7723 * The user is trying to send a message the first time, so we need to
7724 * copy the data into the internal buffers and setup the data structure
7725 * to keep track of partial writes
7726 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007727 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007729 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007733 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007734 return( ret );
7735 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007736 }
7737
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007738 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007739}
7740
7741/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007742 * Write application data, doing 1/n-1 splitting if necessary.
7743 *
7744 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007745 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01007746 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007749static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007750 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007751{
7752 int ret;
7753
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007754 if( ssl->conf->cbc_record_splitting ==
7755 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007756 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7758 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7759 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007760 {
7761 return( ssl_write_real( ssl, buf, len ) );
7762 }
7763
7764 if( ssl->split_done == 0 )
7765 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007766 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007767 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007768 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007769 }
7770
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007771 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7772 return( ret );
7773 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007774
7775 return( ret + 1 );
7776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007778
7779/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007780 * Write application data (public-facing wrapper)
7781 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007782int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007783{
7784 int ret;
7785
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007787
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007788 if( ssl == NULL || ssl->conf == NULL )
7789 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7790
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007791#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007792 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7793 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007794 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007795 return( ret );
7796 }
7797#endif
7798
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007799 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007800 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007801 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007802 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007803 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007804 return( ret );
7805 }
7806 }
7807
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007808#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007809 ret = ssl_write_split( ssl, buf, len );
7810#else
7811 ret = ssl_write_real( ssl, buf, len );
7812#endif
7813
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007815
7816 return( ret );
7817}
7818
7819/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007820 * Notify the peer that the connection is being closed
7821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007822int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007823{
7824 int ret;
7825
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007826 if( ssl == NULL || ssl->conf == NULL )
7827 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007830
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007831 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7837 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7838 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007841 return( ret );
7842 }
7843 }
7844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007846
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007847 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007848}
7849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00007851{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007852 if( transform == NULL )
7853 return;
7854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00007856 deflateEnd( &transform->ctx_deflate );
7857 inflateEnd( &transform->ctx_inflate );
7858#endif
7859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860 mbedtls_cipher_free( &transform->cipher_ctx_enc );
7861 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02007862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863 mbedtls_md_free( &transform->md_ctx_enc );
7864 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02007865
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007866 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007867}
7868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869#if defined(MBEDTLS_X509_CRT_PARSE_C)
7870static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007871{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007873
7874 while( cur != NULL )
7875 {
7876 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007878 cur = next;
7879 }
7880}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007882
Gilles Peskine9b562d52018-04-25 20:32:43 +02007883void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007884{
Gilles Peskine9b562d52018-04-25 20:32:43 +02007885 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
7886
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007887 if( handshake == NULL )
7888 return;
7889
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007890#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
7891 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
7892 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02007893 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007894 handshake->async_in_progress = 0;
7895 }
7896#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
7897
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007898#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7899 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7900 mbedtls_md5_free( &handshake->fin_md5 );
7901 mbedtls_sha1_free( &handshake->fin_sha1 );
7902#endif
7903#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7904#if defined(MBEDTLS_SHA256_C)
7905 mbedtls_sha256_free( &handshake->fin_sha256 );
7906#endif
7907#if defined(MBEDTLS_SHA512_C)
7908 mbedtls_sha512_free( &handshake->fin_sha512 );
7909#endif
7910#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912#if defined(MBEDTLS_DHM_C)
7913 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007914#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915#if defined(MBEDTLS_ECDH_C)
7916 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007917#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007918#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007919 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007920#if defined(MBEDTLS_SSL_CLI_C)
7921 mbedtls_free( handshake->ecjpake_cache );
7922 handshake->ecjpake_cache = NULL;
7923 handshake->ecjpake_cache_len = 0;
7924#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007925#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02007926
Janos Follath4ae5c292016-02-10 11:27:43 +00007927#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
7928 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02007929 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007930 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007931#endif
7932
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007933#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7934 if( handshake->psk != NULL )
7935 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007936 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007937 mbedtls_free( handshake->psk );
7938 }
7939#endif
7940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7942 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007943 /*
7944 * Free only the linked list wrapper, not the keys themselves
7945 * since the belong to the SNI callback
7946 */
7947 if( handshake->sni_key_cert != NULL )
7948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007950
7951 while( cur != NULL )
7952 {
7953 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007955 cur = next;
7956 }
7957 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960#if defined(MBEDTLS_SSL_PROTO_DTLS)
7961 mbedtls_free( handshake->verify_cookie );
7962 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007963 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007964#endif
7965
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007966 mbedtls_platform_zeroize( handshake,
7967 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007968}
7969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007971{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007972 if( session == NULL )
7973 return;
7974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007976 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007978 mbedtls_x509_crt_free( session->peer_cert );
7979 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007980 }
Paul Bakkered27a042013-04-18 22:46:23 +02007981#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007982
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007983#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007985#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007986
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007987 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007988}
7989
Paul Bakker5121ce52009-01-03 21:22:43 +00007990/*
7991 * Free an SSL context
7992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007994{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007995 if( ssl == NULL )
7996 return;
7997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007999
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008000 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008001 {
Angus Grattond8213d02016-05-25 20:56:48 +10008002 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008004 }
8005
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008006 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008007 {
Angus Grattond8213d02016-05-25 20:56:48 +10008008 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008010 }
8011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008013 if( ssl->compress_buf != NULL )
8014 {
Angus Grattond8213d02016-05-25 20:56:48 +10008015 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008017 }
8018#endif
8019
Paul Bakker48916f92012-09-16 19:57:18 +00008020 if( ssl->transform )
8021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022 mbedtls_ssl_transform_free( ssl->transform );
8023 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008024 }
8025
8026 if( ssl->handshake )
8027 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008028 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008029 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8030 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032 mbedtls_free( ssl->handshake );
8033 mbedtls_free( ssl->transform_negotiate );
8034 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008035 }
8036
Paul Bakkerc0463502013-02-14 11:19:38 +01008037 if( ssl->session )
8038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008039 mbedtls_ssl_session_free( ssl->session );
8040 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008041 }
8042
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008043#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008044 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008045 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008046 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008047 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008048 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8052 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8055 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008056 }
8057#endif
8058
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008059#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008061#endif
8062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008064
Paul Bakker86f04f42013-02-14 11:20:09 +01008065 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008066 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008067}
8068
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008069/*
8070 * Initialze mbedtls_ssl_config
8071 */
8072void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8073{
8074 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8075}
8076
Simon Butcherc97b6972015-12-27 23:48:17 +00008077#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008078static int ssl_preset_default_hashes[] = {
8079#if defined(MBEDTLS_SHA512_C)
8080 MBEDTLS_MD_SHA512,
8081 MBEDTLS_MD_SHA384,
8082#endif
8083#if defined(MBEDTLS_SHA256_C)
8084 MBEDTLS_MD_SHA256,
8085 MBEDTLS_MD_SHA224,
8086#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008087#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008088 MBEDTLS_MD_SHA1,
8089#endif
8090 MBEDTLS_MD_NONE
8091};
Simon Butcherc97b6972015-12-27 23:48:17 +00008092#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008093
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008094static int ssl_preset_suiteb_ciphersuites[] = {
8095 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8096 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8097 0
8098};
8099
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008100#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008101static int ssl_preset_suiteb_hashes[] = {
8102 MBEDTLS_MD_SHA256,
8103 MBEDTLS_MD_SHA384,
8104 MBEDTLS_MD_NONE
8105};
8106#endif
8107
8108#if defined(MBEDTLS_ECP_C)
8109static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8110 MBEDTLS_ECP_DP_SECP256R1,
8111 MBEDTLS_ECP_DP_SECP384R1,
8112 MBEDTLS_ECP_DP_NONE
8113};
8114#endif
8115
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008116/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008117 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008118 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008119int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008120 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008121{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008122#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008123 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008124#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008125
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008126 /* Use the functions here so that they are covered in tests,
8127 * but otherwise access member directly for efficiency */
8128 mbedtls_ssl_conf_endpoint( conf, endpoint );
8129 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008130
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008131 /*
8132 * Things that are common to all presets
8133 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008134#if defined(MBEDTLS_SSL_CLI_C)
8135 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8136 {
8137 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8138#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8139 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8140#endif
8141 }
8142#endif
8143
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008144#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008145 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008146#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008147
8148#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8149 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8150#endif
8151
8152#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8153 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8154#endif
8155
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008156#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8157 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8158#endif
8159
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008160#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008161 conf->f_cookie_write = ssl_cookie_write_dummy;
8162 conf->f_cookie_check = ssl_cookie_check_dummy;
8163#endif
8164
8165#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8166 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8167#endif
8168
Janos Follath088ce432017-04-10 12:42:31 +01008169#if defined(MBEDTLS_SSL_SRV_C)
8170 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8171#endif
8172
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008173#if defined(MBEDTLS_SSL_PROTO_DTLS)
8174 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8175 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8176#endif
8177
8178#if defined(MBEDTLS_SSL_RENEGOTIATION)
8179 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008180 memset( conf->renego_period, 0x00, 2 );
8181 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008182#endif
8183
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008184#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8185 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8186 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008187 const unsigned char dhm_p[] =
8188 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8189 const unsigned char dhm_g[] =
8190 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8191
Hanno Beckera90658f2017-10-04 15:29:08 +01008192 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8193 dhm_p, sizeof( dhm_p ),
8194 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008195 {
8196 return( ret );
8197 }
8198 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008199#endif
8200
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008201 /*
8202 * Preset-specific defaults
8203 */
8204 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008205 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008206 /*
8207 * NSA Suite B
8208 */
8209 case MBEDTLS_SSL_PRESET_SUITEB:
8210 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8211 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8212 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8213 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8214
8215 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8216 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8217 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8218 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8219 ssl_preset_suiteb_ciphersuites;
8220
8221#if defined(MBEDTLS_X509_CRT_PARSE_C)
8222 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008223#endif
8224
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008225#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008226 conf->sig_hashes = ssl_preset_suiteb_hashes;
8227#endif
8228
8229#if defined(MBEDTLS_ECP_C)
8230 conf->curve_list = ssl_preset_suiteb_curves;
8231#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008232 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008233
8234 /*
8235 * Default
8236 */
8237 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008238 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8239 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8240 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8241 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8242 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8243 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8244 MBEDTLS_SSL_MIN_MINOR_VERSION :
8245 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008246 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8247 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8248
8249#if defined(MBEDTLS_SSL_PROTO_DTLS)
8250 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8251 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8252#endif
8253
8254 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8255 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8256 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8257 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8258 mbedtls_ssl_list_ciphersuites();
8259
8260#if defined(MBEDTLS_X509_CRT_PARSE_C)
8261 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8262#endif
8263
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008264#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008265 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008266#endif
8267
8268#if defined(MBEDTLS_ECP_C)
8269 conf->curve_list = mbedtls_ecp_grp_id_list();
8270#endif
8271
8272#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8273 conf->dhm_min_bitlen = 1024;
8274#endif
8275 }
8276
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008277 return( 0 );
8278}
8279
8280/*
8281 * Free mbedtls_ssl_config
8282 */
8283void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8284{
8285#if defined(MBEDTLS_DHM_C)
8286 mbedtls_mpi_free( &conf->dhm_P );
8287 mbedtls_mpi_free( &conf->dhm_G );
8288#endif
8289
8290#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8291 if( conf->psk != NULL )
8292 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008293 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008294 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00008295 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008296 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09008297 }
8298
8299 if( conf->psk_identity != NULL )
8300 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008301 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09008302 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00008303 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008304 conf->psk_identity_len = 0;
8305 }
8306#endif
8307
8308#if defined(MBEDTLS_X509_CRT_PARSE_C)
8309 ssl_key_cert_free( conf->key_cert );
8310#endif
8311
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008312 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008313}
8314
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008315#if defined(MBEDTLS_PK_C) && \
8316 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008317/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008321{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322#if defined(MBEDTLS_RSA_C)
8323 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8324 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008325#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326#if defined(MBEDTLS_ECDSA_C)
8327 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8328 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008329#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008331}
8332
Hanno Becker7e5437a2017-04-28 17:15:26 +01008333unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8334{
8335 switch( type ) {
8336 case MBEDTLS_PK_RSA:
8337 return( MBEDTLS_SSL_SIG_RSA );
8338 case MBEDTLS_PK_ECDSA:
8339 case MBEDTLS_PK_ECKEY:
8340 return( MBEDTLS_SSL_SIG_ECDSA );
8341 default:
8342 return( MBEDTLS_SSL_SIG_ANON );
8343 }
8344}
8345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008347{
8348 switch( sig )
8349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350#if defined(MBEDTLS_RSA_C)
8351 case MBEDTLS_SSL_SIG_RSA:
8352 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008353#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354#if defined(MBEDTLS_ECDSA_C)
8355 case MBEDTLS_SSL_SIG_ECDSA:
8356 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008357#endif
8358 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008360 }
8361}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008362#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008363
Hanno Becker7e5437a2017-04-28 17:15:26 +01008364#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8365 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8366
8367/* Find an entry in a signature-hash set matching a given hash algorithm. */
8368mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8369 mbedtls_pk_type_t sig_alg )
8370{
8371 switch( sig_alg )
8372 {
8373 case MBEDTLS_PK_RSA:
8374 return( set->rsa );
8375 case MBEDTLS_PK_ECDSA:
8376 return( set->ecdsa );
8377 default:
8378 return( MBEDTLS_MD_NONE );
8379 }
8380}
8381
8382/* Add a signature-hash-pair to a signature-hash set */
8383void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8384 mbedtls_pk_type_t sig_alg,
8385 mbedtls_md_type_t md_alg )
8386{
8387 switch( sig_alg )
8388 {
8389 case MBEDTLS_PK_RSA:
8390 if( set->rsa == MBEDTLS_MD_NONE )
8391 set->rsa = md_alg;
8392 break;
8393
8394 case MBEDTLS_PK_ECDSA:
8395 if( set->ecdsa == MBEDTLS_MD_NONE )
8396 set->ecdsa = md_alg;
8397 break;
8398
8399 default:
8400 break;
8401 }
8402}
8403
8404/* Allow exactly one hash algorithm for each signature. */
8405void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8406 mbedtls_md_type_t md_alg )
8407{
8408 set->rsa = md_alg;
8409 set->ecdsa = md_alg;
8410}
8411
8412#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8413 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8414
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008415/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008416 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008419{
8420 switch( hash )
8421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422#if defined(MBEDTLS_MD5_C)
8423 case MBEDTLS_SSL_HASH_MD5:
8424 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008425#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008426#if defined(MBEDTLS_SHA1_C)
8427 case MBEDTLS_SSL_HASH_SHA1:
8428 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008429#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008430#if defined(MBEDTLS_SHA256_C)
8431 case MBEDTLS_SSL_HASH_SHA224:
8432 return( MBEDTLS_MD_SHA224 );
8433 case MBEDTLS_SSL_HASH_SHA256:
8434 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008435#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008436#if defined(MBEDTLS_SHA512_C)
8437 case MBEDTLS_SSL_HASH_SHA384:
8438 return( MBEDTLS_MD_SHA384 );
8439 case MBEDTLS_SSL_HASH_SHA512:
8440 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008441#endif
8442 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008444 }
8445}
8446
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008447/*
8448 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8449 */
8450unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8451{
8452 switch( md )
8453 {
8454#if defined(MBEDTLS_MD5_C)
8455 case MBEDTLS_MD_MD5:
8456 return( MBEDTLS_SSL_HASH_MD5 );
8457#endif
8458#if defined(MBEDTLS_SHA1_C)
8459 case MBEDTLS_MD_SHA1:
8460 return( MBEDTLS_SSL_HASH_SHA1 );
8461#endif
8462#if defined(MBEDTLS_SHA256_C)
8463 case MBEDTLS_MD_SHA224:
8464 return( MBEDTLS_SSL_HASH_SHA224 );
8465 case MBEDTLS_MD_SHA256:
8466 return( MBEDTLS_SSL_HASH_SHA256 );
8467#endif
8468#if defined(MBEDTLS_SHA512_C)
8469 case MBEDTLS_MD_SHA384:
8470 return( MBEDTLS_SSL_HASH_SHA384 );
8471 case MBEDTLS_MD_SHA512:
8472 return( MBEDTLS_SSL_HASH_SHA512 );
8473#endif
8474 default:
8475 return( MBEDTLS_SSL_HASH_NONE );
8476 }
8477}
8478
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008479#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008480/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008481 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008482 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008483 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008484int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008485{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008486 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008487
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008488 if( ssl->conf->curve_list == NULL )
8489 return( -1 );
8490
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008491 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008492 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008493 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008494
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008495 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008496}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008497#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008498
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008499#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008500/*
8501 * Check if a hash proposed by the peer is in our list.
8502 * Return 0 if we're willing to use it, -1 otherwise.
8503 */
8504int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8505 mbedtls_md_type_t md )
8506{
8507 const int *cur;
8508
8509 if( ssl->conf->sig_hashes == NULL )
8510 return( -1 );
8511
8512 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8513 if( *cur == (int) md )
8514 return( 0 );
8515
8516 return( -1 );
8517}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008518#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520#if defined(MBEDTLS_X509_CRT_PARSE_C)
8521int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8522 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008523 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008524 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008525{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008526 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008528 int usage = 0;
8529#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008530#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008531 const char *ext_oid;
8532 size_t ext_len;
8533#endif
8534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008535#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8536 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008537 ((void) cert);
8538 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008539 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008540#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008542#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8543 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008544 {
8545 /* Server part of the key exchange */
8546 switch( ciphersuite->key_exchange )
8547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008548 case MBEDTLS_KEY_EXCHANGE_RSA:
8549 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008550 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008551 break;
8552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008553 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8554 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8555 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8556 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008557 break;
8558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008559 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8560 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008561 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008562 break;
8563
8564 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008565 case MBEDTLS_KEY_EXCHANGE_NONE:
8566 case MBEDTLS_KEY_EXCHANGE_PSK:
8567 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8568 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008569 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008570 usage = 0;
8571 }
8572 }
8573 else
8574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008575 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8576 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008577 }
8578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008579 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008580 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008581 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008582 ret = -1;
8583 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008584#else
8585 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008586#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008588#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8589 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8592 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008593 }
8594 else
8595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8597 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008598 }
8599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008601 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008602 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008603 ret = -1;
8604 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008606
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008607 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008608}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008610
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008611/*
8612 * Convert version numbers to/from wire format
8613 * and, for DTLS, to/from TLS equivalent.
8614 *
8615 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008616 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008617 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8618 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008621 unsigned char ver[2] )
8622{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623#if defined(MBEDTLS_SSL_PROTO_DTLS)
8624 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008626 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008627 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8628
8629 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8630 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8631 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008632 else
8633#else
8634 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008635#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008636 {
8637 ver[0] = (unsigned char) major;
8638 ver[1] = (unsigned char) minor;
8639 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008640}
8641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008642void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008643 const unsigned char ver[2] )
8644{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645#if defined(MBEDTLS_SSL_PROTO_DTLS)
8646 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008647 {
8648 *major = 255 - ver[0] + 2;
8649 *minor = 255 - ver[1] + 1;
8650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008651 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008652 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8653 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008654 else
8655#else
8656 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008657#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008658 {
8659 *major = ver[0];
8660 *minor = ver[1];
8661 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008662}
8663
Simon Butcher99000142016-10-13 17:21:01 +01008664int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8665{
8666#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8667 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8668 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8669
8670 switch( md )
8671 {
8672#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8673#if defined(MBEDTLS_MD5_C)
8674 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008675 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008676#endif
8677#if defined(MBEDTLS_SHA1_C)
8678 case MBEDTLS_SSL_HASH_SHA1:
8679 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8680 break;
8681#endif
8682#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8683#if defined(MBEDTLS_SHA512_C)
8684 case MBEDTLS_SSL_HASH_SHA384:
8685 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8686 break;
8687#endif
8688#if defined(MBEDTLS_SHA256_C)
8689 case MBEDTLS_SSL_HASH_SHA256:
8690 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8691 break;
8692#endif
8693 default:
8694 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8695 }
8696
8697 return 0;
8698#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8699 (void) ssl;
8700 (void) md;
8701
8702 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8703#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8704}
8705
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008706#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8707 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8708int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8709 unsigned char *output,
8710 unsigned char *data, size_t data_len )
8711{
8712 int ret = 0;
8713 mbedtls_md5_context mbedtls_md5;
8714 mbedtls_sha1_context mbedtls_sha1;
8715
8716 mbedtls_md5_init( &mbedtls_md5 );
8717 mbedtls_sha1_init( &mbedtls_sha1 );
8718
8719 /*
8720 * digitally-signed struct {
8721 * opaque md5_hash[16];
8722 * opaque sha_hash[20];
8723 * };
8724 *
8725 * md5_hash
8726 * MD5(ClientHello.random + ServerHello.random
8727 * + ServerParams);
8728 * sha_hash
8729 * SHA(ClientHello.random + ServerHello.random
8730 * + ServerParams);
8731 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008732 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008733 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008734 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008735 goto exit;
8736 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008737 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008738 ssl->handshake->randbytes, 64 ) ) != 0 )
8739 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008740 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008741 goto exit;
8742 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008743 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008744 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008745 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008746 goto exit;
8747 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008748 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008749 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008751 goto exit;
8752 }
8753
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008754 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008755 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008756 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008757 goto exit;
8758 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008759 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008760 ssl->handshake->randbytes, 64 ) ) != 0 )
8761 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008762 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008763 goto exit;
8764 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008765 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008766 data_len ) ) != 0 )
8767 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008768 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008769 goto exit;
8770 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008771 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008772 output + 16 ) ) != 0 )
8773 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008774 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008775 goto exit;
8776 }
8777
8778exit:
8779 mbedtls_md5_free( &mbedtls_md5 );
8780 mbedtls_sha1_free( &mbedtls_sha1 );
8781
8782 if( ret != 0 )
8783 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8784 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8785
8786 return( ret );
8787
8788}
8789#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
8790 MBEDTLS_SSL_PROTO_TLS1_1 */
8791
8792#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8793 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8794int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02008795 unsigned char *hash, size_t *hashlen,
8796 unsigned char *data, size_t data_len,
8797 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008798{
8799 int ret = 0;
8800 mbedtls_md_context_t ctx;
8801 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02008802 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008803
8804 mbedtls_md_init( &ctx );
8805
8806 /*
8807 * digitally-signed struct {
8808 * opaque client_random[32];
8809 * opaque server_random[32];
8810 * ServerDHParams params;
8811 * };
8812 */
8813 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8814 {
8815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8816 goto exit;
8817 }
8818 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8819 {
8820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8821 goto exit;
8822 }
8823 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8824 {
8825 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8826 goto exit;
8827 }
8828 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8829 {
8830 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8831 goto exit;
8832 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02008833 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008834 {
8835 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8836 goto exit;
8837 }
8838
8839exit:
8840 mbedtls_md_free( &ctx );
8841
8842 if( ret != 0 )
8843 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8844 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8845
8846 return( ret );
8847}
8848#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
8849 MBEDTLS_SSL_PROTO_TLS1_2 */
8850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008851#endif /* MBEDTLS_SSL_TLS_C */