blob: d22b0e228d1f6b8a99294912551a5ea6f18190dc [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 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003052 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003053 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3054 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003055 /* In SSLv3, the client might send a NoCertificate alert. */
3056#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3057 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3058 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3059 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3060#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3061 {
3062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3063 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3064 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003065 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003066
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003067 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3068 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3069 ssl->handshake == NULL )
3070 {
3071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3073 }
3074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003076 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003077 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003079 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3081 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003082 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003083#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003084
3085 /*
3086 * Fill handshake headers
3087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003089 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003090 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3091 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3092 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003093
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003094 /*
3095 * DTLS has additional fields in the Handshake layer,
3096 * between the length field and the actual payload:
3097 * uint16 message_seq;
3098 * uint24 fragment_offset;
3099 * uint24 fragment_length;
3100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003102 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003103 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003104 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003105 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003106 {
3107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3108 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003109 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003110 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003111 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3112 }
3113
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003114 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003115 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003116
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003117 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003118 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003119 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003120 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3121 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3122 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003123 }
3124 else
3125 {
3126 ssl->out_msg[4] = 0;
3127 ssl->out_msg[5] = 0;
3128 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003129
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003130 /* Handshake hashes are computed without fragmentation,
3131 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003132 memset( ssl->out_msg + 6, 0x00, 3 );
3133 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003134 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003136
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003137 /* Update running hashes of hanshake messages seen */
3138 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3139 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003140 }
3141
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003142 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003144 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003145 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003146 {
3147 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003150 return( ret );
3151 }
3152 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003153 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003154#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003155 {
3156 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
3157 {
3158 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3159 return( ret );
3160 }
3161 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003162
3163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3164
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003165 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003166}
3167
3168/*
3169 * Record layer functions
3170 */
3171
3172/*
3173 * Write current record.
3174 *
3175 * Uses:
3176 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3177 * - ssl->out_msglen: length of the record content (excl headers)
3178 * - ssl->out_msg: record content
3179 */
3180int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
3181{
3182 int ret, done = 0;
3183 size_t len = ssl->out_msglen;
3184
3185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003188 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003190 {
3191 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003194 return( ret );
3195 }
3196
3197 len = ssl->out_msglen;
3198 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3202 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 ret = mbedtls_ssl_hw_record_write( ssl );
3207 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3210 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003211 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003212
3213 if( ret == 0 )
3214 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003215 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003217 if( !done )
3218 {
3219 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003221 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003222
3223 ssl->out_len[0] = (unsigned char)( len >> 8 );
3224 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003225
Paul Bakker48916f92012-09-16 19:57:18 +00003226 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003227 {
3228 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003231 return( ret );
3232 }
3233
3234 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003235 ssl->out_len[0] = (unsigned char)( len >> 8 );
3236 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003237 }
3238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00003240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00003242 "version = [%d:%d], msglen = %d",
3243 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003244 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3247 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003248 }
3249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003253 return( ret );
3254 }
3255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003257
3258 return( 0 );
3259}
3260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003262/*
3263 * Mark bits in bitmask (used for DTLS HS reassembly)
3264 */
3265static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3266{
3267 unsigned int start_bits, end_bits;
3268
3269 start_bits = 8 - ( offset % 8 );
3270 if( start_bits != 8 )
3271 {
3272 size_t first_byte_idx = offset / 8;
3273
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003274 /* Special case */
3275 if( len <= start_bits )
3276 {
3277 for( ; len != 0; len-- )
3278 mask[first_byte_idx] |= 1 << ( start_bits - len );
3279
3280 /* Avoid potential issues with offset or len becoming invalid */
3281 return;
3282 }
3283
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003284 offset += start_bits; /* Now offset % 8 == 0 */
3285 len -= start_bits;
3286
3287 for( ; start_bits != 0; start_bits-- )
3288 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3289 }
3290
3291 end_bits = len % 8;
3292 if( end_bits != 0 )
3293 {
3294 size_t last_byte_idx = ( offset + len ) / 8;
3295
3296 len -= end_bits; /* Now len % 8 == 0 */
3297
3298 for( ; end_bits != 0; end_bits-- )
3299 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3300 }
3301
3302 memset( mask + offset / 8, 0xFF, len / 8 );
3303}
3304
3305/*
3306 * Check that bitmask is full
3307 */
3308static int ssl_bitmask_check( unsigned char *mask, size_t len )
3309{
3310 size_t i;
3311
3312 for( i = 0; i < len / 8; i++ )
3313 if( mask[i] != 0xFF )
3314 return( -1 );
3315
3316 for( i = 0; i < len % 8; i++ )
3317 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3318 return( -1 );
3319
3320 return( 0 );
3321}
3322
3323/*
3324 * Reassemble fragmented DTLS handshake messages.
3325 *
3326 * Use a temporary buffer for reassembly, divided in two parts:
3327 * - the first holds the reassembled message (including handshake header),
3328 * - the second holds a bitmask indicating which parts of the message
3329 * (excluding headers) have been received so far.
3330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003332{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003333 unsigned char *msg, *bitmask;
3334 size_t frag_len, frag_off;
3335 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3336
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003337 if( ssl->handshake == NULL )
3338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3340 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003341 }
3342
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003343 /*
3344 * For first fragment, check size and allocate buffer
3345 */
3346 if( ssl->handshake->hs_msg == NULL )
3347 {
3348 size_t alloc_len;
3349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003351 msg_len ) );
3352
Angus Grattond8213d02016-05-25 20:56:48 +10003353 if( ssl->in_hslen > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3356 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003357 }
3358
3359 /* The bitmask needs one bit per byte of message excluding header */
3360 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3361
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003362 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003363 if( ssl->handshake->hs_msg == NULL )
3364 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003366 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003367 }
3368
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003369 /* Prepare final header: copy msg_type, length and message_seq,
3370 * then add standardised fragment_offset and fragment_length */
3371 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3372 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3373 memcpy( ssl->handshake->hs_msg + 9,
3374 ssl->handshake->hs_msg + 1, 3 );
3375 }
3376 else
3377 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003378 /* Make sure msg_type and length are consistent */
3379 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3382 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003383 }
3384 }
3385
3386 msg = ssl->handshake->hs_msg + 12;
3387 bitmask = msg + msg_len;
3388
3389 /*
3390 * Check and copy current fragment
3391 */
3392 frag_off = ( ssl->in_msg[6] << 16 ) |
3393 ( ssl->in_msg[7] << 8 ) |
3394 ssl->in_msg[8];
3395 frag_len = ( ssl->in_msg[9] << 16 ) |
3396 ( ssl->in_msg[10] << 8 ) |
3397 ssl->in_msg[11];
3398
3399 if( frag_off + frag_len > msg_len )
3400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003402 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003404 }
3405
3406 if( frag_len + 12 > ssl->in_msglen )
3407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003409 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003411 }
3412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003414 frag_off, frag_len ) );
3415
3416 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3417 ssl_bitmask_set( bitmask, frag_off, frag_len );
3418
3419 /*
3420 * Do we have the complete message by now?
3421 * If yes, finalize it, else ask to read the next record.
3422 */
3423 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01003426 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003427 }
3428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003430
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003431 if( frag_len + 12 < ssl->in_msglen )
3432 {
3433 /*
3434 * We'got more handshake messages in the same record.
3435 * This case is not handled now because no know implementation does
3436 * that and it's hard to test, so we prefer to fail cleanly for now.
3437 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3439 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003440 }
3441
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003442 if( ssl->in_left > ssl->next_record_offset )
3443 {
3444 /*
3445 * We've got more data in the buffer after the current record,
3446 * that we don't want to overwrite. Move it before writing the
3447 * reassembled message, and adjust in_left and next_record_offset.
3448 */
3449 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3450 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3451 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3452
3453 /* First compute and check new lengths */
3454 ssl->next_record_offset = new_remain - ssl->in_hdr;
3455 ssl->in_left = ssl->next_record_offset + remain_len;
3456
Angus Grattond8213d02016-05-25 20:56:48 +10003457 if( ssl->in_left > MBEDTLS_SSL_IN_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003458 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3461 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003462 }
3463
3464 memmove( new_remain, cur_remain, remain_len );
3465 }
3466
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003467 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003470 ssl->handshake->hs_msg = NULL;
3471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003473 ssl->in_msg, ssl->in_hslen );
3474
3475 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003476}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003478
Simon Butcher99000142016-10-13 17:21:01 +01003479int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003480{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003484 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003486 }
3487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003489 ( ssl->in_msg[1] << 16 ) |
3490 ( ssl->in_msg[2] << 8 ) |
3491 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003494 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003495 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003498 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003499 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003500 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003501 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003502
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003503 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003504 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3505 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3506 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3507 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003508 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003509 /* Retransmit only on last message from previous flight, to avoid
3510 * too many retransmissions.
3511 * Besides, No sane server ever retransmits HelloVerifyRequest */
3512 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003516 "message_seq = %d, start_of_flight = %d",
3517 recv_msg_seq,
3518 ssl->handshake->in_flight_start_seq ) );
3519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003523 return( ret );
3524 }
3525 }
3526 else
3527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003529 "message_seq = %d, expected = %d",
3530 recv_msg_seq,
3531 ssl->handshake->in_msg_seq ) );
3532 }
3533
Hanno Becker90333da2017-10-10 11:27:13 +01003534 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003535 }
3536 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003537
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003538 /* Reassemble if current message is fragmented or reassembly is
3539 * already in progress */
3540 if( ssl->in_msglen < ssl->in_hslen ||
3541 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3542 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3543 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003546
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003547 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003550 return( ret );
3551 }
3552 }
3553 }
3554 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003556 /* With TLS we don't handle fragmentation (for now) */
3557 if( ssl->in_msglen < ssl->in_hslen )
3558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3560 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003561 }
3562
Simon Butcher99000142016-10-13 17:21:01 +01003563 return( 0 );
3564}
3565
3566void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3567{
3568
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003569 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3570 ssl->handshake != NULL )
3571 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003572 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003573 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003574
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003575 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003577 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003578 ssl->handshake != NULL )
3579 {
3580 ssl->handshake->in_msg_seq++;
3581 }
3582#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003583}
3584
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003585/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003586 * DTLS anti-replay: RFC 6347 4.1.2.6
3587 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003588 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3589 * Bit n is set iff record number in_window_top - n has been seen.
3590 *
3591 * Usually, in_window_top is the last record number seen and the lsb of
3592 * in_window is set. The only exception is the initial state (record number 0
3593 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3596static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003597{
3598 ssl->in_window_top = 0;
3599 ssl->in_window = 0;
3600}
3601
3602static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3603{
3604 return( ( (uint64_t) buf[0] << 40 ) |
3605 ( (uint64_t) buf[1] << 32 ) |
3606 ( (uint64_t) buf[2] << 24 ) |
3607 ( (uint64_t) buf[3] << 16 ) |
3608 ( (uint64_t) buf[4] << 8 ) |
3609 ( (uint64_t) buf[5] ) );
3610}
3611
3612/*
3613 * Return 0 if sequence number is acceptable, -1 otherwise
3614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003616{
3617 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3618 uint64_t bit;
3619
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003620 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003621 return( 0 );
3622
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003623 if( rec_seqnum > ssl->in_window_top )
3624 return( 0 );
3625
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003626 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003627
3628 if( bit >= 64 )
3629 return( -1 );
3630
3631 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3632 return( -1 );
3633
3634 return( 0 );
3635}
3636
3637/*
3638 * Update replay window on new validated record
3639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003641{
3642 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3643
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003644 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003645 return;
3646
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003647 if( rec_seqnum > ssl->in_window_top )
3648 {
3649 /* Update window_top and the contents of the window */
3650 uint64_t shift = rec_seqnum - ssl->in_window_top;
3651
3652 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003653 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003654 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003655 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003656 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003657 ssl->in_window |= 1;
3658 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003659
3660 ssl->in_window_top = rec_seqnum;
3661 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003662 else
3663 {
3664 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003665 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003666
3667 if( bit < 64 ) /* Always true, but be extra sure */
3668 ssl->in_window |= (uint64_t) 1 << bit;
3669 }
3670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003672
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003673#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003674/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003675static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3676
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003677/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003678 * Without any SSL context, check if a datagram looks like a ClientHello with
3679 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003680 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003681 *
3682 * - if cookie is valid, return 0
3683 * - if ClientHello looks superficially valid but cookie is not,
3684 * fill obuf and set olen, then
3685 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3686 * - otherwise return a specific error code
3687 */
3688static int ssl_check_dtls_clihlo_cookie(
3689 mbedtls_ssl_cookie_write_t *f_cookie_write,
3690 mbedtls_ssl_cookie_check_t *f_cookie_check,
3691 void *p_cookie,
3692 const unsigned char *cli_id, size_t cli_id_len,
3693 const unsigned char *in, size_t in_len,
3694 unsigned char *obuf, size_t buf_len, size_t *olen )
3695{
3696 size_t sid_len, cookie_len;
3697 unsigned char *p;
3698
3699 if( f_cookie_write == NULL || f_cookie_check == NULL )
3700 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3701
3702 /*
3703 * Structure of ClientHello with record and handshake headers,
3704 * and expected values. We don't need to check a lot, more checks will be
3705 * done when actually parsing the ClientHello - skipping those checks
3706 * avoids code duplication and does not make cookie forging any easier.
3707 *
3708 * 0-0 ContentType type; copied, must be handshake
3709 * 1-2 ProtocolVersion version; copied
3710 * 3-4 uint16 epoch; copied, must be 0
3711 * 5-10 uint48 sequence_number; copied
3712 * 11-12 uint16 length; (ignored)
3713 *
3714 * 13-13 HandshakeType msg_type; (ignored)
3715 * 14-16 uint24 length; (ignored)
3716 * 17-18 uint16 message_seq; copied
3717 * 19-21 uint24 fragment_offset; copied, must be 0
3718 * 22-24 uint24 fragment_length; (ignored)
3719 *
3720 * 25-26 ProtocolVersion client_version; (ignored)
3721 * 27-58 Random random; (ignored)
3722 * 59-xx SessionID session_id; 1 byte len + sid_len content
3723 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3724 * ...
3725 *
3726 * Minimum length is 61 bytes.
3727 */
3728 if( in_len < 61 ||
3729 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3730 in[3] != 0 || in[4] != 0 ||
3731 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3732 {
3733 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3734 }
3735
3736 sid_len = in[59];
3737 if( sid_len > in_len - 61 )
3738 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3739
3740 cookie_len = in[60 + sid_len];
3741 if( cookie_len > in_len - 60 )
3742 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3743
3744 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3745 cli_id, cli_id_len ) == 0 )
3746 {
3747 /* Valid cookie */
3748 return( 0 );
3749 }
3750
3751 /*
3752 * If we get here, we've got an invalid cookie, let's prepare HVR.
3753 *
3754 * 0-0 ContentType type; copied
3755 * 1-2 ProtocolVersion version; copied
3756 * 3-4 uint16 epoch; copied
3757 * 5-10 uint48 sequence_number; copied
3758 * 11-12 uint16 length; olen - 13
3759 *
3760 * 13-13 HandshakeType msg_type; hello_verify_request
3761 * 14-16 uint24 length; olen - 25
3762 * 17-18 uint16 message_seq; copied
3763 * 19-21 uint24 fragment_offset; copied
3764 * 22-24 uint24 fragment_length; olen - 25
3765 *
3766 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3767 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3768 *
3769 * Minimum length is 28.
3770 */
3771 if( buf_len < 28 )
3772 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3773
3774 /* Copy most fields and adapt others */
3775 memcpy( obuf, in, 25 );
3776 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3777 obuf[25] = 0xfe;
3778 obuf[26] = 0xff;
3779
3780 /* Generate and write actual cookie */
3781 p = obuf + 28;
3782 if( f_cookie_write( p_cookie,
3783 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3784 {
3785 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3786 }
3787
3788 *olen = p - obuf;
3789
3790 /* Go back and fill length fields */
3791 obuf[27] = (unsigned char)( *olen - 28 );
3792
3793 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3794 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3795 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3796
3797 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3798 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3799
3800 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3801}
3802
3803/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003804 * Handle possible client reconnect with the same UDP quadruplet
3805 * (RFC 6347 Section 4.2.8).
3806 *
3807 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3808 * that looks like a ClientHello.
3809 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003810 * - if the input looks like a ClientHello without cookies,
3811 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003812 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003813 * - if the input looks like a ClientHello with a valid cookie,
3814 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003815 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003816 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003817 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003818 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003819 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3820 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003821 */
3822static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3823{
3824 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003825 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003826
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003827 ret = ssl_check_dtls_clihlo_cookie(
3828 ssl->conf->f_cookie_write,
3829 ssl->conf->f_cookie_check,
3830 ssl->conf->p_cookie,
3831 ssl->cli_id, ssl->cli_id_len,
3832 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003833 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003834
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003835 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3836
3837 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003838 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003839 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003840 * If the error is permanent we'll catch it later,
3841 * if it's not, then hopefully it'll work next time. */
3842 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3843
3844 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003845 }
3846
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003847 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003848 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003849 /* Got a valid cookie, partially reset context */
3850 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3851 {
3852 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3853 return( ret );
3854 }
3855
3856 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003857 }
3858
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003859 return( ret );
3860}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003861#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003862
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003863/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003864 * ContentType type;
3865 * ProtocolVersion version;
3866 * uint16 epoch; // DTLS only
3867 * uint48 sequence_number; // DTLS only
3868 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003869 *
3870 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003871 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003872 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3873 *
3874 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003875 * 1. proceed with the record if this function returns 0
3876 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3877 * 3. return CLIENT_RECONNECT if this function return that value
3878 * 4. drop the whole datagram if this function returns anything else.
3879 * Point 2 is needed when the peer is resending, and we have already received
3880 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003882static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003883{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003884 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003886 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 +02003887
Paul Bakker5121ce52009-01-03 21:22:43 +00003888 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003889 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003890 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003893 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003894 ssl->in_msgtype,
3895 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003896
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003897 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3899 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3900 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3901 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003904
3905#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01003906 /* Silently ignore invalid DTLS records as recommended by RFC 6347
3907 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003908 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3909#endif /* MBEDTLS_SSL_PROTO_DTLS */
3910 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3911 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
3912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003913 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003914 }
3915
3916 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003917 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3920 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003921 }
3922
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003923 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3926 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003927 }
3928
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003929 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10003930 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003931 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3934 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003935 }
3936
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003937 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003938 * DTLS-related tests.
3939 * Check epoch before checking length constraint because
3940 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3941 * message gets duplicated before the corresponding Finished message,
3942 * the second ChangeCipherSpec should be discarded because it belongs
3943 * to an old epoch, but not because its length is shorter than
3944 * the minimum record length for packets using the new record transform.
3945 * Note that these two kinds of failures are handled differently,
3946 * as an unexpected record is silently skipped but an invalid
3947 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003948 */
3949#if defined(MBEDTLS_SSL_PROTO_DTLS)
3950 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3951 {
3952 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3953
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003954 /* Check epoch (and sequence number) with DTLS */
3955 if( rec_epoch != ssl->in_epoch )
3956 {
3957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3958 "expected %d, received %d",
3959 ssl->in_epoch, rec_epoch ) );
3960
3961#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3962 /*
3963 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3964 * access the first byte of record content (handshake type), as we
3965 * have an active transform (possibly iv_len != 0), so use the
3966 * fact that the record header len is 13 instead.
3967 */
3968 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3969 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3970 rec_epoch == 0 &&
3971 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3972 ssl->in_left > 13 &&
3973 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3974 {
3975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3976 "from the same port" ) );
3977 return( ssl_handle_possible_reconnect( ssl ) );
3978 }
3979 else
3980#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3981 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3982 }
3983
3984#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3985 /* Replay detection only works for the current epoch */
3986 if( rec_epoch == ssl->in_epoch &&
3987 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3988 {
3989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3990 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3991 }
3992#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01003993
3994 /* Drop unexpected ChangeCipherSpec messages */
3995 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3996 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3997 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3998 {
3999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
4000 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4001 }
4002
4003 /* Drop unexpected ApplicationData records,
4004 * except at the beginning of renegotiations */
4005 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4006 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4007#if defined(MBEDTLS_SSL_RENEGOTIATION)
4008 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4009 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4010#endif
4011 )
4012 {
4013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4014 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4015 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004016 }
4017#endif /* MBEDTLS_SSL_PROTO_DTLS */
4018
Hanno Becker52c6dc62017-05-26 16:07:36 +01004019
4020 /* Check length against bounds of the current transform and version */
4021 if( ssl->transform_in == NULL )
4022 {
4023 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004024 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004025 {
4026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4027 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4028 }
4029 }
4030 else
4031 {
4032 if( ssl->in_msglen < ssl->transform_in->minlen )
4033 {
4034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4035 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4036 }
4037
4038#if defined(MBEDTLS_SSL_PROTO_SSL3)
4039 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004040 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004041 {
4042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4043 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4044 }
4045#endif
4046#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4047 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4048 /*
4049 * TLS encrypted messages can have up to 256 bytes of padding
4050 */
4051 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4052 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004053 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004054 {
4055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4056 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4057 }
4058#endif
4059 }
4060
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004061 return( 0 );
4062}
Paul Bakker5121ce52009-01-03 21:22:43 +00004063
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004064/*
4065 * If applicable, decrypt (and decompress) record content
4066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004067static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004068{
4069 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4072 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004074#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4075 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004079 ret = mbedtls_ssl_hw_record_read( ssl );
4080 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4083 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004084 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004085
4086 if( ret == 0 )
4087 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004088 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004090 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004091 {
4092 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004094 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004095 return( ret );
4096 }
4097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004099 ssl->in_msg, ssl->in_msglen );
4100
Angus Grattond8213d02016-05-25 20:56:48 +10004101 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4104 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004105 }
4106 }
4107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004108#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004109 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004111 {
4112 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004114 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004115 return( ret );
4116 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004117 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004118#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004120#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004121 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004124 }
4125#endif
4126
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004127 return( 0 );
4128}
4129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004131
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004132/*
4133 * Read a record.
4134 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004135 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4136 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4137 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004139int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004140{
4141 int ret;
4142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004144
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004145 if( ssl->keep_current_message == 0 )
4146 {
4147 do {
Simon Butcher99000142016-10-13 17:21:01 +01004148
Hanno Becker90333da2017-10-10 11:27:13 +01004149 do ret = mbedtls_ssl_read_record_layer( ssl );
4150 while( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
4151
4152 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004153 {
4154 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4155 return( ret );
4156 }
4157
4158 ret = mbedtls_ssl_handle_message_type( ssl );
4159
Hanno Becker90333da2017-10-10 11:27:13 +01004160 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4161 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004162
4163 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004164 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004165 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004166 return( ret );
4167 }
4168
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004169 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4170 {
4171 mbedtls_ssl_update_handshake_status( ssl );
4172 }
Simon Butcher99000142016-10-13 17:21:01 +01004173 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004174 else
Simon Butcher99000142016-10-13 17:21:01 +01004175 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4177 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004178 }
4179
4180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4181
4182 return( 0 );
4183}
4184
4185int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4186{
4187 int ret;
4188
Hanno Becker4a810fb2017-05-24 16:27:30 +01004189 /*
4190 * Step A
4191 *
4192 * Consume last content-layer message and potentially
4193 * update in_msglen which keeps track of the contents'
4194 * consumption state.
4195 *
4196 * (1) Handshake messages:
4197 * Remove last handshake message, move content
4198 * and adapt in_msglen.
4199 *
4200 * (2) Alert messages:
4201 * Consume whole record content, in_msglen = 0.
4202 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004203 * (3) Change cipher spec:
4204 * Consume whole record content, in_msglen = 0.
4205 *
4206 * (4) Application data:
4207 * Don't do anything - the record layer provides
4208 * the application data as a stream transport
4209 * and consumes through mbedtls_ssl_read only.
4210 *
4211 */
4212
4213 /* Case (1): Handshake messages */
4214 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004215 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004216 /* Hard assertion to be sure that no application data
4217 * is in flight, as corrupting ssl->in_msglen during
4218 * ssl->in_offt != NULL is fatal. */
4219 if( ssl->in_offt != NULL )
4220 {
4221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4222 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4223 }
4224
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004225 /*
4226 * Get next Handshake message in the current record
4227 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004228
Hanno Becker4a810fb2017-05-24 16:27:30 +01004229 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004230 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004231 * current handshake content: If DTLS handshake
4232 * fragmentation is used, that's the fragment
4233 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004234 * size here is faulty and should be changed at
4235 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004236 * (2) While it doesn't seem to cause problems, one
4237 * has to be very careful not to assume that in_hslen
4238 * is always <= in_msglen in a sensible communication.
4239 * Again, it's wrong for DTLS handshake fragmentation.
4240 * The following check is therefore mandatory, and
4241 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004242 * Additionally, ssl->in_hslen might be arbitrarily out of
4243 * bounds after handling a DTLS message with an unexpected
4244 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004245 */
4246 if( ssl->in_hslen < ssl->in_msglen )
4247 {
4248 ssl->in_msglen -= ssl->in_hslen;
4249 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4250 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004251
Hanno Becker4a810fb2017-05-24 16:27:30 +01004252 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4253 ssl->in_msg, ssl->in_msglen );
4254 }
4255 else
4256 {
4257 ssl->in_msglen = 0;
4258 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004259
Hanno Becker4a810fb2017-05-24 16:27:30 +01004260 ssl->in_hslen = 0;
4261 }
4262 /* Case (4): Application data */
4263 else if( ssl->in_offt != NULL )
4264 {
4265 return( 0 );
4266 }
4267 /* Everything else (CCS & Alerts) */
4268 else
4269 {
4270 ssl->in_msglen = 0;
4271 }
4272
4273 /*
4274 * Step B
4275 *
4276 * Fetch and decode new record if current one is fully consumed.
4277 *
4278 */
4279
4280 if( ssl->in_msglen > 0 )
4281 {
4282 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004283 return( 0 );
4284 }
4285
Hanno Becker4a810fb2017-05-24 16:27:30 +01004286 /* Current record either fully processed or to be discarded. */
4287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004288 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004290 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004291 return( ret );
4292 }
4293
4294 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004296#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004297 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4298 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004299 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004300 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4301 {
4302 /* Skip unexpected record (but not whole datagram) */
4303 ssl->next_record_offset = ssl->in_msglen
4304 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004305
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4307 "(header)" ) );
4308 }
4309 else
4310 {
4311 /* Skip invalid record and the rest of the datagram */
4312 ssl->next_record_offset = 0;
4313 ssl->in_left = 0;
4314
4315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4316 "(header)" ) );
4317 }
4318
4319 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004320 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004321 }
4322#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004323 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004324 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004325
4326 /*
4327 * Read and optionally decrypt the message contents
4328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4330 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004333 return( ret );
4334 }
4335
4336 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004337#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004338 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004341 if( ssl->next_record_offset < ssl->in_left )
4342 {
4343 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4344 }
4345 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004346 else
4347#endif
4348 ssl->in_left = 0;
4349
4350 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004352#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004353 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004354 {
4355 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4357 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004358 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004359 /* Except when waiting for Finished as a bad mac here
4360 * probably means something went wrong in the handshake
4361 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4362 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4363 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4364 {
4365#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4366 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4367 {
4368 mbedtls_ssl_send_alert_message( ssl,
4369 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4370 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4371 }
4372#endif
4373 return( ret );
4374 }
4375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004376#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004377 if( ssl->conf->badmac_limit != 0 &&
4378 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4381 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004382 }
4383#endif
4384
Hanno Becker4a810fb2017-05-24 16:27:30 +01004385 /* As above, invalid records cause
4386 * dismissal of the whole datagram. */
4387
4388 ssl->next_record_offset = 0;
4389 ssl->in_left = 0;
4390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004392 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004393 }
4394
4395 return( ret );
4396 }
4397 else
4398#endif
4399 {
4400 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4402 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404 mbedtls_ssl_send_alert_message( ssl,
4405 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4406 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004407 }
4408#endif
4409 return( ret );
4410 }
4411 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004412
Simon Butcher99000142016-10-13 17:21:01 +01004413 return( 0 );
4414}
4415
4416int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4417{
4418 int ret;
4419
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004420 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004421 * Handle particular types of records
4422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004424 {
Simon Butcher99000142016-10-13 17:21:01 +01004425 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4426 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004427 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004428 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004429 }
4430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004431 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004432 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004433 if( ssl->in_msglen != 2 )
4434 {
4435 /* Note: Standard allows for more than one 2 byte alert
4436 to be packed in a single message, but Mbed TLS doesn't
4437 currently support this. */
4438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4439 ssl->in_msglen ) );
4440 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4441 }
4442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004444 ssl->in_msg[0], ssl->in_msg[1] ) );
4445
4446 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004447 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004452 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004454 }
4455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004456 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4457 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4460 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004461 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004462
4463#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4464 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4465 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4466 {
Hanno Becker90333da2017-10-10 11:27:13 +01004467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004468 /* Will be handled when trying to parse ServerHello */
4469 return( 0 );
4470 }
4471#endif
4472
4473#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4474 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4475 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4476 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4477 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4478 {
4479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4480 /* Will be handled in mbedtls_ssl_parse_certificate() */
4481 return( 0 );
4482 }
4483#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4484
4485 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004486 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004487 }
4488
Hanno Beckerc76c6192017-06-06 10:03:17 +01004489#if defined(MBEDTLS_SSL_PROTO_DTLS)
4490 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4491 ssl->handshake != NULL &&
4492 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4493 {
4494 ssl_handshake_wrapup_free_hs_transform( ssl );
4495 }
4496#endif
4497
Paul Bakker5121ce52009-01-03 21:22:43 +00004498 return( 0 );
4499}
4500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004501int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004502{
4503 int ret;
4504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004505 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4506 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4507 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004508 {
4509 return( ret );
4510 }
4511
4512 return( 0 );
4513}
4514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004515int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004516 unsigned char level,
4517 unsigned char message )
4518{
4519 int ret;
4520
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004521 if( ssl == NULL || ssl->conf == NULL )
4522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004525 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004527 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004528 ssl->out_msglen = 2;
4529 ssl->out_msg[0] = level;
4530 ssl->out_msg[1] = message;
4531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004534 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004535 return( ret );
4536 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004538
4539 return( 0 );
4540}
4541
Paul Bakker5121ce52009-01-03 21:22:43 +00004542/*
4543 * Handshake functions
4544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004545#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4546 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4547 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4548 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4549 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4550 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4551 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004552/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004554{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4560 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004561 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4562 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004565 ssl->state++;
4566 return( 0 );
4567 }
4568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4570 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004571}
4572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004574{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004575 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004579 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4580 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004581 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4582 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004585 ssl->state++;
4586 return( 0 );
4587 }
4588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4590 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004591}
Gilles Peskinef9828522017-05-03 12:28:43 +02004592
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004593#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004594/* Some certificate support -> implement write and parse */
4595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004596int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004597{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004598 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004599 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004600 const mbedtls_x509_crt *crt;
4601 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004605 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4606 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004607 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4608 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004611 ssl->state++;
4612 return( 0 );
4613 }
4614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004615#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004616 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004617 {
4618 if( ssl->client_auth == 0 )
4619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004621 ssl->state++;
4622 return( 0 );
4623 }
4624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004625#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004626 /*
4627 * If using SSLv3 and got no cert, send an Alert message
4628 * (otherwise an empty Certificate message will be sent).
4629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004630 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4631 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004632 {
4633 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4635 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4636 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004639 goto write_msg;
4640 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004641#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004642 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004643#endif /* MBEDTLS_SSL_CLI_C */
4644#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004645 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004647 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4650 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004651 }
4652 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004653#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004656
4657 /*
4658 * 0 . 0 handshake type
4659 * 1 . 3 handshake length
4660 * 4 . 6 length of all certs
4661 * 7 . 9 length of cert. 1
4662 * 10 . n-1 peer certificate
4663 * n . n+2 length of cert. 2
4664 * n+3 . ... upper level cert, etc.
4665 */
4666 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004667 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004668
Paul Bakker29087132010-03-21 21:03:34 +00004669 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004670 {
4671 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10004672 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10004675 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004676 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004677 }
4678
4679 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4680 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4681 ssl->out_msg[i + 2] = (unsigned char)( n );
4682
4683 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4684 i += n; crt = crt->next;
4685 }
4686
4687 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4688 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4689 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4690
4691 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004692 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4693 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004694
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004695#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004696write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004697#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004698
4699 ssl->state++;
4700
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004701 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004702 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004703 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004704 return( ret );
4705 }
4706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004708
Paul Bakkered27a042013-04-18 22:46:23 +02004709 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004710}
4711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004713{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004714 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004715 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004716 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004717 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004718 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004722 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4723 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004724 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4725 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004728 ssl->state++;
4729 return( 0 );
4730 }
4731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004732#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004733 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004734 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4735 {
4736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4737 ssl->state++;
4738 return( 0 );
4739 }
4740
4741#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4742 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4743 authmode = ssl->handshake->sni_authmode;
4744#endif
4745
4746 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4747 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004748 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004749 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004751 ssl->state++;
4752 return( 0 );
4753 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004754#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004757 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004758 /* mbedtls_ssl_read_record may have sent an alert already. We
4759 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004761 return( ret );
4762 }
4763
4764 ssl->state++;
4765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004766#if defined(MBEDTLS_SSL_SRV_C)
4767#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004768 /*
4769 * Check if the client sent an empty certificate
4770 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004771 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004772 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004773 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004774 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004775 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4776 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4777 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004780
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004781 /* The client was asked for a certificate but didn't send
4782 one. The client should know what's going on, so we
4783 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004784 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004785 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004786 return( 0 );
4787 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004788 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004789 }
4790 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004791#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004793#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4794 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004795 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004798 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4799 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4800 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4801 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004804
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004805 /* The client was asked for a certificate but didn't send
4806 one. The client should know what's going on, so we
4807 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004808 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004809 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004810 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004811 else
4812 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004813 }
4814 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4816 MBEDTLS_SSL_PROTO_TLS1_2 */
4817#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004822 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4823 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004825 }
4826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004827 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4828 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004831 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4832 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004834 }
4835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004836 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004837
Paul Bakker5121ce52009-01-03 21:22:43 +00004838 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004840 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004841 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004842
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004843 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004844 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004847 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4848 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004850 }
4851
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004852 /* In case we tried to reuse a session but it failed */
4853 if( ssl->session_negotiate->peer_cert != NULL )
4854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004855 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4856 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004857 }
4858
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004859 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004860 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004861 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004864 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4865 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004866 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004867 }
4868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004869 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004870
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004871 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004872
4873 while( i < ssl->in_hslen )
4874 {
Philippe Antoine747fd532018-05-30 09:13:21 +02004875 if ( i + 3 > ssl->in_hslen ) {
4876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4877 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4878 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4879 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
4880 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004881 if( ssl->in_msg[i] != 0 )
4882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004884 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4885 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004886 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004887 }
4888
4889 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4890 | (unsigned int) ssl->in_msg[i + 2];
4891 i += 3;
4892
4893 if( n < 128 || i + n > ssl->in_hslen )
4894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004896 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4897 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004899 }
4900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004901 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004902 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004903 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004904 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004905 case 0: /*ok*/
4906 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
4907 /* Ignore certificate with an unknown algorithm: maybe a
4908 prior certificate was already trusted. */
4909 break;
4910
4911 case MBEDTLS_ERR_X509_ALLOC_FAILED:
4912 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
4913 goto crt_parse_der_failed;
4914
4915 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
4916 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4917 goto crt_parse_der_failed;
4918
4919 default:
4920 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4921 crt_parse_der_failed:
4922 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004923 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004924 return( ret );
4925 }
4926
4927 i += n;
4928 }
4929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004930 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004931
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004932 /*
4933 * On client, make sure the server cert doesn't change during renego to
4934 * avoid "triple handshake" attack: https://secure-resumption.com/
4935 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004937 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004938 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004939 {
4940 if( ssl->session->peer_cert == NULL )
4941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004943 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4944 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004945 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004946 }
4947
4948 if( ssl->session->peer_cert->raw.len !=
4949 ssl->session_negotiate->peer_cert->raw.len ||
4950 memcmp( ssl->session->peer_cert->raw.p,
4951 ssl->session_negotiate->peer_cert->raw.p,
4952 ssl->session->peer_cert->raw.len ) != 0 )
4953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004955 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4956 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004957 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004958 }
4959 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004960#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004961
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004962 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004963 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004964 mbedtls_x509_crt *ca_chain;
4965 mbedtls_x509_crl *ca_crl;
4966
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004967#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004968 if( ssl->handshake->sni_ca_chain != NULL )
4969 {
4970 ca_chain = ssl->handshake->sni_ca_chain;
4971 ca_crl = ssl->handshake->sni_ca_crl;
4972 }
4973 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004974#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004975 {
4976 ca_chain = ssl->conf->ca_chain;
4977 ca_crl = ssl->conf->ca_crl;
4978 }
4979
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004980 /*
4981 * Main check: verify certificate
4982 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004983 ret = mbedtls_x509_crt_verify_with_profile(
4984 ssl->session_negotiate->peer_cert,
4985 ca_chain, ca_crl,
4986 ssl->conf->cert_profile,
4987 ssl->hostname,
4988 &ssl->session_negotiate->verify_result,
4989 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004990
4991 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004993 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004994 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004995
4996 /*
4997 * Secondary checks: always done, but change 'ret' only if it was 0
4998 */
4999
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005000#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005002 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005003
5004 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005005 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005006 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005007 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005008 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005011 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005012 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005013 }
5014 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005015#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005017 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005018 ciphersuite_info,
5019 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005020 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005023 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005024 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005025 }
5026
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005027 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5028 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5029 * with details encoded in the verification flags. All other kinds
5030 * of error codes, including those from the user provided f_vrfy
5031 * functions, are treated as fatal and lead to a failure of
5032 * ssl_parse_certificate even if verification was optional. */
5033 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5034 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5035 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5036 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005037 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005038 }
5039
5040 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5041 {
5042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5043 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5044 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005045
5046 if( ret != 0 )
5047 {
5048 /* The certificate may have been rejected for several reasons.
5049 Pick one and send the corresponding alert. Which alert to send
5050 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005051 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5052 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5053 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5054 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5055 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5056 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5057 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5058 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5059 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5060 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5061 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5062 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5063 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5064 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5065 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5066 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5067 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5068 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5069 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5070 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005071 else
5072 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005073 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5074 alert );
5075 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005076
Hanno Beckere6706e62017-05-15 16:05:15 +01005077#if defined(MBEDTLS_DEBUG_C)
5078 if( ssl->session_negotiate->verify_result != 0 )
5079 {
5080 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5081 ssl->session_negotiate->verify_result ) );
5082 }
5083 else
5084 {
5085 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5086 }
5087#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005088 }
5089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005091
5092 return( ret );
5093}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005094#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5095 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5096 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5097 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5098 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5099 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5100 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005102int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005103{
5104 int ret;
5105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005108 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005109 ssl->out_msglen = 1;
5110 ssl->out_msg[0] = 1;
5111
Paul Bakker5121ce52009-01-03 21:22:43 +00005112 ssl->state++;
5113
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005114 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005115 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005116 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005117 return( ret );
5118 }
5119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005121
5122 return( 0 );
5123}
5124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005126{
5127 int ret;
5128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005131 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005133 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005134 return( ret );
5135 }
5136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005138 {
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_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005142 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005143 }
5144
5145 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005148 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5149 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005150 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005151 }
5152
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005153 /*
5154 * Switch to our negotiated transform and session parameters for inbound
5155 * data.
5156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005158 ssl->transform_in = ssl->transform_negotiate;
5159 ssl->session_in = ssl->session_negotiate;
5160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005161#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005162 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005164#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005165 ssl_dtls_replay_reset( ssl );
5166#endif
5167
5168 /* Increment epoch */
5169 if( ++ssl->in_epoch == 0 )
5170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005172 /* This is highly unlikely to happen for legitimate reasons, so
5173 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005174 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005175 }
5176 }
5177 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005178#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005179 memset( ssl->in_ctr, 0, 8 );
5180
5181 /*
5182 * Set the in_msg pointer to the correct location based on IV length
5183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005184 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005185 {
5186 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
5187 ssl->transform_negotiate->fixed_ivlen;
5188 }
5189 else
5190 ssl->in_msg = ssl->in_iv;
5191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005192#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5193 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005195 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005198 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5199 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005201 }
5202 }
5203#endif
5204
Paul Bakker5121ce52009-01-03 21:22:43 +00005205 ssl->state++;
5206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005208
5209 return( 0 );
5210}
5211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5213 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005214{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005215 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5218 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5219 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005220 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005221 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005222#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005223#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5224#if defined(MBEDTLS_SHA512_C)
5225 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005226 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5227 else
5228#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229#if defined(MBEDTLS_SHA256_C)
5230 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005231 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005232 else
5233#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005234#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005237 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005238 }
Paul Bakker380da532012-04-18 16:10:25 +00005239}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005241void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005243#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5244 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005245 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5246 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005247#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005248#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5249#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005250 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005251#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005252#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005253 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005254#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005255#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005256}
5257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005258static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005259 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005260{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005261#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5262 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005263 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5264 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005265#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005266#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5267#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005268 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005269#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005270#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005271 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005272#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005273#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005274}
5275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005276#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5277 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5278static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005279 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005280{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005281 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5282 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005283}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005284#endif
Paul Bakker380da532012-04-18 16:10:25 +00005285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005286#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5287#if defined(MBEDTLS_SHA256_C)
5288static void ssl_update_checksum_sha256( 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_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005292}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005293#endif
Paul Bakker380da532012-04-18 16:10:25 +00005294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295#if defined(MBEDTLS_SHA512_C)
5296static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005297 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005298{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005299 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005300}
Paul Bakker769075d2012-11-24 11:26:46 +01005301#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005302#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005304#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005305static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005306 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005307{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005308 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005309 mbedtls_md5_context md5;
5310 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005311
Paul Bakker5121ce52009-01-03 21:22:43 +00005312 unsigned char padbuf[48];
5313 unsigned char md5sum[16];
5314 unsigned char sha1sum[20];
5315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005316 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005317 if( !session )
5318 session = ssl->session;
5319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005321
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005322 mbedtls_md5_init( &md5 );
5323 mbedtls_sha1_init( &sha1 );
5324
5325 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5326 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005327
5328 /*
5329 * SSLv3:
5330 * hash =
5331 * MD5( master + pad2 +
5332 * MD5( handshake + sender + master + pad1 ) )
5333 * + SHA1( master + pad2 +
5334 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005335 */
5336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005338 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5339 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005340#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005342#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005343 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5344 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005345#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005348 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005349
Paul Bakker1ef83d62012-04-11 12:09:53 +00005350 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005351
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005352 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5353 mbedtls_md5_update_ret( &md5, session->master, 48 );
5354 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5355 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005356
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005357 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5358 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5359 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5360 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005361
Paul Bakker1ef83d62012-04-11 12:09:53 +00005362 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005363
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005364 mbedtls_md5_starts_ret( &md5 );
5365 mbedtls_md5_update_ret( &md5, session->master, 48 );
5366 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5367 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5368 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005369
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005370 mbedtls_sha1_starts_ret( &sha1 );
5371 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5372 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5373 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5374 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005377
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005378 mbedtls_md5_free( &md5 );
5379 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005380
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005381 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5382 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
5383 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005386}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005390static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005392{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005393 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005394 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005395 mbedtls_md5_context md5;
5396 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005397 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005400 if( !session )
5401 session = ssl->session;
5402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005404
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005405 mbedtls_md5_init( &md5 );
5406 mbedtls_sha1_init( &sha1 );
5407
5408 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5409 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005410
Paul Bakker1ef83d62012-04-11 12:09:53 +00005411 /*
5412 * TLSv1:
5413 * hash = PRF( master, finished_label,
5414 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5415 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005417#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005418 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5419 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005420#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005423 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5424 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005425#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005428 ? "client finished"
5429 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005430
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005431 mbedtls_md5_finish_ret( &md5, padbuf );
5432 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005433
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005434 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005435 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005438
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005439 mbedtls_md5_free( &md5 );
5440 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005441
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005442 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005445}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005448#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5449#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005450static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005452{
5453 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005454 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005455 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005456 unsigned char padbuf[32];
5457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005459 if( !session )
5460 session = ssl->session;
5461
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005462 mbedtls_sha256_init( &sha256 );
5463
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005465
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005466 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005467
5468 /*
5469 * TLSv1.2:
5470 * hash = PRF( master, finished_label,
5471 * Hash( handshake ) )[0.11]
5472 */
5473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005474#if !defined(MBEDTLS_SHA256_ALT)
5475 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005476 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005477#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005480 ? "client finished"
5481 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005482
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005483 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005484
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005485 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005486 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005489
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005490 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005491
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005492 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005495}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005496#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005498#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005499static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005500 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005501{
5502 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005503 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005504 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005505 unsigned char padbuf[48];
5506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005507 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005508 if( !session )
5509 session = ssl->session;
5510
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005511 mbedtls_sha512_init( &sha512 );
5512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005514
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005515 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005516
5517 /*
5518 * TLSv1.2:
5519 * hash = PRF( master, finished_label,
5520 * Hash( handshake ) )[0.11]
5521 */
5522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005524 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5525 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005526#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005529 ? "client finished"
5530 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005531
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005532 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005533
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005534 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005535 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005538
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005539 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005540
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005541 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005544}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005545#endif /* MBEDTLS_SHA512_C */
5546#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005549{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005551
5552 /*
5553 * Free our handshake params
5554 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02005555 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005557 ssl->handshake = NULL;
5558
5559 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005560 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005561 */
5562 if( ssl->transform )
5563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005564 mbedtls_ssl_transform_free( ssl->transform );
5565 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005566 }
5567 ssl->transform = ssl->transform_negotiate;
5568 ssl->transform_negotiate = NULL;
5569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005571}
5572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005573void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005574{
5575 int resume = ssl->handshake->resume;
5576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579#if defined(MBEDTLS_SSL_RENEGOTIATION)
5580 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005582 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005583 ssl->renego_records_seen = 0;
5584 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005585#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005586
5587 /*
5588 * Free the previous session and switch in the current one
5589 */
Paul Bakker0a597072012-09-25 21:55:46 +00005590 if( ssl->session )
5591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005592#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005593 /* RFC 7366 3.1: keep the EtM state */
5594 ssl->session_negotiate->encrypt_then_mac =
5595 ssl->session->encrypt_then_mac;
5596#endif
5597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598 mbedtls_ssl_session_free( ssl->session );
5599 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005600 }
5601 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005602 ssl->session_negotiate = NULL;
5603
Paul Bakker0a597072012-09-25 21:55:46 +00005604 /*
5605 * Add cache entry
5606 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005607 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005608 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005609 resume == 0 )
5610 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005611 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005613 }
Paul Bakker0a597072012-09-25 21:55:46 +00005614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005616 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005617 ssl->handshake->flight != NULL )
5618 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005619 /* Cancel handshake timer */
5620 ssl_set_timer( ssl, 0 );
5621
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005622 /* Keep last flight around in case we need to resend it:
5623 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005625 }
5626 else
5627#endif
5628 ssl_handshake_wrapup_free_hs_transform( ssl );
5629
Paul Bakker48916f92012-09-16 19:57:18 +00005630 ssl->state++;
5631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005633}
5634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005636{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005637 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005640
Paul Bakker92be97b2013-01-02 17:30:03 +01005641 /*
5642 * Set the out_msg pointer to the correct location based on IV length
5643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005644 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005645 {
5646 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5647 ssl->transform_negotiate->fixed_ivlen;
5648 }
5649 else
5650 ssl->out_msg = ssl->out_iv;
5651
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005652 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005653
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005654 /*
5655 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5656 * may define some other value. Currently (early 2016), no defined
5657 * ciphersuite does this (and this is unlikely to change as activity has
5658 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005660 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005662#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005663 ssl->verify_data_len = hash_len;
5664 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005665#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005666
Paul Bakker5121ce52009-01-03 21:22:43 +00005667 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005668 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5669 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005670
5671 /*
5672 * In case of session resuming, invert the client and server
5673 * ChangeCipherSpec messages order.
5674 */
Paul Bakker0a597072012-09-25 21:55:46 +00005675 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005678 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005680#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005681#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005682 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005684#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005685 }
5686 else
5687 ssl->state++;
5688
Paul Bakker48916f92012-09-16 19:57:18 +00005689 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005690 * Switch to our negotiated transform and session parameters for outbound
5691 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005696 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005697 {
5698 unsigned char i;
5699
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005700 /* Remember current epoch settings for resending */
5701 ssl->handshake->alt_transform_out = ssl->transform_out;
5702 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5703
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005704 /* Set sequence_number to zero */
5705 memset( ssl->out_ctr + 2, 0, 6 );
5706
5707 /* Increment epoch */
5708 for( i = 2; i > 0; i-- )
5709 if( ++ssl->out_ctr[i - 1] != 0 )
5710 break;
5711
5712 /* The loop goes to its end iff the counter is wrapping */
5713 if( i == 0 )
5714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5716 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005717 }
5718 }
5719 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005720#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005721 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005722
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005723 ssl->transform_out = ssl->transform_negotiate;
5724 ssl->session_out = ssl->session_negotiate;
5725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005726#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5727 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5732 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005733 }
5734 }
5735#endif
5736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005738 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005739 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005740#endif
5741
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005742 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005743 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005744 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005745 return( ret );
5746 }
5747
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005748#if defined(MBEDTLS_SSL_PROTO_DTLS)
5749 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5750 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
5751 {
5752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
5753 return( ret );
5754 }
5755#endif
5756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005758
5759 return( 0 );
5760}
5761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005762#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005763#define SSL_MAX_HASH_LEN 36
5764#else
5765#define SSL_MAX_HASH_LEN 12
5766#endif
5767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005769{
Paul Bakker23986e52011-04-24 08:57:21 +00005770 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005771 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005772 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005775
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005776 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005778 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005781 return( ret );
5782 }
5783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005787 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5788 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005789 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005790 }
5791
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005792 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793#if defined(MBEDTLS_SSL_PROTO_SSL3)
5794 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005795 hash_len = 36;
5796 else
5797#endif
5798 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5801 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005804 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5805 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005807 }
5808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005809 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005810 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005813 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5814 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005815 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005816 }
5817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005819 ssl->verify_data_len = hash_len;
5820 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005821#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005822
Paul Bakker0a597072012-09-25 21:55:46 +00005823 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005826 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005828#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005829#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005830 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005832#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005833 }
5834 else
5835 ssl->state++;
5836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005838 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005839 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005840#endif
5841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005843
5844 return( 0 );
5845}
5846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005847static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005848{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005849 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5852 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5853 mbedtls_md5_init( &handshake->fin_md5 );
5854 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005855 mbedtls_md5_starts_ret( &handshake->fin_md5 );
5856 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005857#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005858#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5859#if defined(MBEDTLS_SHA256_C)
5860 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005861 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005862#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863#if defined(MBEDTLS_SHA512_C)
5864 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005865 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005866#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005867#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005868
5869 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005870
5871#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5872 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5873 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5874#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005876#if defined(MBEDTLS_DHM_C)
5877 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005878#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879#if defined(MBEDTLS_ECDH_C)
5880 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005881#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02005882#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005883 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02005884#if defined(MBEDTLS_SSL_CLI_C)
5885 handshake->ecjpake_cache = NULL;
5886 handshake->ecjpake_cache_len = 0;
5887#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005888#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005889
5890#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5891 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5892#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005893}
5894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005895static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005896{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005897 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005899 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5900 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005902 mbedtls_md_init( &transform->md_ctx_enc );
5903 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005904}
5905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005906void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005907{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005908 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005909}
5910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005911static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005912{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005913 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005914 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005915 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005916 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005917 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005918 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02005919 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005920
5921 /*
5922 * Either the pointers are now NULL or cleared properly and can be freed.
5923 * Now allocate missing structures.
5924 */
5925 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005926 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005927 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005928 }
Paul Bakker48916f92012-09-16 19:57:18 +00005929
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005930 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005931 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005932 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005933 }
Paul Bakker48916f92012-09-16 19:57:18 +00005934
Paul Bakker82788fb2014-10-20 13:59:19 +02005935 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005936 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005937 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005938 }
Paul Bakker48916f92012-09-16 19:57:18 +00005939
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005940 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005941 if( ssl->handshake == NULL ||
5942 ssl->transform_negotiate == NULL ||
5943 ssl->session_negotiate == NULL )
5944 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947 mbedtls_free( ssl->handshake );
5948 mbedtls_free( ssl->transform_negotiate );
5949 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005950
5951 ssl->handshake = NULL;
5952 ssl->transform_negotiate = NULL;
5953 ssl->session_negotiate = NULL;
5954
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005955 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005956 }
5957
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005958 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005959 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005960 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005961 ssl_handshake_params_init( ssl->handshake );
5962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005964 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5965 {
5966 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005967
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005968 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5969 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5970 else
5971 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005972
5973 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005974 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005975#endif
5976
Paul Bakker48916f92012-09-16 19:57:18 +00005977 return( 0 );
5978}
5979
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005980#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005981/* Dummy cookie callbacks for defaults */
5982static int ssl_cookie_write_dummy( void *ctx,
5983 unsigned char **p, unsigned char *end,
5984 const unsigned char *cli_id, size_t cli_id_len )
5985{
5986 ((void) ctx);
5987 ((void) p);
5988 ((void) end);
5989 ((void) cli_id);
5990 ((void) cli_id_len);
5991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005993}
5994
5995static int ssl_cookie_check_dummy( void *ctx,
5996 const unsigned char *cookie, size_t cookie_len,
5997 const unsigned char *cli_id, size_t cli_id_len )
5998{
5999 ((void) ctx);
6000 ((void) cookie);
6001 ((void) cookie_len);
6002 ((void) cli_id);
6003 ((void) cli_id_len);
6004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006006}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006007#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006008
Paul Bakker5121ce52009-01-03 21:22:43 +00006009/*
6010 * Initialize an SSL context
6011 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006012void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6013{
6014 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6015}
6016
6017/*
6018 * Setup an SSL context
6019 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006020int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006021 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006022{
Paul Bakker48916f92012-09-16 19:57:18 +00006023 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006024
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006025 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006026
6027 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006028 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006029 */
Angus Grattond8213d02016-05-25 20:56:48 +10006030 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6031 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006032 {
Angus Grattond8213d02016-05-25 20:56:48 +10006033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6034 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6035 }
6036
6037 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6038 if( ssl->out_buf == NULL )
6039 {
6040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006042 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006043 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006044 }
6045
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006046#if defined(MBEDTLS_SSL_PROTO_DTLS)
6047 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6048 {
6049 ssl->out_hdr = ssl->out_buf;
6050 ssl->out_ctr = ssl->out_buf + 3;
6051 ssl->out_len = ssl->out_buf + 11;
6052 ssl->out_iv = ssl->out_buf + 13;
6053 ssl->out_msg = ssl->out_buf + 13;
6054
6055 ssl->in_hdr = ssl->in_buf;
6056 ssl->in_ctr = ssl->in_buf + 3;
6057 ssl->in_len = ssl->in_buf + 11;
6058 ssl->in_iv = ssl->in_buf + 13;
6059 ssl->in_msg = ssl->in_buf + 13;
6060 }
6061 else
6062#endif
6063 {
6064 ssl->out_ctr = ssl->out_buf;
6065 ssl->out_hdr = ssl->out_buf + 8;
6066 ssl->out_len = ssl->out_buf + 11;
6067 ssl->out_iv = ssl->out_buf + 13;
6068 ssl->out_msg = ssl->out_buf + 13;
6069
6070 ssl->in_ctr = ssl->in_buf;
6071 ssl->in_hdr = ssl->in_buf + 8;
6072 ssl->in_len = ssl->in_buf + 11;
6073 ssl->in_iv = ssl->in_buf + 13;
6074 ssl->in_msg = ssl->in_buf + 13;
6075 }
6076
Paul Bakker48916f92012-09-16 19:57:18 +00006077 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6078 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006079
6080 return( 0 );
6081}
6082
6083/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006084 * Reset an initialized and used SSL context for re-use while retaining
6085 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006086 *
6087 * If partial is non-zero, keep data in the input buffer and client ID.
6088 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006089 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006090static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006091{
Paul Bakker48916f92012-09-16 19:57:18 +00006092 int ret;
6093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006094 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006095
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006096 /* Cancel any possibly running timer */
6097 ssl_set_timer( ssl, 0 );
6098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006099#if defined(MBEDTLS_SSL_RENEGOTIATION)
6100 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006101 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006102
6103 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006104 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6105 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006106#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006108
Paul Bakker7eb013f2011-10-06 12:37:39 +00006109 ssl->in_offt = NULL;
6110
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006111 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006112 ssl->in_msgtype = 0;
6113 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006114 if( partial == 0 )
6115 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006117 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006118 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006119#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006120#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006121 ssl_dtls_replay_reset( ssl );
6122#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006123
6124 ssl->in_hslen = 0;
6125 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006126
6127 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006128
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006129 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006130 ssl->out_msgtype = 0;
6131 ssl->out_msglen = 0;
6132 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006133#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6134 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006135 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006136#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006137
Paul Bakker48916f92012-09-16 19:57:18 +00006138 ssl->transform_in = NULL;
6139 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006140
Hanno Becker78640902018-08-13 16:35:15 +01006141 ssl->session_in = NULL;
6142 ssl->session_out = NULL;
6143
Angus Grattond8213d02016-05-25 20:56:48 +10006144 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006145 if( partial == 0 )
Angus Grattond8213d02016-05-25 20:56:48 +10006146 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00006147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6149 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6152 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6155 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006156 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006157 }
6158#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006159
Paul Bakker48916f92012-09-16 19:57:18 +00006160 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006162 mbedtls_ssl_transform_free( ssl->transform );
6163 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006164 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006165 }
Paul Bakker48916f92012-09-16 19:57:18 +00006166
Paul Bakkerc0463502013-02-14 11:19:38 +01006167 if( ssl->session )
6168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006169 mbedtls_ssl_session_free( ssl->session );
6170 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006171 ssl->session = NULL;
6172 }
6173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006175 ssl->alpn_chosen = NULL;
6176#endif
6177
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006178#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006179 if( partial == 0 )
6180 {
6181 mbedtls_free( ssl->cli_id );
6182 ssl->cli_id = NULL;
6183 ssl->cli_id_len = 0;
6184 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006185#endif
6186
Paul Bakker48916f92012-09-16 19:57:18 +00006187 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6188 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006189
6190 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006191}
6192
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006193/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006194 * Reset an initialized and used SSL context for re-use while retaining
6195 * all application-set variables, function pointers and data.
6196 */
6197int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6198{
6199 return( ssl_session_reset_int( ssl, 0 ) );
6200}
6201
6202/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006203 * SSL set accessors
6204 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006205void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006206{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006207 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006208}
6209
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006210void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006211{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006212 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006213}
6214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006216void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006217{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006218 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006219}
6220#endif
6221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006223void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006224{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006225 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006226}
6227#endif
6228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006230void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006231{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006232 conf->hs_timeout_min = min;
6233 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006234}
6235#endif
6236
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006237void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006238{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006239 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006240}
6241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006243void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006244 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006245 void *p_vrfy )
6246{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006247 conf->f_vrfy = f_vrfy;
6248 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006249}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006250#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006251
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006252void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006253 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006254 void *p_rng )
6255{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006256 conf->f_rng = f_rng;
6257 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006258}
6259
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006260void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006261 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006262 void *p_dbg )
6263{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006264 conf->f_dbg = f_dbg;
6265 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006266}
6267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006269 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006270 mbedtls_ssl_send_t *f_send,
6271 mbedtls_ssl_recv_t *f_recv,
6272 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006273{
6274 ssl->p_bio = p_bio;
6275 ssl->f_send = f_send;
6276 ssl->f_recv = f_recv;
6277 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006278}
6279
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02006280#if defined(MBEDTLS_SSL_PROTO_DTLS)
6281void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
6282{
6283 ssl->mtu = mtu;
6284}
6285#endif
6286
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006287void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006288{
6289 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006290}
6291
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006292void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6293 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006294 mbedtls_ssl_set_timer_t *f_set_timer,
6295 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006296{
6297 ssl->p_timer = p_timer;
6298 ssl->f_set_timer = f_set_timer;
6299 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006300
6301 /* Make sure we start with no timer running */
6302 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006303}
6304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006306void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006307 void *p_cache,
6308 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6309 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006310{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006311 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006312 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006313 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006314}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006315#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317#if defined(MBEDTLS_SSL_CLI_C)
6318int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006319{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006320 int ret;
6321
6322 if( ssl == NULL ||
6323 session == NULL ||
6324 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006325 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006328 }
6329
6330 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6331 return( ret );
6332
Paul Bakker0a597072012-09-25 21:55:46 +00006333 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006334
6335 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006336}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006338
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006339void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006340 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006341{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006342 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6343 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6344 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6345 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006346}
6347
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006348void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006349 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006350 int major, int minor )
6351{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006353 return;
6354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006356 return;
6357
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006358 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006359}
6360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006361#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006362void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006363 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006364{
6365 conf->cert_profile = profile;
6366}
6367
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006368/* Append a new keycert entry to a (possibly empty) list */
6369static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6370 mbedtls_x509_crt *cert,
6371 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006372{
niisato8ee24222018-06-25 19:05:48 +09006373 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006374
niisato8ee24222018-06-25 19:05:48 +09006375 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6376 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006377 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006378
niisato8ee24222018-06-25 19:05:48 +09006379 new_cert->cert = cert;
6380 new_cert->key = key;
6381 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006382
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006383 /* Update head is the list was null, else add to the end */
6384 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006385 {
niisato8ee24222018-06-25 19:05:48 +09006386 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006387 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006388 else
6389 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006390 mbedtls_ssl_key_cert *cur = *head;
6391 while( cur->next != NULL )
6392 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09006393 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006394 }
6395
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006396 return( 0 );
6397}
6398
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006399int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006400 mbedtls_x509_crt *own_cert,
6401 mbedtls_pk_context *pk_key )
6402{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006403 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006404}
6405
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006406void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006407 mbedtls_x509_crt *ca_chain,
6408 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006409{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006410 conf->ca_chain = ca_chain;
6411 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006412}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006413#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006414
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006415#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6416int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6417 mbedtls_x509_crt *own_cert,
6418 mbedtls_pk_context *pk_key )
6419{
6420 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6421 own_cert, pk_key ) );
6422}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006423
6424void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6425 mbedtls_x509_crt *ca_chain,
6426 mbedtls_x509_crl *ca_crl )
6427{
6428 ssl->handshake->sni_ca_chain = ca_chain;
6429 ssl->handshake->sni_ca_crl = ca_crl;
6430}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006431
6432void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6433 int authmode )
6434{
6435 ssl->handshake->sni_authmode = authmode;
6436}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006437#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6438
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006439#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006440/*
6441 * Set EC J-PAKE password for current handshake
6442 */
6443int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6444 const unsigned char *pw,
6445 size_t pw_len )
6446{
6447 mbedtls_ecjpake_role role;
6448
Janos Follath8eb64132016-06-03 15:40:57 +01006449 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006450 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6451
6452 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6453 role = MBEDTLS_ECJPAKE_SERVER;
6454 else
6455 role = MBEDTLS_ECJPAKE_CLIENT;
6456
6457 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6458 role,
6459 MBEDTLS_MD_SHA256,
6460 MBEDTLS_ECP_DP_SECP256R1,
6461 pw, pw_len ) );
6462}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006463#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006466int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006467 const unsigned char *psk, size_t psk_len,
6468 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006469{
Paul Bakker6db455e2013-09-18 17:29:31 +02006470 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006475
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006476 /* Identity len will be encoded on two bytes */
6477 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10006478 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006479 {
6480 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6481 }
6482
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006483 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006484 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006485 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006486
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006487 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006488 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006489 conf->psk_len = 0;
6490 }
6491 if( conf->psk_identity != NULL )
6492 {
6493 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006494 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006495 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006496 }
6497
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006498 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6499 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006500 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006501 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006502 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006503 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006504 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006505 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006506 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006507
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006508 conf->psk_len = psk_len;
6509 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006510
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006511 memcpy( conf->psk, psk, conf->psk_len );
6512 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006513
6514 return( 0 );
6515}
6516
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006517int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6518 const unsigned char *psk, size_t psk_len )
6519{
6520 if( psk == NULL || ssl->handshake == NULL )
6521 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6522
6523 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6524 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6525
6526 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006527 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006528 mbedtls_platform_zeroize( ssl->handshake->psk,
6529 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006530 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006531 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006532 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006533
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006534 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006535 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006536
6537 ssl->handshake->psk_len = psk_len;
6538 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6539
6540 return( 0 );
6541}
6542
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006543void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006545 size_t),
6546 void *p_psk )
6547{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006548 conf->f_psk = f_psk;
6549 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006550}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006552
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006553#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006554
6555#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006556int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006557{
6558 int ret;
6559
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006560 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6561 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6562 {
6563 mbedtls_mpi_free( &conf->dhm_P );
6564 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006565 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006566 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006567
6568 return( 0 );
6569}
Hanno Becker470a8c42017-10-04 15:28:46 +01006570#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006571
Hanno Beckera90658f2017-10-04 15:29:08 +01006572int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6573 const unsigned char *dhm_P, size_t P_len,
6574 const unsigned char *dhm_G, size_t G_len )
6575{
6576 int ret;
6577
6578 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6579 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6580 {
6581 mbedtls_mpi_free( &conf->dhm_P );
6582 mbedtls_mpi_free( &conf->dhm_G );
6583 return( ret );
6584 }
6585
6586 return( 0 );
6587}
Paul Bakker5121ce52009-01-03 21:22:43 +00006588
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006589int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006590{
6591 int ret;
6592
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006593 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6594 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6595 {
6596 mbedtls_mpi_free( &conf->dhm_P );
6597 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006598 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006599 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006600
6601 return( 0 );
6602}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006603#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006604
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006605#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6606/*
6607 * Set the minimum length for Diffie-Hellman parameters
6608 */
6609void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6610 unsigned int bitlen )
6611{
6612 conf->dhm_min_bitlen = bitlen;
6613}
6614#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6615
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006616#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006617/*
6618 * Set allowed/preferred hashes for handshake signatures
6619 */
6620void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6621 const int *hashes )
6622{
6623 conf->sig_hashes = hashes;
6624}
Hanno Becker947194e2017-04-07 13:25:49 +01006625#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006626
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006627#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006628/*
6629 * Set the allowed elliptic curves
6630 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006631void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006632 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006633{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006634 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006635}
Hanno Becker947194e2017-04-07 13:25:49 +01006636#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006637
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006638#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006640{
Hanno Becker947194e2017-04-07 13:25:49 +01006641 /* Initialize to suppress unnecessary compiler warning */
6642 size_t hostname_len = 0;
6643
6644 /* Check if new hostname is valid before
6645 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006646 if( hostname != NULL )
6647 {
6648 hostname_len = strlen( hostname );
6649
6650 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6652 }
6653
6654 /* Now it's clear that we will overwrite the old hostname,
6655 * so we can free it safely */
6656
6657 if( ssl->hostname != NULL )
6658 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006659 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01006660 mbedtls_free( ssl->hostname );
6661 }
6662
6663 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006664
Paul Bakker5121ce52009-01-03 21:22:43 +00006665 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006666 {
6667 ssl->hostname = NULL;
6668 }
6669 else
6670 {
6671 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006672 if( ssl->hostname == NULL )
6673 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006674
Hanno Becker947194e2017-04-07 13:25:49 +01006675 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006676
Hanno Becker947194e2017-04-07 13:25:49 +01006677 ssl->hostname[hostname_len] = '\0';
6678 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006679
6680 return( 0 );
6681}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006682#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006683
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006684#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006685void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006687 const unsigned char *, size_t),
6688 void *p_sni )
6689{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006690 conf->f_sni = f_sni;
6691 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006692}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006693#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006696int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006697{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006698 size_t cur_len, tot_len;
6699 const char **p;
6700
6701 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006702 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6703 * MUST NOT be truncated."
6704 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006705 */
6706 tot_len = 0;
6707 for( p = protos; *p != NULL; p++ )
6708 {
6709 cur_len = strlen( *p );
6710 tot_len += cur_len;
6711
6712 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006714 }
6715
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006716 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006717
6718 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006719}
6720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006722{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006723 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006724}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006726
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006727void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006728{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006729 conf->max_major_ver = major;
6730 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006731}
6732
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006733void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006734{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006735 conf->min_major_ver = major;
6736 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006737}
6738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006740void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006741{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006742 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006743}
6744#endif
6745
Janos Follath088ce432017-04-10 12:42:31 +01006746#if defined(MBEDTLS_SSL_SRV_C)
6747void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6748 char cert_req_ca_list )
6749{
6750 conf->cert_req_ca_list = cert_req_ca_list;
6751}
6752#endif
6753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006755void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006756{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006757 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006758}
6759#endif
6760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006762void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006763{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006764 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006765}
6766#endif
6767
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006768#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006769void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006770{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006771 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006772}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006773#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006776int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006777{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10006779 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006781 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006782 }
6783
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006784 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006785
6786 return( 0 );
6787}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006788#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006791void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006792{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006793 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006794}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006798void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006799{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006800 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006801}
6802#endif
6803
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006804void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006805{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006806 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006807}
6808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006810void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006811{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006812 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006813}
6814
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006815void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006816{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006817 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006818}
6819
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006820void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006821 const unsigned char period[8] )
6822{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006823 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006824}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006825#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006828#if defined(MBEDTLS_SSL_CLI_C)
6829void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006830{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006831 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006832}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006833#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006834
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006835#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006836void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6837 mbedtls_ssl_ticket_write_t *f_ticket_write,
6838 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6839 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006840{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006841 conf->f_ticket_write = f_ticket_write;
6842 conf->f_ticket_parse = f_ticket_parse;
6843 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006844}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006845#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006847
Robert Cragie4feb7ae2015-10-02 13:33:37 +01006848#if defined(MBEDTLS_SSL_EXPORT_KEYS)
6849void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
6850 mbedtls_ssl_export_keys_t *f_export_keys,
6851 void *p_export_keys )
6852{
6853 conf->f_export_keys = f_export_keys;
6854 conf->p_export_keys = p_export_keys;
6855}
6856#endif
6857
Gilles Peskineb74a1c72018-04-24 13:09:22 +02006858#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006859void mbedtls_ssl_conf_async_private_cb(
6860 mbedtls_ssl_config *conf,
6861 mbedtls_ssl_async_sign_t *f_async_sign,
6862 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
6863 mbedtls_ssl_async_resume_t *f_async_resume,
6864 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006865 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006866{
6867 conf->f_async_sign_start = f_async_sign;
6868 conf->f_async_decrypt_start = f_async_decrypt;
6869 conf->f_async_resume = f_async_resume;
6870 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006871 conf->p_async_config_data = async_config_data;
6872}
6873
Gilles Peskine8f97af72018-04-26 11:46:10 +02006874void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
6875{
6876 return( conf->p_async_config_data );
6877}
6878
Gilles Peskine1febfef2018-04-30 11:54:39 +02006879void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006880{
6881 if( ssl->handshake == NULL )
6882 return( NULL );
6883 else
6884 return( ssl->handshake->user_async_ctx );
6885}
6886
Gilles Peskine1febfef2018-04-30 11:54:39 +02006887void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006888 void *ctx )
6889{
6890 if( ssl->handshake != NULL )
6891 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006892}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02006893#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006894
Paul Bakker5121ce52009-01-03 21:22:43 +00006895/*
6896 * SSL get accessors
6897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006899{
6900 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6901}
6902
Hanno Becker8b170a02017-10-10 11:51:19 +01006903int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
6904{
6905 /*
6906 * Case A: We're currently holding back
6907 * a message for further processing.
6908 */
6909
6910 if( ssl->keep_current_message == 1 )
6911 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006912 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006913 return( 1 );
6914 }
6915
6916 /*
6917 * Case B: Further records are pending in the current datagram.
6918 */
6919
6920#if defined(MBEDTLS_SSL_PROTO_DTLS)
6921 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6922 ssl->in_left > ssl->next_record_offset )
6923 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006924 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006925 return( 1 );
6926 }
6927#endif /* MBEDTLS_SSL_PROTO_DTLS */
6928
6929 /*
6930 * Case C: A handshake message is being processed.
6931 */
6932
Hanno Becker8b170a02017-10-10 11:51:19 +01006933 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
6934 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006935 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006936 return( 1 );
6937 }
6938
6939 /*
6940 * Case D: An application data message is being processed
6941 */
6942 if( ssl->in_offt != NULL )
6943 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006944 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006945 return( 1 );
6946 }
6947
6948 /*
6949 * In all other cases, the rest of the message can be dropped.
6950 * As in ssl_read_record_layer, this needs to be adapted if
6951 * we implement support for multiple alerts in single records.
6952 */
6953
6954 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
6955 return( 0 );
6956}
6957
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006958uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006959{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006960 if( ssl->session != NULL )
6961 return( ssl->session->verify_result );
6962
6963 if( ssl->session_negotiate != NULL )
6964 return( ssl->session_negotiate->verify_result );
6965
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006966 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006967}
6968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006970{
Paul Bakker926c8e42013-03-06 10:23:34 +01006971 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006972 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006975}
6976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006978{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006980 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006981 {
6982 switch( ssl->minor_ver )
6983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006985 return( "DTLSv1.0" );
6986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006988 return( "DTLSv1.2" );
6989
6990 default:
6991 return( "unknown (DTLS)" );
6992 }
6993 }
6994#endif
6995
Paul Bakker43ca69c2011-01-15 17:35:19 +00006996 switch( ssl->minor_ver )
6997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006999 return( "SSLv3.0" );
7000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007002 return( "TLSv1.0" );
7003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007005 return( "TLSv1.1" );
7006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007008 return( "TLSv1.2" );
7009
Paul Bakker43ca69c2011-01-15 17:35:19 +00007010 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007011 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007012 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007013}
7014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007015int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007016{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007017 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007019 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007020
Hanno Becker78640902018-08-13 16:35:15 +01007021 if( transform == NULL )
7022 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024#if defined(MBEDTLS_ZLIB_SUPPORT)
7025 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7026 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007027 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007028#endif
7029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032 case MBEDTLS_MODE_GCM:
7033 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007034 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007036 transform_expansion = transform->minlen;
7037 break;
7038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007039 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007040
7041 block_size = mbedtls_cipher_get_block_size(
7042 &transform->cipher_ctx_enc );
7043
7044#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7045 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7046 {
7047 /* Expansion due to addition of
7048 * - MAC
7049 * - CBC padding (theoretically up to 256 bytes, but
7050 * we never use more than block_size)
7051 * - explicit IV
7052 */
7053 transform_expansion = transform->maclen + 2 * block_size;
7054 }
7055 else
7056#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7057 {
7058 /* No explicit IV prior to TLS 1.1. */
7059 transform_expansion = transform->maclen + block_size;
7060 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007061 break;
7062
7063 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007065 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007066 }
7067
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007068 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007069}
7070
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007071#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7072size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7073{
7074 size_t max_len;
7075
7076 /*
7077 * Assume mfl_code is correct since it was checked when set
7078 */
Angus Grattond8213d02016-05-25 20:56:48 +10007079 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007080
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007081 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007082 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007083 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007084 {
Angus Grattond8213d02016-05-25 20:56:48 +10007085 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007086 }
7087
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007088 /* During a handshake, use the value being negotiated */
7089 if( ssl->session_negotiate != NULL &&
7090 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7091 {
7092 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7093 }
7094
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007095 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007096}
7097#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7098
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007099int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7100{
7101 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7102
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02007103#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7104 !defined(MBEDTLS_SSL_PROTO_DTLS)
7105 (void) ssl;
7106#endif
7107
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007108#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7109 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7110
7111 if( max_len > mfl )
7112 max_len = mfl;
7113#endif
7114
7115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007116 if( ssl->mtu != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007117 {
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007118 const size_t mtu = ssl->mtu;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007119 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7120 const size_t overhead = (size_t) ret;
7121
7122 if( ret < 0 )
7123 return( ret );
7124
7125 if( mtu <= overhead )
7126 {
7127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7128 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7129 }
7130
7131 if( max_len > mtu - overhead )
7132 max_len = mtu - overhead;
7133 }
7134#endif
7135
7136 return( (int) max_len );
7137}
7138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139#if defined(MBEDTLS_X509_CRT_PARSE_C)
7140const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007141{
7142 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007143 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007144
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007145 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149#if defined(MBEDTLS_SSL_CLI_C)
7150int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007151{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007152 if( ssl == NULL ||
7153 dst == NULL ||
7154 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007155 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007158 }
7159
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007160 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007163
Paul Bakker5121ce52009-01-03 21:22:43 +00007164/*
Paul Bakker1961b702013-01-25 14:49:24 +01007165 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007168{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007170
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007171 if( ssl == NULL || ssl->conf == NULL )
7172 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007175 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007177#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007179 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007181#endif
7182
Paul Bakker1961b702013-01-25 14:49:24 +01007183 return( ret );
7184}
7185
7186/*
7187 * Perform the SSL handshake
7188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007190{
7191 int ret = 0;
7192
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007193 if( ssl == NULL || ssl->conf == NULL )
7194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007201
7202 if( ret != 0 )
7203 break;
7204 }
7205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007207
7208 return( ret );
7209}
7210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211#if defined(MBEDTLS_SSL_RENEGOTIATION)
7212#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007213/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007214 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007217{
7218 int ret;
7219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007221
7222 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7224 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007225
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007226 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007227 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007229 return( ret );
7230 }
7231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007233
7234 return( 0 );
7235}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007236#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007237
7238/*
7239 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240 * - any side: calling mbedtls_ssl_renegotiate(),
7241 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7242 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007243 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007244 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007248{
7249 int ret;
7250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007252
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007253 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7254 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007255
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007256 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7257 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007259 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007261 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007262 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007263 ssl->handshake->out_msg_seq = 1;
7264 else
7265 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007266 }
7267#endif
7268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7270 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007275 return( ret );
7276 }
7277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007279
7280 return( 0 );
7281}
7282
7283/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007284 * Renegotiate current connection on client,
7285 * or request renegotiation on server
7286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007288{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007290
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007291 if( ssl == NULL || ssl->conf == NULL )
7292 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007295 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007296 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007302
7303 /* Did we already try/start sending HelloRequest? */
7304 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007305 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007306
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007307 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007308 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007312 /*
7313 * On client, either start the renegotiation process or,
7314 * if already in progress, continue the handshake
7315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7319 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007320
7321 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007324 return( ret );
7325 }
7326 }
7327 else
7328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007332 return( ret );
7333 }
7334 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007335#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007336
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007337 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007338}
7339
7340/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007341 * Check record counters and renegotiate if they're above the limit.
7342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007344{
Andres AG2196c7f2016-12-15 17:01:16 +00007345 size_t ep_len = ssl_ep_len( ssl );
7346 int in_ctr_cmp;
7347 int out_ctr_cmp;
7348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007349 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7350 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007351 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007352 {
7353 return( 0 );
7354 }
7355
Andres AG2196c7f2016-12-15 17:01:16 +00007356 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7357 ssl->conf->renego_period + ep_len, 8 - ep_len );
7358 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
7359 ssl->conf->renego_period + ep_len, 8 - ep_len );
7360
7361 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007362 {
7363 return( 0 );
7364 }
7365
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007370
7371/*
7372 * Receive application data decrypted from the SSL layer
7373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007375{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007376 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007377 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007378
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007379 if( ssl == NULL || ssl->conf == NULL )
7380 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007385 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007388 return( ret );
7389
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007390 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007391 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007392 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007393 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007394 return( ret );
7395 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007396 }
7397#endif
7398
Hanno Becker4a810fb2017-05-24 16:27:30 +01007399 /*
7400 * Check if renegotiation is necessary and/or handshake is
7401 * in process. If yes, perform/continue, and fall through
7402 * if an unexpected packet is received while the client
7403 * is waiting for the ServerHello.
7404 *
7405 * (There is no equivalent to the last condition on
7406 * the server-side as it is not treated as within
7407 * a handshake while waiting for the ClientHello
7408 * after a renegotiation request.)
7409 */
7410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007412 ret = ssl_check_ctr_renegotiate( ssl );
7413 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7414 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007417 return( ret );
7418 }
7419#endif
7420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007424 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7425 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007428 return( ret );
7429 }
7430 }
7431
Hanno Beckere41158b2017-10-23 13:30:32 +01007432 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01007433 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007434 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007435 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007436 if( ssl->f_get_timer != NULL &&
7437 ssl->f_get_timer( ssl->p_timer ) == -1 )
7438 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007439 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007440 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007441
Hanno Becker4a810fb2017-05-24 16:27:30 +01007442 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007443 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007444 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7445 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007446
Hanno Becker4a810fb2017-05-24 16:27:30 +01007447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7448 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007449 }
7450
7451 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007453 {
7454 /*
7455 * OpenSSL sends empty messages to randomize the IV
7456 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007460 return( 0 );
7461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007463 return( ret );
7464 }
7465 }
7466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007470
Hanno Becker4a810fb2017-05-24 16:27:30 +01007471 /*
7472 * - For client-side, expect SERVER_HELLO_REQUEST.
7473 * - For server-side, expect CLIENT_HELLO.
7474 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7475 */
7476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007478 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007480 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007483
7484 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007485#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007486 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007487 {
7488 continue;
7489 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007490#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007492 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007493#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007494
Hanno Becker4a810fb2017-05-24 16:27:30 +01007495#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007496 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007500
7501 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007503 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007504 {
7505 continue;
7506 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007507#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007509 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007510#endif /* MBEDTLS_SSL_SRV_C */
7511
Hanno Becker21df7f92017-10-17 11:03:26 +01007512#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007513 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007514 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7515 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7516 ssl->conf->allow_legacy_renegotiation ==
7517 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7518 {
7519 /*
7520 * Accept renegotiation request
7521 */
Paul Bakker48916f92012-09-16 19:57:18 +00007522
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007523 /* DTLS clients need to know renego is server-initiated */
7524#if defined(MBEDTLS_SSL_PROTO_DTLS)
7525 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7526 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7527 {
7528 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7529 }
7530#endif
7531 ret = ssl_start_renegotiation( ssl );
7532 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7533 ret != 0 )
7534 {
7535 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7536 return( ret );
7537 }
7538 }
7539 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007540#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007541 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007542 /*
7543 * Refuse renegotiation
7544 */
7545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007548#if defined(MBEDTLS_SSL_PROTO_SSL3)
7549 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007550 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007551 /* SSLv3 does not have a "no_renegotiation" warning, so
7552 we send a fatal alert and abort the connection. */
7553 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7554 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7555 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007556 }
7557 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7559#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7560 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7561 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7564 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7565 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007566 {
7567 return( ret );
7568 }
Paul Bakker48916f92012-09-16 19:57:18 +00007569 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007570 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007571#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7572 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7575 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007576 }
Paul Bakker48916f92012-09-16 19:57:18 +00007577 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007578
Hanno Becker90333da2017-10-10 11:27:13 +01007579 /* At this point, we don't know whether the renegotiation has been
7580 * completed or not. The cases to consider are the following:
7581 * 1) The renegotiation is complete. In this case, no new record
7582 * has been read yet.
7583 * 2) The renegotiation is incomplete because the client received
7584 * an application data record while awaiting the ServerHello.
7585 * 3) The renegotiation is incomplete because the client received
7586 * a non-handshake, non-application data message while awaiting
7587 * the ServerHello.
7588 * In each of these case, looping will be the proper action:
7589 * - For 1), the next iteration will read a new record and check
7590 * if it's application data.
7591 * - For 2), the loop condition isn't satisfied as application data
7592 * is present, hence continue is the same as break
7593 * - For 3), the loop condition is satisfied and read_record
7594 * will re-deliver the message that was held back by the client
7595 * when expecting the ServerHello.
7596 */
7597 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00007598 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007599#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007601 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007602 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007603 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007604 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007607 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007609 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007610 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007611 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7615 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007618 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007619 }
7620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7624 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007625 }
7626
7627 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007628
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007629 /* We're going to return something now, cancel timer,
7630 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007632 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007633
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007634#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007635 /* If we requested renego but received AppData, resend HelloRequest.
7636 * Do it now, after setting in_offt, to avoid taking this branch
7637 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007639 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007641 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007642 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007645 return( ret );
7646 }
7647 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007649#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007650 }
7651
7652 n = ( len < ssl->in_msglen )
7653 ? len : ssl->in_msglen;
7654
7655 memcpy( buf, ssl->in_offt, n );
7656 ssl->in_msglen -= n;
7657
7658 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007659 {
7660 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007661 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007662 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007663 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007664 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007665 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007666 /* more data available */
7667 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007668 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007671
Paul Bakker23986e52011-04-24 08:57:21 +00007672 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007673}
7674
7675/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007676 * Send application data to be encrypted by the SSL layer, taking care of max
7677 * fragment length and buffer size.
7678 *
7679 * According to RFC 5246 Section 6.2.1:
7680 *
7681 * Zero-length fragments of Application data MAY be sent as they are
7682 * potentially useful as a traffic analysis countermeasure.
7683 *
7684 * Therefore, it is possible that the input message length is 0 and the
7685 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007686 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007687static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007688 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007689{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007690 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
7691 const size_t max_len = (size_t) ret;
7692
7693 if( ret < 0 )
7694 {
7695 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
7696 return( ret );
7697 }
7698
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007699 if( len > max_len )
7700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007701#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007702 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007705 "maximum fragment length: %d > %d",
7706 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007708 }
7709 else
7710#endif
7711 len = max_len;
7712 }
Paul Bakker887bd502011-06-08 13:10:54 +00007713
Paul Bakker5121ce52009-01-03 21:22:43 +00007714 if( ssl->out_left != 0 )
7715 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007716 /*
7717 * The user has previously tried to send the data and
7718 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7719 * written. In this case, we expect the high-level write function
7720 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007725 return( ret );
7726 }
7727 }
Paul Bakker887bd502011-06-08 13:10:54 +00007728 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007729 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007730 /*
7731 * The user is trying to send a message the first time, so we need to
7732 * copy the data into the internal buffers and setup the data structure
7733 * to keep track of partial writes
7734 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007735 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007737 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007741 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007742 return( ret );
7743 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007744 }
7745
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007746 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007747}
7748
7749/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007750 * Write application data, doing 1/n-1 splitting if necessary.
7751 *
7752 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007753 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01007754 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007757static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007758 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007759{
7760 int ret;
7761
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007762 if( ssl->conf->cbc_record_splitting ==
7763 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007764 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7766 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7767 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007768 {
7769 return( ssl_write_real( ssl, buf, len ) );
7770 }
7771
7772 if( ssl->split_done == 0 )
7773 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007774 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007775 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007776 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007777 }
7778
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007779 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7780 return( ret );
7781 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007782
7783 return( ret + 1 );
7784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007786
7787/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007788 * Write application data (public-facing wrapper)
7789 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007790int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007791{
7792 int ret;
7793
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007795
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007796 if( ssl == NULL || ssl->conf == NULL )
7797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7798
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007799#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007800 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7801 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007802 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007803 return( ret );
7804 }
7805#endif
7806
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007807 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007808 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007809 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007810 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007812 return( ret );
7813 }
7814 }
7815
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007816#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007817 ret = ssl_write_split( ssl, buf, len );
7818#else
7819 ret = ssl_write_real( ssl, buf, len );
7820#endif
7821
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007823
7824 return( ret );
7825}
7826
7827/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007828 * Notify the peer that the connection is being closed
7829 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007831{
7832 int ret;
7833
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007834 if( ssl == NULL || ssl->conf == NULL )
7835 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007838
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007839 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7845 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7846 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007848 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007849 return( ret );
7850 }
7851 }
7852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007854
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007855 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007856}
7857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00007859{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007860 if( transform == NULL )
7861 return;
7862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00007864 deflateEnd( &transform->ctx_deflate );
7865 inflateEnd( &transform->ctx_inflate );
7866#endif
7867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868 mbedtls_cipher_free( &transform->cipher_ctx_enc );
7869 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02007870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871 mbedtls_md_free( &transform->md_ctx_enc );
7872 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02007873
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007874 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007875}
7876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877#if defined(MBEDTLS_X509_CRT_PARSE_C)
7878static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007881
7882 while( cur != NULL )
7883 {
7884 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007886 cur = next;
7887 }
7888}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007890
Gilles Peskine9b562d52018-04-25 20:32:43 +02007891void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007892{
Gilles Peskine9b562d52018-04-25 20:32:43 +02007893 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
7894
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007895 if( handshake == NULL )
7896 return;
7897
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007898#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
7899 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
7900 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02007901 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007902 handshake->async_in_progress = 0;
7903 }
7904#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
7905
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007906#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7907 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7908 mbedtls_md5_free( &handshake->fin_md5 );
7909 mbedtls_sha1_free( &handshake->fin_sha1 );
7910#endif
7911#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7912#if defined(MBEDTLS_SHA256_C)
7913 mbedtls_sha256_free( &handshake->fin_sha256 );
7914#endif
7915#if defined(MBEDTLS_SHA512_C)
7916 mbedtls_sha512_free( &handshake->fin_sha512 );
7917#endif
7918#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920#if defined(MBEDTLS_DHM_C)
7921 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007922#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923#if defined(MBEDTLS_ECDH_C)
7924 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007925#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007926#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007927 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007928#if defined(MBEDTLS_SSL_CLI_C)
7929 mbedtls_free( handshake->ecjpake_cache );
7930 handshake->ecjpake_cache = NULL;
7931 handshake->ecjpake_cache_len = 0;
7932#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007933#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02007934
Janos Follath4ae5c292016-02-10 11:27:43 +00007935#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
7936 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02007937 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007939#endif
7940
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007941#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7942 if( handshake->psk != NULL )
7943 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007944 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007945 mbedtls_free( handshake->psk );
7946 }
7947#endif
7948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7950 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007951 /*
7952 * Free only the linked list wrapper, not the keys themselves
7953 * since the belong to the SNI callback
7954 */
7955 if( handshake->sni_key_cert != NULL )
7956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007958
7959 while( cur != NULL )
7960 {
7961 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007963 cur = next;
7964 }
7965 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968#if defined(MBEDTLS_SSL_PROTO_DTLS)
7969 mbedtls_free( handshake->verify_cookie );
7970 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007971 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007972#endif
7973
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007974 mbedtls_platform_zeroize( handshake,
7975 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007976}
7977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007978void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007979{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007980 if( session == NULL )
7981 return;
7982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007984 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986 mbedtls_x509_crt_free( session->peer_cert );
7987 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007988 }
Paul Bakkered27a042013-04-18 22:46:23 +02007989#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007990
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007991#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007993#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007994
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007995 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007996}
7997
Paul Bakker5121ce52009-01-03 21:22:43 +00007998/*
7999 * Free an SSL context
8000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008002{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008003 if( ssl == NULL )
8004 return;
8005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008007
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008008 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008009 {
Angus Grattond8213d02016-05-25 20:56:48 +10008010 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008012 }
8013
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008014 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008015 {
Angus Grattond8213d02016-05-25 20:56:48 +10008016 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008018 }
8019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008021 if( ssl->compress_buf != NULL )
8022 {
Angus Grattond8213d02016-05-25 20:56:48 +10008023 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008025 }
8026#endif
8027
Paul Bakker48916f92012-09-16 19:57:18 +00008028 if( ssl->transform )
8029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008030 mbedtls_ssl_transform_free( ssl->transform );
8031 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008032 }
8033
8034 if( ssl->handshake )
8035 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008036 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8038 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008040 mbedtls_free( ssl->handshake );
8041 mbedtls_free( ssl->transform_negotiate );
8042 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008043 }
8044
Paul Bakkerc0463502013-02-14 11:19:38 +01008045 if( ssl->session )
8046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008047 mbedtls_ssl_session_free( ssl->session );
8048 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008049 }
8050
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008052 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008053 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008054 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008056 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008057#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008059#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8060 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8063 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008064 }
8065#endif
8066
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008067#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008068 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008069#endif
8070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008072
Paul Bakker86f04f42013-02-14 11:20:09 +01008073 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008074 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008075}
8076
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008077/*
8078 * Initialze mbedtls_ssl_config
8079 */
8080void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8081{
8082 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8083}
8084
Simon Butcherc97b6972015-12-27 23:48:17 +00008085#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008086static int ssl_preset_default_hashes[] = {
8087#if defined(MBEDTLS_SHA512_C)
8088 MBEDTLS_MD_SHA512,
8089 MBEDTLS_MD_SHA384,
8090#endif
8091#if defined(MBEDTLS_SHA256_C)
8092 MBEDTLS_MD_SHA256,
8093 MBEDTLS_MD_SHA224,
8094#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008095#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008096 MBEDTLS_MD_SHA1,
8097#endif
8098 MBEDTLS_MD_NONE
8099};
Simon Butcherc97b6972015-12-27 23:48:17 +00008100#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008101
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008102static int ssl_preset_suiteb_ciphersuites[] = {
8103 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8104 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8105 0
8106};
8107
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008108#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008109static int ssl_preset_suiteb_hashes[] = {
8110 MBEDTLS_MD_SHA256,
8111 MBEDTLS_MD_SHA384,
8112 MBEDTLS_MD_NONE
8113};
8114#endif
8115
8116#if defined(MBEDTLS_ECP_C)
8117static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8118 MBEDTLS_ECP_DP_SECP256R1,
8119 MBEDTLS_ECP_DP_SECP384R1,
8120 MBEDTLS_ECP_DP_NONE
8121};
8122#endif
8123
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008124/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008125 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008126 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008127int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008128 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008129{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008130#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008131 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008132#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008133
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008134 /* Use the functions here so that they are covered in tests,
8135 * but otherwise access member directly for efficiency */
8136 mbedtls_ssl_conf_endpoint( conf, endpoint );
8137 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008138
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008139 /*
8140 * Things that are common to all presets
8141 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008142#if defined(MBEDTLS_SSL_CLI_C)
8143 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8144 {
8145 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8146#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8147 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8148#endif
8149 }
8150#endif
8151
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008152#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008153 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008154#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008155
8156#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8157 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8158#endif
8159
8160#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8161 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8162#endif
8163
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008164#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8165 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8166#endif
8167
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008168#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008169 conf->f_cookie_write = ssl_cookie_write_dummy;
8170 conf->f_cookie_check = ssl_cookie_check_dummy;
8171#endif
8172
8173#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8174 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8175#endif
8176
Janos Follath088ce432017-04-10 12:42:31 +01008177#if defined(MBEDTLS_SSL_SRV_C)
8178 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8179#endif
8180
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008181#if defined(MBEDTLS_SSL_PROTO_DTLS)
8182 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8183 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8184#endif
8185
8186#if defined(MBEDTLS_SSL_RENEGOTIATION)
8187 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008188 memset( conf->renego_period, 0x00, 2 );
8189 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008190#endif
8191
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008192#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8193 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8194 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008195 const unsigned char dhm_p[] =
8196 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8197 const unsigned char dhm_g[] =
8198 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8199
Hanno Beckera90658f2017-10-04 15:29:08 +01008200 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8201 dhm_p, sizeof( dhm_p ),
8202 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008203 {
8204 return( ret );
8205 }
8206 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008207#endif
8208
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008209 /*
8210 * Preset-specific defaults
8211 */
8212 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008213 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008214 /*
8215 * NSA Suite B
8216 */
8217 case MBEDTLS_SSL_PRESET_SUITEB:
8218 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8219 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8220 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8221 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8222
8223 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8224 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8225 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8226 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8227 ssl_preset_suiteb_ciphersuites;
8228
8229#if defined(MBEDTLS_X509_CRT_PARSE_C)
8230 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008231#endif
8232
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008233#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008234 conf->sig_hashes = ssl_preset_suiteb_hashes;
8235#endif
8236
8237#if defined(MBEDTLS_ECP_C)
8238 conf->curve_list = ssl_preset_suiteb_curves;
8239#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008240 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008241
8242 /*
8243 * Default
8244 */
8245 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008246 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8247 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8248 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8249 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8250 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8251 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8252 MBEDTLS_SSL_MIN_MINOR_VERSION :
8253 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008254 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8255 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8256
8257#if defined(MBEDTLS_SSL_PROTO_DTLS)
8258 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8259 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8260#endif
8261
8262 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8263 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8264 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8265 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8266 mbedtls_ssl_list_ciphersuites();
8267
8268#if defined(MBEDTLS_X509_CRT_PARSE_C)
8269 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8270#endif
8271
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008272#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008273 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008274#endif
8275
8276#if defined(MBEDTLS_ECP_C)
8277 conf->curve_list = mbedtls_ecp_grp_id_list();
8278#endif
8279
8280#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8281 conf->dhm_min_bitlen = 1024;
8282#endif
8283 }
8284
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008285 return( 0 );
8286}
8287
8288/*
8289 * Free mbedtls_ssl_config
8290 */
8291void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8292{
8293#if defined(MBEDTLS_DHM_C)
8294 mbedtls_mpi_free( &conf->dhm_P );
8295 mbedtls_mpi_free( &conf->dhm_G );
8296#endif
8297
8298#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8299 if( conf->psk != NULL )
8300 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008301 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008302 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00008303 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008304 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09008305 }
8306
8307 if( conf->psk_identity != NULL )
8308 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008309 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09008310 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00008311 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008312 conf->psk_identity_len = 0;
8313 }
8314#endif
8315
8316#if defined(MBEDTLS_X509_CRT_PARSE_C)
8317 ssl_key_cert_free( conf->key_cert );
8318#endif
8319
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008320 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008321}
8322
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008323#if defined(MBEDTLS_PK_C) && \
8324 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008325/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008329{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330#if defined(MBEDTLS_RSA_C)
8331 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8332 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008333#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334#if defined(MBEDTLS_ECDSA_C)
8335 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8336 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008337#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008338 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008339}
8340
Hanno Becker7e5437a2017-04-28 17:15:26 +01008341unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8342{
8343 switch( type ) {
8344 case MBEDTLS_PK_RSA:
8345 return( MBEDTLS_SSL_SIG_RSA );
8346 case MBEDTLS_PK_ECDSA:
8347 case MBEDTLS_PK_ECKEY:
8348 return( MBEDTLS_SSL_SIG_ECDSA );
8349 default:
8350 return( MBEDTLS_SSL_SIG_ANON );
8351 }
8352}
8353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008355{
8356 switch( sig )
8357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358#if defined(MBEDTLS_RSA_C)
8359 case MBEDTLS_SSL_SIG_RSA:
8360 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008361#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008362#if defined(MBEDTLS_ECDSA_C)
8363 case MBEDTLS_SSL_SIG_ECDSA:
8364 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008365#endif
8366 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008367 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008368 }
8369}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008370#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008371
Hanno Becker7e5437a2017-04-28 17:15:26 +01008372#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8373 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8374
8375/* Find an entry in a signature-hash set matching a given hash algorithm. */
8376mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8377 mbedtls_pk_type_t sig_alg )
8378{
8379 switch( sig_alg )
8380 {
8381 case MBEDTLS_PK_RSA:
8382 return( set->rsa );
8383 case MBEDTLS_PK_ECDSA:
8384 return( set->ecdsa );
8385 default:
8386 return( MBEDTLS_MD_NONE );
8387 }
8388}
8389
8390/* Add a signature-hash-pair to a signature-hash set */
8391void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8392 mbedtls_pk_type_t sig_alg,
8393 mbedtls_md_type_t md_alg )
8394{
8395 switch( sig_alg )
8396 {
8397 case MBEDTLS_PK_RSA:
8398 if( set->rsa == MBEDTLS_MD_NONE )
8399 set->rsa = md_alg;
8400 break;
8401
8402 case MBEDTLS_PK_ECDSA:
8403 if( set->ecdsa == MBEDTLS_MD_NONE )
8404 set->ecdsa = md_alg;
8405 break;
8406
8407 default:
8408 break;
8409 }
8410}
8411
8412/* Allow exactly one hash algorithm for each signature. */
8413void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8414 mbedtls_md_type_t md_alg )
8415{
8416 set->rsa = md_alg;
8417 set->ecdsa = md_alg;
8418}
8419
8420#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8421 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8422
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008423/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008424 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008426mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008427{
8428 switch( hash )
8429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008430#if defined(MBEDTLS_MD5_C)
8431 case MBEDTLS_SSL_HASH_MD5:
8432 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008433#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008434#if defined(MBEDTLS_SHA1_C)
8435 case MBEDTLS_SSL_HASH_SHA1:
8436 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008437#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008438#if defined(MBEDTLS_SHA256_C)
8439 case MBEDTLS_SSL_HASH_SHA224:
8440 return( MBEDTLS_MD_SHA224 );
8441 case MBEDTLS_SSL_HASH_SHA256:
8442 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008443#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008444#if defined(MBEDTLS_SHA512_C)
8445 case MBEDTLS_SSL_HASH_SHA384:
8446 return( MBEDTLS_MD_SHA384 );
8447 case MBEDTLS_SSL_HASH_SHA512:
8448 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008449#endif
8450 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008451 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008452 }
8453}
8454
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008455/*
8456 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8457 */
8458unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8459{
8460 switch( md )
8461 {
8462#if defined(MBEDTLS_MD5_C)
8463 case MBEDTLS_MD_MD5:
8464 return( MBEDTLS_SSL_HASH_MD5 );
8465#endif
8466#if defined(MBEDTLS_SHA1_C)
8467 case MBEDTLS_MD_SHA1:
8468 return( MBEDTLS_SSL_HASH_SHA1 );
8469#endif
8470#if defined(MBEDTLS_SHA256_C)
8471 case MBEDTLS_MD_SHA224:
8472 return( MBEDTLS_SSL_HASH_SHA224 );
8473 case MBEDTLS_MD_SHA256:
8474 return( MBEDTLS_SSL_HASH_SHA256 );
8475#endif
8476#if defined(MBEDTLS_SHA512_C)
8477 case MBEDTLS_MD_SHA384:
8478 return( MBEDTLS_SSL_HASH_SHA384 );
8479 case MBEDTLS_MD_SHA512:
8480 return( MBEDTLS_SSL_HASH_SHA512 );
8481#endif
8482 default:
8483 return( MBEDTLS_SSL_HASH_NONE );
8484 }
8485}
8486
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008487#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008488/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008489 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008490 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008491 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008492int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008493{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008494 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008495
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008496 if( ssl->conf->curve_list == NULL )
8497 return( -1 );
8498
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008499 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008500 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008501 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008502
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008503 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008504}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008505#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008506
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008507#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008508/*
8509 * Check if a hash proposed by the peer is in our list.
8510 * Return 0 if we're willing to use it, -1 otherwise.
8511 */
8512int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8513 mbedtls_md_type_t md )
8514{
8515 const int *cur;
8516
8517 if( ssl->conf->sig_hashes == NULL )
8518 return( -1 );
8519
8520 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8521 if( *cur == (int) md )
8522 return( 0 );
8523
8524 return( -1 );
8525}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008526#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008528#if defined(MBEDTLS_X509_CRT_PARSE_C)
8529int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8530 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008531 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008532 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008533{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008534 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008535#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008536 int usage = 0;
8537#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008538#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008539 const char *ext_oid;
8540 size_t ext_len;
8541#endif
8542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008543#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8544 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008545 ((void) cert);
8546 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008547 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008548#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008550#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8551 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008552 {
8553 /* Server part of the key exchange */
8554 switch( ciphersuite->key_exchange )
8555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556 case MBEDTLS_KEY_EXCHANGE_RSA:
8557 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008558 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008559 break;
8560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8562 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8563 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8564 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008565 break;
8566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008567 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8568 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008569 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008570 break;
8571
8572 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008573 case MBEDTLS_KEY_EXCHANGE_NONE:
8574 case MBEDTLS_KEY_EXCHANGE_PSK:
8575 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8576 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008577 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008578 usage = 0;
8579 }
8580 }
8581 else
8582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008583 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8584 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008585 }
8586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008588 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008589 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008590 ret = -1;
8591 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008592#else
8593 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008594#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8597 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8600 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008601 }
8602 else
8603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008604 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8605 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008606 }
8607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008609 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008610 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008611 ret = -1;
8612 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008614
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008615 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008616}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008617#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008618
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008619/*
8620 * Convert version numbers to/from wire format
8621 * and, for DTLS, to/from TLS equivalent.
8622 *
8623 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008624 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008625 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8626 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008629 unsigned char ver[2] )
8630{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008631#if defined(MBEDTLS_SSL_PROTO_DTLS)
8632 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008634 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008635 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8636
8637 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8638 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8639 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008640 else
8641#else
8642 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008643#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008644 {
8645 ver[0] = (unsigned char) major;
8646 ver[1] = (unsigned char) minor;
8647 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008648}
8649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008650void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008651 const unsigned char ver[2] )
8652{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008653#if defined(MBEDTLS_SSL_PROTO_DTLS)
8654 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008655 {
8656 *major = 255 - ver[0] + 2;
8657 *minor = 255 - ver[1] + 1;
8658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008659 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008660 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8661 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008662 else
8663#else
8664 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008665#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008666 {
8667 *major = ver[0];
8668 *minor = ver[1];
8669 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008670}
8671
Simon Butcher99000142016-10-13 17:21:01 +01008672int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8673{
8674#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8675 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8676 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8677
8678 switch( md )
8679 {
8680#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8681#if defined(MBEDTLS_MD5_C)
8682 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008683 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008684#endif
8685#if defined(MBEDTLS_SHA1_C)
8686 case MBEDTLS_SSL_HASH_SHA1:
8687 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8688 break;
8689#endif
8690#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8691#if defined(MBEDTLS_SHA512_C)
8692 case MBEDTLS_SSL_HASH_SHA384:
8693 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8694 break;
8695#endif
8696#if defined(MBEDTLS_SHA256_C)
8697 case MBEDTLS_SSL_HASH_SHA256:
8698 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8699 break;
8700#endif
8701 default:
8702 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8703 }
8704
8705 return 0;
8706#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8707 (void) ssl;
8708 (void) md;
8709
8710 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8711#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8712}
8713
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008714#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8715 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8716int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8717 unsigned char *output,
8718 unsigned char *data, size_t data_len )
8719{
8720 int ret = 0;
8721 mbedtls_md5_context mbedtls_md5;
8722 mbedtls_sha1_context mbedtls_sha1;
8723
8724 mbedtls_md5_init( &mbedtls_md5 );
8725 mbedtls_sha1_init( &mbedtls_sha1 );
8726
8727 /*
8728 * digitally-signed struct {
8729 * opaque md5_hash[16];
8730 * opaque sha_hash[20];
8731 * };
8732 *
8733 * md5_hash
8734 * MD5(ClientHello.random + ServerHello.random
8735 * + ServerParams);
8736 * sha_hash
8737 * SHA(ClientHello.random + ServerHello.random
8738 * + ServerParams);
8739 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008740 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008741 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008742 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008743 goto exit;
8744 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008745 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008746 ssl->handshake->randbytes, 64 ) ) != 0 )
8747 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008748 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008749 goto exit;
8750 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008751 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008752 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008754 goto exit;
8755 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008756 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008757 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008758 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008759 goto exit;
8760 }
8761
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008762 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008763 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008764 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008765 goto exit;
8766 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008767 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008768 ssl->handshake->randbytes, 64 ) ) != 0 )
8769 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008770 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008771 goto exit;
8772 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008773 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008774 data_len ) ) != 0 )
8775 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008776 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008777 goto exit;
8778 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008779 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008780 output + 16 ) ) != 0 )
8781 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008783 goto exit;
8784 }
8785
8786exit:
8787 mbedtls_md5_free( &mbedtls_md5 );
8788 mbedtls_sha1_free( &mbedtls_sha1 );
8789
8790 if( ret != 0 )
8791 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8792 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8793
8794 return( ret );
8795
8796}
8797#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
8798 MBEDTLS_SSL_PROTO_TLS1_1 */
8799
8800#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8801 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8802int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02008803 unsigned char *hash, size_t *hashlen,
8804 unsigned char *data, size_t data_len,
8805 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008806{
8807 int ret = 0;
8808 mbedtls_md_context_t ctx;
8809 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02008810 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008811
8812 mbedtls_md_init( &ctx );
8813
8814 /*
8815 * digitally-signed struct {
8816 * opaque client_random[32];
8817 * opaque server_random[32];
8818 * ServerDHParams params;
8819 * };
8820 */
8821 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8822 {
8823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8824 goto exit;
8825 }
8826 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8827 {
8828 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8829 goto exit;
8830 }
8831 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8832 {
8833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8834 goto exit;
8835 }
8836 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8837 {
8838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8839 goto exit;
8840 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02008841 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008842 {
8843 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8844 goto exit;
8845 }
8846
8847exit:
8848 mbedtls_md_free( &ctx );
8849
8850 if( ret != 0 )
8851 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8852 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8853
8854 return( ret );
8855}
8856#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
8857 MBEDTLS_SSL_PROTO_TLS1_2 */
8858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008859#endif /* MBEDTLS_SSL_TLS_C */