blob: 3a972a59829801d892c35527b76b556830b22432 [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 &&
Hanno Becker081bd812018-08-22 16:07:59 +01003145 ( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
3146 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003147 {
3148 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003151 return( ret );
3152 }
3153 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003154 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003155#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003156 {
3157 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
3158 {
3159 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3160 return( ret );
3161 }
3162 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003163
3164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3165
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003166 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003167}
3168
3169/*
3170 * Record layer functions
3171 */
3172
3173/*
3174 * Write current record.
3175 *
3176 * Uses:
3177 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3178 * - ssl->out_msglen: length of the record content (excl headers)
3179 * - ssl->out_msg: record content
3180 */
3181int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
3182{
3183 int ret, done = 0;
3184 size_t len = ssl->out_msglen;
3185
3186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003189 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003191 {
3192 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003195 return( ret );
3196 }
3197
3198 len = ssl->out_msglen;
3199 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3203 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 ret = mbedtls_ssl_hw_record_write( ssl );
3208 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3211 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003212 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003213
3214 if( ret == 0 )
3215 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003216 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003218 if( !done )
3219 {
3220 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003222 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003223
3224 ssl->out_len[0] = (unsigned char)( len >> 8 );
3225 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003226
Paul Bakker48916f92012-09-16 19:57:18 +00003227 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003228 {
3229 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003232 return( ret );
3233 }
3234
3235 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003236 ssl->out_len[0] = (unsigned char)( len >> 8 );
3237 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003238 }
3239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00003241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00003243 "version = [%d:%d], msglen = %d",
3244 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003245 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3248 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 }
3250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003254 return( ret );
3255 }
3256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003258
3259 return( 0 );
3260}
3261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003263/*
3264 * Mark bits in bitmask (used for DTLS HS reassembly)
3265 */
3266static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3267{
3268 unsigned int start_bits, end_bits;
3269
3270 start_bits = 8 - ( offset % 8 );
3271 if( start_bits != 8 )
3272 {
3273 size_t first_byte_idx = offset / 8;
3274
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003275 /* Special case */
3276 if( len <= start_bits )
3277 {
3278 for( ; len != 0; len-- )
3279 mask[first_byte_idx] |= 1 << ( start_bits - len );
3280
3281 /* Avoid potential issues with offset or len becoming invalid */
3282 return;
3283 }
3284
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003285 offset += start_bits; /* Now offset % 8 == 0 */
3286 len -= start_bits;
3287
3288 for( ; start_bits != 0; start_bits-- )
3289 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3290 }
3291
3292 end_bits = len % 8;
3293 if( end_bits != 0 )
3294 {
3295 size_t last_byte_idx = ( offset + len ) / 8;
3296
3297 len -= end_bits; /* Now len % 8 == 0 */
3298
3299 for( ; end_bits != 0; end_bits-- )
3300 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3301 }
3302
3303 memset( mask + offset / 8, 0xFF, len / 8 );
3304}
3305
3306/*
3307 * Check that bitmask is full
3308 */
3309static int ssl_bitmask_check( unsigned char *mask, size_t len )
3310{
3311 size_t i;
3312
3313 for( i = 0; i < len / 8; i++ )
3314 if( mask[i] != 0xFF )
3315 return( -1 );
3316
3317 for( i = 0; i < len % 8; i++ )
3318 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3319 return( -1 );
3320
3321 return( 0 );
3322}
3323
3324/*
3325 * Reassemble fragmented DTLS handshake messages.
3326 *
3327 * Use a temporary buffer for reassembly, divided in two parts:
3328 * - the first holds the reassembled message (including handshake header),
3329 * - the second holds a bitmask indicating which parts of the message
3330 * (excluding headers) have been received so far.
3331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003333{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003334 unsigned char *msg, *bitmask;
3335 size_t frag_len, frag_off;
3336 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3337
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003338 if( ssl->handshake == NULL )
3339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3341 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003342 }
3343
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003344 /*
3345 * For first fragment, check size and allocate buffer
3346 */
3347 if( ssl->handshake->hs_msg == NULL )
3348 {
3349 size_t alloc_len;
3350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003352 msg_len ) );
3353
Angus Grattond8213d02016-05-25 20:56:48 +10003354 if( ssl->in_hslen > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3357 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003358 }
3359
3360 /* The bitmask needs one bit per byte of message excluding header */
3361 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3362
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003363 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003364 if( ssl->handshake->hs_msg == NULL )
3365 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003367 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003368 }
3369
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003370 /* Prepare final header: copy msg_type, length and message_seq,
3371 * then add standardised fragment_offset and fragment_length */
3372 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3373 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3374 memcpy( ssl->handshake->hs_msg + 9,
3375 ssl->handshake->hs_msg + 1, 3 );
3376 }
3377 else
3378 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003379 /* Make sure msg_type and length are consistent */
3380 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3383 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003384 }
3385 }
3386
3387 msg = ssl->handshake->hs_msg + 12;
3388 bitmask = msg + msg_len;
3389
3390 /*
3391 * Check and copy current fragment
3392 */
3393 frag_off = ( ssl->in_msg[6] << 16 ) |
3394 ( ssl->in_msg[7] << 8 ) |
3395 ssl->in_msg[8];
3396 frag_len = ( ssl->in_msg[9] << 16 ) |
3397 ( ssl->in_msg[10] << 8 ) |
3398 ssl->in_msg[11];
3399
3400 if( frag_off + frag_len > msg_len )
3401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003403 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003405 }
3406
3407 if( frag_len + 12 > ssl->in_msglen )
3408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003410 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003412 }
3413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003415 frag_off, frag_len ) );
3416
3417 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3418 ssl_bitmask_set( bitmask, frag_off, frag_len );
3419
3420 /*
3421 * Do we have the complete message by now?
3422 * If yes, finalize it, else ask to read the next record.
3423 */
3424 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01003427 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003428 }
3429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003431
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003432 if( frag_len + 12 < ssl->in_msglen )
3433 {
3434 /*
3435 * We'got more handshake messages in the same record.
3436 * This case is not handled now because no know implementation does
3437 * that and it's hard to test, so we prefer to fail cleanly for now.
3438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3440 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003441 }
3442
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003443 if( ssl->in_left > ssl->next_record_offset )
3444 {
3445 /*
3446 * We've got more data in the buffer after the current record,
3447 * that we don't want to overwrite. Move it before writing the
3448 * reassembled message, and adjust in_left and next_record_offset.
3449 */
3450 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3451 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3452 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3453
3454 /* First compute and check new lengths */
3455 ssl->next_record_offset = new_remain - ssl->in_hdr;
3456 ssl->in_left = ssl->next_record_offset + remain_len;
3457
Angus Grattond8213d02016-05-25 20:56:48 +10003458 if( ssl->in_left > MBEDTLS_SSL_IN_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003459 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3462 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003463 }
3464
3465 memmove( new_remain, cur_remain, remain_len );
3466 }
3467
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003468 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003471 ssl->handshake->hs_msg = NULL;
3472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003474 ssl->in_msg, ssl->in_hslen );
3475
3476 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003479
Simon Butcher99000142016-10-13 17:21:01 +01003480int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003481{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003485 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003487 }
3488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003490 ( ssl->in_msg[1] << 16 ) |
3491 ( ssl->in_msg[2] << 8 ) |
3492 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003495 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003496 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003499 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003500 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003501 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003502 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003503
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003504 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003505 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3506 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3507 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3508 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003509 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003510 /* Retransmit only on last message from previous flight, to avoid
3511 * too many retransmissions.
3512 * Besides, No sane server ever retransmits HelloVerifyRequest */
3513 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003517 "message_seq = %d, start_of_flight = %d",
3518 recv_msg_seq,
3519 ssl->handshake->in_flight_start_seq ) );
3520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003524 return( ret );
3525 }
3526 }
3527 else
3528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003530 "message_seq = %d, expected = %d",
3531 recv_msg_seq,
3532 ssl->handshake->in_msg_seq ) );
3533 }
3534
Hanno Becker90333da2017-10-10 11:27:13 +01003535 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003536 }
3537 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003538
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003539 /* Reassemble if current message is fragmented or reassembly is
3540 * already in progress */
3541 if( ssl->in_msglen < ssl->in_hslen ||
3542 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3543 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3544 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003547
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003548 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003551 return( ret );
3552 }
3553 }
3554 }
3555 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003557 /* With TLS we don't handle fragmentation (for now) */
3558 if( ssl->in_msglen < ssl->in_hslen )
3559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3561 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003562 }
3563
Simon Butcher99000142016-10-13 17:21:01 +01003564 return( 0 );
3565}
3566
3567void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3568{
3569
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003570 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3571 ssl->handshake != NULL )
3572 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003573 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003574 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003575
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003576 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003578 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003579 ssl->handshake != NULL )
3580 {
3581 ssl->handshake->in_msg_seq++;
3582 }
3583#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003584}
3585
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003586/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003587 * DTLS anti-replay: RFC 6347 4.1.2.6
3588 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003589 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3590 * Bit n is set iff record number in_window_top - n has been seen.
3591 *
3592 * Usually, in_window_top is the last record number seen and the lsb of
3593 * in_window is set. The only exception is the initial state (record number 0
3594 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3597static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003598{
3599 ssl->in_window_top = 0;
3600 ssl->in_window = 0;
3601}
3602
3603static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3604{
3605 return( ( (uint64_t) buf[0] << 40 ) |
3606 ( (uint64_t) buf[1] << 32 ) |
3607 ( (uint64_t) buf[2] << 24 ) |
3608 ( (uint64_t) buf[3] << 16 ) |
3609 ( (uint64_t) buf[4] << 8 ) |
3610 ( (uint64_t) buf[5] ) );
3611}
3612
3613/*
3614 * Return 0 if sequence number is acceptable, -1 otherwise
3615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003617{
3618 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3619 uint64_t bit;
3620
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003621 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003622 return( 0 );
3623
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003624 if( rec_seqnum > ssl->in_window_top )
3625 return( 0 );
3626
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003627 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003628
3629 if( bit >= 64 )
3630 return( -1 );
3631
3632 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3633 return( -1 );
3634
3635 return( 0 );
3636}
3637
3638/*
3639 * Update replay window on new validated record
3640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003642{
3643 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3644
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003645 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003646 return;
3647
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003648 if( rec_seqnum > ssl->in_window_top )
3649 {
3650 /* Update window_top and the contents of the window */
3651 uint64_t shift = rec_seqnum - ssl->in_window_top;
3652
3653 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003654 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003655 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003656 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003657 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003658 ssl->in_window |= 1;
3659 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003660
3661 ssl->in_window_top = rec_seqnum;
3662 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003663 else
3664 {
3665 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003666 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003667
3668 if( bit < 64 ) /* Always true, but be extra sure */
3669 ssl->in_window |= (uint64_t) 1 << bit;
3670 }
3671}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003673
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003674#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003675/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003676static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3677
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003678/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003679 * Without any SSL context, check if a datagram looks like a ClientHello with
3680 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003681 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003682 *
3683 * - if cookie is valid, return 0
3684 * - if ClientHello looks superficially valid but cookie is not,
3685 * fill obuf and set olen, then
3686 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3687 * - otherwise return a specific error code
3688 */
3689static int ssl_check_dtls_clihlo_cookie(
3690 mbedtls_ssl_cookie_write_t *f_cookie_write,
3691 mbedtls_ssl_cookie_check_t *f_cookie_check,
3692 void *p_cookie,
3693 const unsigned char *cli_id, size_t cli_id_len,
3694 const unsigned char *in, size_t in_len,
3695 unsigned char *obuf, size_t buf_len, size_t *olen )
3696{
3697 size_t sid_len, cookie_len;
3698 unsigned char *p;
3699
3700 if( f_cookie_write == NULL || f_cookie_check == NULL )
3701 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3702
3703 /*
3704 * Structure of ClientHello with record and handshake headers,
3705 * and expected values. We don't need to check a lot, more checks will be
3706 * done when actually parsing the ClientHello - skipping those checks
3707 * avoids code duplication and does not make cookie forging any easier.
3708 *
3709 * 0-0 ContentType type; copied, must be handshake
3710 * 1-2 ProtocolVersion version; copied
3711 * 3-4 uint16 epoch; copied, must be 0
3712 * 5-10 uint48 sequence_number; copied
3713 * 11-12 uint16 length; (ignored)
3714 *
3715 * 13-13 HandshakeType msg_type; (ignored)
3716 * 14-16 uint24 length; (ignored)
3717 * 17-18 uint16 message_seq; copied
3718 * 19-21 uint24 fragment_offset; copied, must be 0
3719 * 22-24 uint24 fragment_length; (ignored)
3720 *
3721 * 25-26 ProtocolVersion client_version; (ignored)
3722 * 27-58 Random random; (ignored)
3723 * 59-xx SessionID session_id; 1 byte len + sid_len content
3724 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3725 * ...
3726 *
3727 * Minimum length is 61 bytes.
3728 */
3729 if( in_len < 61 ||
3730 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3731 in[3] != 0 || in[4] != 0 ||
3732 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3733 {
3734 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3735 }
3736
3737 sid_len = in[59];
3738 if( sid_len > in_len - 61 )
3739 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3740
3741 cookie_len = in[60 + sid_len];
3742 if( cookie_len > in_len - 60 )
3743 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3744
3745 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3746 cli_id, cli_id_len ) == 0 )
3747 {
3748 /* Valid cookie */
3749 return( 0 );
3750 }
3751
3752 /*
3753 * If we get here, we've got an invalid cookie, let's prepare HVR.
3754 *
3755 * 0-0 ContentType type; copied
3756 * 1-2 ProtocolVersion version; copied
3757 * 3-4 uint16 epoch; copied
3758 * 5-10 uint48 sequence_number; copied
3759 * 11-12 uint16 length; olen - 13
3760 *
3761 * 13-13 HandshakeType msg_type; hello_verify_request
3762 * 14-16 uint24 length; olen - 25
3763 * 17-18 uint16 message_seq; copied
3764 * 19-21 uint24 fragment_offset; copied
3765 * 22-24 uint24 fragment_length; olen - 25
3766 *
3767 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3768 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3769 *
3770 * Minimum length is 28.
3771 */
3772 if( buf_len < 28 )
3773 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3774
3775 /* Copy most fields and adapt others */
3776 memcpy( obuf, in, 25 );
3777 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3778 obuf[25] = 0xfe;
3779 obuf[26] = 0xff;
3780
3781 /* Generate and write actual cookie */
3782 p = obuf + 28;
3783 if( f_cookie_write( p_cookie,
3784 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3785 {
3786 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3787 }
3788
3789 *olen = p - obuf;
3790
3791 /* Go back and fill length fields */
3792 obuf[27] = (unsigned char)( *olen - 28 );
3793
3794 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3795 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3796 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3797
3798 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3799 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3800
3801 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3802}
3803
3804/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003805 * Handle possible client reconnect with the same UDP quadruplet
3806 * (RFC 6347 Section 4.2.8).
3807 *
3808 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3809 * that looks like a ClientHello.
3810 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003811 * - if the input looks like a ClientHello without cookies,
3812 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003813 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003814 * - if the input looks like a ClientHello with a valid cookie,
3815 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003816 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003817 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003818 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003819 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003820 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3821 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003822 */
3823static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3824{
3825 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003826 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003827
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003828 ret = ssl_check_dtls_clihlo_cookie(
3829 ssl->conf->f_cookie_write,
3830 ssl->conf->f_cookie_check,
3831 ssl->conf->p_cookie,
3832 ssl->cli_id, ssl->cli_id_len,
3833 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003834 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003835
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003836 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3837
3838 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003839 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003840 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003841 * If the error is permanent we'll catch it later,
3842 * if it's not, then hopefully it'll work next time. */
3843 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3844
3845 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003846 }
3847
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003848 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003849 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003850 /* Got a valid cookie, partially reset context */
3851 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3852 {
3853 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3854 return( ret );
3855 }
3856
3857 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003858 }
3859
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003860 return( ret );
3861}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003862#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003863
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003864/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003865 * ContentType type;
3866 * ProtocolVersion version;
3867 * uint16 epoch; // DTLS only
3868 * uint48 sequence_number; // DTLS only
3869 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003870 *
3871 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003872 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003873 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3874 *
3875 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003876 * 1. proceed with the record if this function returns 0
3877 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3878 * 3. return CLIENT_RECONNECT if this function return that value
3879 * 4. drop the whole datagram if this function returns anything else.
3880 * Point 2 is needed when the peer is resending, and we have already received
3881 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003884{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003885 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 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 +02003888
Paul Bakker5121ce52009-01-03 21:22:43 +00003889 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003890 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003891 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003894 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003895 ssl->in_msgtype,
3896 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003897
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003898 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003899 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3900 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3901 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3902 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003905
3906#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01003907 /* Silently ignore invalid DTLS records as recommended by RFC 6347
3908 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003909 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3910#endif /* MBEDTLS_SSL_PROTO_DTLS */
3911 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3912 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
3913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003915 }
3916
3917 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003918 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3921 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003922 }
3923
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003924 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3927 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003928 }
3929
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003930 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10003931 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003932 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3935 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003936 }
3937
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003938 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003939 * DTLS-related tests.
3940 * Check epoch before checking length constraint because
3941 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3942 * message gets duplicated before the corresponding Finished message,
3943 * the second ChangeCipherSpec should be discarded because it belongs
3944 * to an old epoch, but not because its length is shorter than
3945 * the minimum record length for packets using the new record transform.
3946 * Note that these two kinds of failures are handled differently,
3947 * as an unexpected record is silently skipped but an invalid
3948 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003949 */
3950#if defined(MBEDTLS_SSL_PROTO_DTLS)
3951 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3952 {
3953 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3954
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003955 /* Check epoch (and sequence number) with DTLS */
3956 if( rec_epoch != ssl->in_epoch )
3957 {
3958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3959 "expected %d, received %d",
3960 ssl->in_epoch, rec_epoch ) );
3961
3962#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3963 /*
3964 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3965 * access the first byte of record content (handshake type), as we
3966 * have an active transform (possibly iv_len != 0), so use the
3967 * fact that the record header len is 13 instead.
3968 */
3969 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3970 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3971 rec_epoch == 0 &&
3972 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3973 ssl->in_left > 13 &&
3974 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3975 {
3976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3977 "from the same port" ) );
3978 return( ssl_handle_possible_reconnect( ssl ) );
3979 }
3980 else
3981#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3982 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3983 }
3984
3985#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3986 /* Replay detection only works for the current epoch */
3987 if( rec_epoch == ssl->in_epoch &&
3988 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3989 {
3990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3991 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3992 }
3993#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01003994
3995 /* Drop unexpected ChangeCipherSpec messages */
3996 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3997 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3998 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3999 {
4000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
4001 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4002 }
4003
4004 /* Drop unexpected ApplicationData records,
4005 * except at the beginning of renegotiations */
4006 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4007 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4008#if defined(MBEDTLS_SSL_RENEGOTIATION)
4009 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4010 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4011#endif
4012 )
4013 {
4014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4015 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4016 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004017 }
4018#endif /* MBEDTLS_SSL_PROTO_DTLS */
4019
Hanno Becker52c6dc62017-05-26 16:07:36 +01004020
4021 /* Check length against bounds of the current transform and version */
4022 if( ssl->transform_in == NULL )
4023 {
4024 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004025 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004026 {
4027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4028 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4029 }
4030 }
4031 else
4032 {
4033 if( ssl->in_msglen < ssl->transform_in->minlen )
4034 {
4035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4036 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4037 }
4038
4039#if defined(MBEDTLS_SSL_PROTO_SSL3)
4040 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004041 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004042 {
4043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4044 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4045 }
4046#endif
4047#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4048 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4049 /*
4050 * TLS encrypted messages can have up to 256 bytes of padding
4051 */
4052 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4053 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004054 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004055 {
4056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4057 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4058 }
4059#endif
4060 }
4061
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004062 return( 0 );
4063}
Paul Bakker5121ce52009-01-03 21:22:43 +00004064
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004065/*
4066 * If applicable, decrypt (and decompress) record content
4067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004068static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004069{
4070 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4073 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4076 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080 ret = mbedtls_ssl_hw_record_read( ssl );
4081 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004083 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4084 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004085 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004086
4087 if( ret == 0 )
4088 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004089 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004091 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004092 {
4093 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004096 return( ret );
4097 }
4098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004100 ssl->in_msg, ssl->in_msglen );
4101
Angus Grattond8213d02016-05-25 20:56:48 +10004102 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4105 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004106 }
4107 }
4108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004110 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004111 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004112 {
4113 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004115 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004116 return( ret );
4117 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004118 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004121#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004122 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004125 }
4126#endif
4127
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004128 return( 0 );
4129}
4130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004131static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004132
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004133/*
4134 * Read a record.
4135 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004136 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4137 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4138 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004141{
4142 int ret;
4143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004145
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004146 if( ssl->keep_current_message == 0 )
4147 {
4148 do {
Simon Butcher99000142016-10-13 17:21:01 +01004149
Hanno Becker90333da2017-10-10 11:27:13 +01004150 do ret = mbedtls_ssl_read_record_layer( ssl );
4151 while( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
4152
4153 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004154 {
4155 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4156 return( ret );
4157 }
4158
4159 ret = mbedtls_ssl_handle_message_type( ssl );
4160
Hanno Becker90333da2017-10-10 11:27:13 +01004161 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4162 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004163
4164 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004165 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004166 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004167 return( ret );
4168 }
4169
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004170 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4171 {
4172 mbedtls_ssl_update_handshake_status( ssl );
4173 }
Simon Butcher99000142016-10-13 17:21:01 +01004174 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004175 else
Simon Butcher99000142016-10-13 17:21:01 +01004176 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4178 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004179 }
4180
4181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4182
4183 return( 0 );
4184}
4185
4186int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4187{
4188 int ret;
4189
Hanno Becker4a810fb2017-05-24 16:27:30 +01004190 /*
4191 * Step A
4192 *
4193 * Consume last content-layer message and potentially
4194 * update in_msglen which keeps track of the contents'
4195 * consumption state.
4196 *
4197 * (1) Handshake messages:
4198 * Remove last handshake message, move content
4199 * and adapt in_msglen.
4200 *
4201 * (2) Alert messages:
4202 * Consume whole record content, in_msglen = 0.
4203 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004204 * (3) Change cipher spec:
4205 * Consume whole record content, in_msglen = 0.
4206 *
4207 * (4) Application data:
4208 * Don't do anything - the record layer provides
4209 * the application data as a stream transport
4210 * and consumes through mbedtls_ssl_read only.
4211 *
4212 */
4213
4214 /* Case (1): Handshake messages */
4215 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004216 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004217 /* Hard assertion to be sure that no application data
4218 * is in flight, as corrupting ssl->in_msglen during
4219 * ssl->in_offt != NULL is fatal. */
4220 if( ssl->in_offt != NULL )
4221 {
4222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4223 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4224 }
4225
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004226 /*
4227 * Get next Handshake message in the current record
4228 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004229
Hanno Becker4a810fb2017-05-24 16:27:30 +01004230 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004231 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004232 * current handshake content: If DTLS handshake
4233 * fragmentation is used, that's the fragment
4234 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004235 * size here is faulty and should be changed at
4236 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004237 * (2) While it doesn't seem to cause problems, one
4238 * has to be very careful not to assume that in_hslen
4239 * is always <= in_msglen in a sensible communication.
4240 * Again, it's wrong for DTLS handshake fragmentation.
4241 * The following check is therefore mandatory, and
4242 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004243 * Additionally, ssl->in_hslen might be arbitrarily out of
4244 * bounds after handling a DTLS message with an unexpected
4245 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004246 */
4247 if( ssl->in_hslen < ssl->in_msglen )
4248 {
4249 ssl->in_msglen -= ssl->in_hslen;
4250 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4251 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004252
Hanno Becker4a810fb2017-05-24 16:27:30 +01004253 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4254 ssl->in_msg, ssl->in_msglen );
4255 }
4256 else
4257 {
4258 ssl->in_msglen = 0;
4259 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004260
Hanno Becker4a810fb2017-05-24 16:27:30 +01004261 ssl->in_hslen = 0;
4262 }
4263 /* Case (4): Application data */
4264 else if( ssl->in_offt != NULL )
4265 {
4266 return( 0 );
4267 }
4268 /* Everything else (CCS & Alerts) */
4269 else
4270 {
4271 ssl->in_msglen = 0;
4272 }
4273
4274 /*
4275 * Step B
4276 *
4277 * Fetch and decode new record if current one is fully consumed.
4278 *
4279 */
4280
4281 if( ssl->in_msglen > 0 )
4282 {
4283 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004284 return( 0 );
4285 }
4286
Hanno Becker4a810fb2017-05-24 16:27:30 +01004287 /* Current record either fully processed or to be discarded. */
4288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004292 return( ret );
4293 }
4294
4295 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004298 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4299 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004300 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004301 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4302 {
4303 /* Skip unexpected record (but not whole datagram) */
4304 ssl->next_record_offset = ssl->in_msglen
4305 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004306
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4308 "(header)" ) );
4309 }
4310 else
4311 {
4312 /* Skip invalid record and the rest of the datagram */
4313 ssl->next_record_offset = 0;
4314 ssl->in_left = 0;
4315
4316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4317 "(header)" ) );
4318 }
4319
4320 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004321 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004322 }
4323#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004324 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004325 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004326
4327 /*
4328 * Read and optionally decrypt the message contents
4329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004330 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4331 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004334 return( ret );
4335 }
4336
4337 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004339 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004342 if( ssl->next_record_offset < ssl->in_left )
4343 {
4344 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4345 }
4346 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004347 else
4348#endif
4349 ssl->in_left = 0;
4350
4351 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004353#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004354 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004355 {
4356 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4358 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004359 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004360 /* Except when waiting for Finished as a bad mac here
4361 * probably means something went wrong in the handshake
4362 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4363 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4364 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4365 {
4366#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4367 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4368 {
4369 mbedtls_ssl_send_alert_message( ssl,
4370 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4371 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4372 }
4373#endif
4374 return( ret );
4375 }
4376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004377#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004378 if( ssl->conf->badmac_limit != 0 &&
4379 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4382 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004383 }
4384#endif
4385
Hanno Becker4a810fb2017-05-24 16:27:30 +01004386 /* As above, invalid records cause
4387 * dismissal of the whole datagram. */
4388
4389 ssl->next_record_offset = 0;
4390 ssl->in_left = 0;
4391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004393 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004394 }
4395
4396 return( ret );
4397 }
4398 else
4399#endif
4400 {
4401 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004402#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4403 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004405 mbedtls_ssl_send_alert_message( ssl,
4406 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4407 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004408 }
4409#endif
4410 return( ret );
4411 }
4412 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004413
Simon Butcher99000142016-10-13 17:21:01 +01004414 return( 0 );
4415}
4416
4417int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4418{
4419 int ret;
4420
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004421 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004422 * Handle particular types of records
4423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004424 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004425 {
Simon Butcher99000142016-10-13 17:21:01 +01004426 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4427 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004428 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004429 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004430 }
4431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004433 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004434 if( ssl->in_msglen != 2 )
4435 {
4436 /* Note: Standard allows for more than one 2 byte alert
4437 to be packed in a single message, but Mbed TLS doesn't
4438 currently support this. */
4439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4440 ssl->in_msglen ) );
4441 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4442 }
4443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004445 ssl->in_msg[0], ssl->in_msg[1] ) );
4446
4447 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004448 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004450 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004453 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004455 }
4456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4458 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4461 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004462 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004463
4464#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4465 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4466 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4467 {
Hanno Becker90333da2017-10-10 11:27:13 +01004468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004469 /* Will be handled when trying to parse ServerHello */
4470 return( 0 );
4471 }
4472#endif
4473
4474#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4475 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4476 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4477 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4478 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4479 {
4480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4481 /* Will be handled in mbedtls_ssl_parse_certificate() */
4482 return( 0 );
4483 }
4484#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4485
4486 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004487 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004488 }
4489
Hanno Beckerc76c6192017-06-06 10:03:17 +01004490#if defined(MBEDTLS_SSL_PROTO_DTLS)
4491 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4492 ssl->handshake != NULL &&
4493 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4494 {
4495 ssl_handshake_wrapup_free_hs_transform( ssl );
4496 }
4497#endif
4498
Paul Bakker5121ce52009-01-03 21:22:43 +00004499 return( 0 );
4500}
4501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004502int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004503{
4504 int ret;
4505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4507 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4508 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004509 {
4510 return( ret );
4511 }
4512
4513 return( 0 );
4514}
4515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004517 unsigned char level,
4518 unsigned char message )
4519{
4520 int ret;
4521
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004522 if( ssl == NULL || ssl->conf == NULL )
4523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004526 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004529 ssl->out_msglen = 2;
4530 ssl->out_msg[0] = level;
4531 ssl->out_msg[1] = message;
4532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004533 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004536 return( ret );
4537 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004539
4540 return( 0 );
4541}
4542
Paul Bakker5121ce52009-01-03 21:22:43 +00004543/*
4544 * Handshake functions
4545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4547 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4548 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4549 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4550 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4551 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4552 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004553/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004554int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004555{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004556 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004560 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4561 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004562 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4563 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004566 ssl->state++;
4567 return( 0 );
4568 }
4569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4571 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004572}
4573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004575{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004580 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4581 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004582 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4583 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004586 ssl->state++;
4587 return( 0 );
4588 }
4589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4591 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004592}
Gilles Peskinef9828522017-05-03 12:28:43 +02004593
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004594#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004595/* Some certificate support -> implement write and parse */
4596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004598{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004600 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004601 const mbedtls_x509_crt *crt;
4602 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004606 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4607 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004608 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4609 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004612 ssl->state++;
4613 return( 0 );
4614 }
4615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004616#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004617 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004618 {
4619 if( ssl->client_auth == 0 )
4620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004622 ssl->state++;
4623 return( 0 );
4624 }
4625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004626#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004627 /*
4628 * If using SSLv3 and got no cert, send an Alert message
4629 * (otherwise an empty Certificate message will be sent).
4630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4632 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004633 {
4634 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4636 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4637 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004640 goto write_msg;
4641 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004642#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004643 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644#endif /* MBEDTLS_SSL_CLI_C */
4645#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004646 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4651 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004652 }
4653 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004654#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004657
4658 /*
4659 * 0 . 0 handshake type
4660 * 1 . 3 handshake length
4661 * 4 . 6 length of all certs
4662 * 7 . 9 length of cert. 1
4663 * 10 . n-1 peer certificate
4664 * n . n+2 length of cert. 2
4665 * n+3 . ... upper level cert, etc.
4666 */
4667 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004668 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004669
Paul Bakker29087132010-03-21 21:03:34 +00004670 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004671 {
4672 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10004673 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10004676 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004677 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004678 }
4679
4680 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4681 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4682 ssl->out_msg[i + 2] = (unsigned char)( n );
4683
4684 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4685 i += n; crt = crt->next;
4686 }
4687
4688 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4689 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4690 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4691
4692 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004693 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4694 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004695
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004696#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004697write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004698#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004699
4700 ssl->state++;
4701
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004702 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004703 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004704 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004705 return( ret );
4706 }
4707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004709
Paul Bakkered27a042013-04-18 22:46:23 +02004710 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004711}
4712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004713int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004714{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004715 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004716 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004718 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004719 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4724 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004725 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4726 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004729 ssl->state++;
4730 return( 0 );
4731 }
4732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004733#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004734 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004735 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4736 {
4737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4738 ssl->state++;
4739 return( 0 );
4740 }
4741
4742#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4743 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4744 authmode = ssl->handshake->sni_authmode;
4745#endif
4746
4747 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4748 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004749 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004750 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004752 ssl->state++;
4753 return( 0 );
4754 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004755#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004757 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004758 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004759 /* mbedtls_ssl_read_record may have sent an alert already. We
4760 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004762 return( ret );
4763 }
4764
4765 ssl->state++;
4766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004767#if defined(MBEDTLS_SSL_SRV_C)
4768#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004769 /*
4770 * Check if the client sent an empty certificate
4771 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004772 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004774 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004775 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004776 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4777 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4778 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004781
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004782 /* The client was asked for a certificate but didn't send
4783 one. The client should know what's going on, so we
4784 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004785 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004786 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004787 return( 0 );
4788 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004790 }
4791 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004792#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4795 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004796 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004799 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4800 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4801 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4802 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004805
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004806 /* The client was asked for a certificate but didn't send
4807 one. The client should know what's going on, so we
4808 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004809 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004810 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004811 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004812 else
4813 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004814 }
4815 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4817 MBEDTLS_SSL_PROTO_TLS1_2 */
4818#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004823 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4824 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004825 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004826 }
4827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4829 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004832 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4833 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004834 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004835 }
4836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004838
Paul Bakker5121ce52009-01-03 21:22:43 +00004839 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004840 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004841 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004842 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004843
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004844 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004848 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4849 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004851 }
4852
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004853 /* In case we tried to reuse a session but it failed */
4854 if( ssl->session_negotiate->peer_cert != NULL )
4855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4857 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004858 }
4859
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004860 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004861 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004862 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004864 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004865 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4866 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004867 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004868 }
4869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004870 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004871
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004872 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004873
4874 while( i < ssl->in_hslen )
4875 {
Philippe Antoine747fd532018-05-30 09:13:21 +02004876 if ( i + 3 > ssl->in_hslen ) {
4877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4878 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4879 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4880 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
4881 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004882 if( ssl->in_msg[i] != 0 )
4883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004885 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4886 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004887 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004888 }
4889
4890 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4891 | (unsigned int) ssl->in_msg[i + 2];
4892 i += 3;
4893
4894 if( n < 128 || i + n > ssl->in_hslen )
4895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004897 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4898 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004899 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004900 }
4901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004903 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004904 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004905 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004906 case 0: /*ok*/
4907 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
4908 /* Ignore certificate with an unknown algorithm: maybe a
4909 prior certificate was already trusted. */
4910 break;
4911
4912 case MBEDTLS_ERR_X509_ALLOC_FAILED:
4913 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
4914 goto crt_parse_der_failed;
4915
4916 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
4917 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4918 goto crt_parse_der_failed;
4919
4920 default:
4921 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4922 crt_parse_der_failed:
4923 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004925 return( ret );
4926 }
4927
4928 i += n;
4929 }
4930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004931 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004932
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004933 /*
4934 * On client, make sure the server cert doesn't change during renego to
4935 * avoid "triple handshake" attack: https://secure-resumption.com/
4936 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004937#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004938 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004940 {
4941 if( ssl->session->peer_cert == NULL )
4942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004944 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4945 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004947 }
4948
4949 if( ssl->session->peer_cert->raw.len !=
4950 ssl->session_negotiate->peer_cert->raw.len ||
4951 memcmp( ssl->session->peer_cert->raw.p,
4952 ssl->session_negotiate->peer_cert->raw.p,
4953 ssl->session->peer_cert->raw.len ) != 0 )
4954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004956 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4957 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004958 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004959 }
4960 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004961#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004962
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004963 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004964 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004965 mbedtls_x509_crt *ca_chain;
4966 mbedtls_x509_crl *ca_crl;
4967
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004968#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004969 if( ssl->handshake->sni_ca_chain != NULL )
4970 {
4971 ca_chain = ssl->handshake->sni_ca_chain;
4972 ca_crl = ssl->handshake->sni_ca_crl;
4973 }
4974 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004975#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004976 {
4977 ca_chain = ssl->conf->ca_chain;
4978 ca_crl = ssl->conf->ca_crl;
4979 }
4980
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004981 /*
4982 * Main check: verify certificate
4983 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004984 ret = mbedtls_x509_crt_verify_with_profile(
4985 ssl->session_negotiate->peer_cert,
4986 ca_chain, ca_crl,
4987 ssl->conf->cert_profile,
4988 ssl->hostname,
4989 &ssl->session_negotiate->verify_result,
4990 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004991
4992 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004994 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004995 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004996
4997 /*
4998 * Secondary checks: always done, but change 'ret' only if it was 0
4999 */
5000
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005001#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005003 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005004
5005 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005006 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005007 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005008 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005009 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005012 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005013 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005014 }
5015 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005016#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005018 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005019 ciphersuite_info,
5020 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005021 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005024 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005025 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005026 }
5027
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005028 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5029 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5030 * with details encoded in the verification flags. All other kinds
5031 * of error codes, including those from the user provided f_vrfy
5032 * functions, are treated as fatal and lead to a failure of
5033 * ssl_parse_certificate even if verification was optional. */
5034 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5035 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5036 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5037 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005038 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005039 }
5040
5041 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5042 {
5043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5044 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5045 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005046
5047 if( ret != 0 )
5048 {
5049 /* The certificate may have been rejected for several reasons.
5050 Pick one and send the corresponding alert. Which alert to send
5051 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005052 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5053 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5054 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5055 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5056 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5057 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5058 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5059 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5060 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5061 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5062 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5063 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5064 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5065 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5066 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5067 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5068 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5069 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5070 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5071 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005072 else
5073 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005074 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5075 alert );
5076 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005077
Hanno Beckere6706e62017-05-15 16:05:15 +01005078#if defined(MBEDTLS_DEBUG_C)
5079 if( ssl->session_negotiate->verify_result != 0 )
5080 {
5081 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5082 ssl->session_negotiate->verify_result ) );
5083 }
5084 else
5085 {
5086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5087 }
5088#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005089 }
5090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005092
5093 return( ret );
5094}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005095#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5096 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5097 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5098 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5099 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5100 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5101 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005103int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005104{
5105 int ret;
5106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005110 ssl->out_msglen = 1;
5111 ssl->out_msg[0] = 1;
5112
Paul Bakker5121ce52009-01-03 21:22:43 +00005113 ssl->state++;
5114
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005115 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005116 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005117 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005118 return( ret );
5119 }
5120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005122
5123 return( 0 );
5124}
5125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005127{
5128 int ret;
5129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005135 return( ret );
5136 }
5137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005138 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005141 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5142 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005143 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005144 }
5145
5146 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005149 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5150 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005151 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005152 }
5153
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005154 /*
5155 * Switch to our negotiated transform and session parameters for inbound
5156 * data.
5157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005159 ssl->transform_in = ssl->transform_negotiate;
5160 ssl->session_in = ssl->session_negotiate;
5161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005163 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005165#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005166 ssl_dtls_replay_reset( ssl );
5167#endif
5168
5169 /* Increment epoch */
5170 if( ++ssl->in_epoch == 0 )
5171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005173 /* This is highly unlikely to happen for legitimate reasons, so
5174 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005175 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005176 }
5177 }
5178 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005179#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005180 memset( ssl->in_ctr, 0, 8 );
5181
5182 /*
5183 * Set the in_msg pointer to the correct location based on IV length
5184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005185 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005186 {
5187 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
5188 ssl->transform_negotiate->fixed_ivlen;
5189 }
5190 else
5191 ssl->in_msg = ssl->in_iv;
5192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005193#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5194 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005196 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005198 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005199 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5200 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005201 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005202 }
5203 }
5204#endif
5205
Paul Bakker5121ce52009-01-03 21:22:43 +00005206 ssl->state++;
5207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005209
5210 return( 0 );
5211}
5212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005213void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5214 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005215{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005216 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5219 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5220 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005221 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005222 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005223#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005224#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5225#if defined(MBEDTLS_SHA512_C)
5226 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005227 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5228 else
5229#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005230#if defined(MBEDTLS_SHA256_C)
5231 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005232 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005233 else
5234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005235#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005238 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005239 }
Paul Bakker380da532012-04-18 16:10:25 +00005240}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005242void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005244#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5245 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005246 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5247 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005248#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005249#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5250#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005251 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005252#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005253#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005254 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005255#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005256#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005257}
5258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005259static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005260 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005262#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5263 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005264 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5265 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005266#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005267#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5268#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005269 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005270#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005271#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005272 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005273#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005275}
5276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5278 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5279static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005280 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005281{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005282 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5283 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005284}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005285#endif
Paul Bakker380da532012-04-18 16:10:25 +00005286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005287#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5288#if defined(MBEDTLS_SHA256_C)
5289static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005290 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005291{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005292 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005293}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005294#endif
Paul Bakker380da532012-04-18 16:10:25 +00005295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296#if defined(MBEDTLS_SHA512_C)
5297static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005298 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005299{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005300 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005301}
Paul Bakker769075d2012-11-24 11:26:46 +01005302#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005306static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005307 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005308{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005309 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005310 mbedtls_md5_context md5;
5311 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005312
Paul Bakker5121ce52009-01-03 21:22:43 +00005313 unsigned char padbuf[48];
5314 unsigned char md5sum[16];
5315 unsigned char sha1sum[20];
5316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005318 if( !session )
5319 session = ssl->session;
5320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005322
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005323 mbedtls_md5_init( &md5 );
5324 mbedtls_sha1_init( &sha1 );
5325
5326 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5327 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005328
5329 /*
5330 * SSLv3:
5331 * hash =
5332 * MD5( master + pad2 +
5333 * MD5( handshake + sender + master + pad1 ) )
5334 * + SHA1( master + pad2 +
5335 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005336 */
5337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005338#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005339 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5340 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005341#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005344 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5345 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005346#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005349 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005350
Paul Bakker1ef83d62012-04-11 12:09:53 +00005351 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005352
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005353 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5354 mbedtls_md5_update_ret( &md5, session->master, 48 );
5355 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5356 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005357
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005358 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5359 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5360 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5361 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005362
Paul Bakker1ef83d62012-04-11 12:09:53 +00005363 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005364
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005365 mbedtls_md5_starts_ret( &md5 );
5366 mbedtls_md5_update_ret( &md5, session->master, 48 );
5367 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5368 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5369 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005370
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005371 mbedtls_sha1_starts_ret( &sha1 );
5372 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5373 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5374 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5375 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005378
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005379 mbedtls_md5_free( &md5 );
5380 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005381
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005382 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5383 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
5384 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005387}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005391static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005393{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005394 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005395 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005396 mbedtls_md5_context md5;
5397 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005398 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005400 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005401 if( !session )
5402 session = ssl->session;
5403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005405
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005406 mbedtls_md5_init( &md5 );
5407 mbedtls_sha1_init( &sha1 );
5408
5409 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5410 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005411
Paul Bakker1ef83d62012-04-11 12:09:53 +00005412 /*
5413 * TLSv1:
5414 * hash = PRF( master, finished_label,
5415 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5416 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005419 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5420 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005421#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005423#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005424 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5425 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005426#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005429 ? "client finished"
5430 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005431
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005432 mbedtls_md5_finish_ret( &md5, padbuf );
5433 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005434
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005435 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005436 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005438 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005439
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005440 mbedtls_md5_free( &md5 );
5441 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005442
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005443 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005446}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005447#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005449#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5450#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005451static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005453{
5454 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005455 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005456 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005457 unsigned char padbuf[32];
5458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005459 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005460 if( !session )
5461 session = ssl->session;
5462
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005463 mbedtls_sha256_init( &sha256 );
5464
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005466
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005467 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005468
5469 /*
5470 * TLSv1.2:
5471 * hash = PRF( master, finished_label,
5472 * Hash( handshake ) )[0.11]
5473 */
5474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475#if !defined(MBEDTLS_SHA256_ALT)
5476 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005477 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005478#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005481 ? "client finished"
5482 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005483
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005484 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005485
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005486 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005487 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005490
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005491 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005492
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005493 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005496}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005500static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005502{
5503 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005504 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005505 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005506 unsigned char padbuf[48];
5507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005509 if( !session )
5510 session = ssl->session;
5511
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005512 mbedtls_sha512_init( &sha512 );
5513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005515
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005516 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005517
5518 /*
5519 * TLSv1.2:
5520 * hash = PRF( master, finished_label,
5521 * Hash( handshake ) )[0.11]
5522 */
5523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005525 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5526 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005527#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005530 ? "client finished"
5531 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005532
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005533 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005534
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005535 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005536 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005539
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005540 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005541
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005542 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546#endif /* MBEDTLS_SHA512_C */
5547#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005550{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005551 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005552
5553 /*
5554 * Free our handshake params
5555 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02005556 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005558 ssl->handshake = NULL;
5559
5560 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005561 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005562 */
5563 if( ssl->transform )
5564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565 mbedtls_ssl_transform_free( ssl->transform );
5566 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005567 }
5568 ssl->transform = ssl->transform_negotiate;
5569 ssl->transform_negotiate = NULL;
5570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005572}
5573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005575{
5576 int resume = ssl->handshake->resume;
5577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005578 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580#if defined(MBEDTLS_SSL_RENEGOTIATION)
5581 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005584 ssl->renego_records_seen = 0;
5585 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005586#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005587
5588 /*
5589 * Free the previous session and switch in the current one
5590 */
Paul Bakker0a597072012-09-25 21:55:46 +00005591 if( ssl->session )
5592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005594 /* RFC 7366 3.1: keep the EtM state */
5595 ssl->session_negotiate->encrypt_then_mac =
5596 ssl->session->encrypt_then_mac;
5597#endif
5598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599 mbedtls_ssl_session_free( ssl->session );
5600 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005601 }
5602 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005603 ssl->session_negotiate = NULL;
5604
Paul Bakker0a597072012-09-25 21:55:46 +00005605 /*
5606 * Add cache entry
5607 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005608 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005609 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005610 resume == 0 )
5611 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005612 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005614 }
Paul Bakker0a597072012-09-25 21:55:46 +00005615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005617 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005618 ssl->handshake->flight != NULL )
5619 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005620 /* Cancel handshake timer */
5621 ssl_set_timer( ssl, 0 );
5622
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005623 /* Keep last flight around in case we need to resend it:
5624 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005626 }
5627 else
5628#endif
5629 ssl_handshake_wrapup_free_hs_transform( ssl );
5630
Paul Bakker48916f92012-09-16 19:57:18 +00005631 ssl->state++;
5632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005634}
5635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005637{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005638 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005641
Paul Bakker92be97b2013-01-02 17:30:03 +01005642 /*
5643 * Set the out_msg pointer to the correct location based on IV length
5644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005646 {
5647 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5648 ssl->transform_negotiate->fixed_ivlen;
5649 }
5650 else
5651 ssl->out_msg = ssl->out_iv;
5652
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005653 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005654
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005655 /*
5656 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5657 * may define some other value. Currently (early 2016), no defined
5658 * ciphersuite does this (and this is unlikely to change as activity has
5659 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005663#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005664 ssl->verify_data_len = hash_len;
5665 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005666#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005667
Paul Bakker5121ce52009-01-03 21:22:43 +00005668 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005669 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5670 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005671
5672 /*
5673 * In case of session resuming, invert the client and server
5674 * ChangeCipherSpec messages order.
5675 */
Paul Bakker0a597072012-09-25 21:55:46 +00005676 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005678#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005679 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005680 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005681#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005683 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005684 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005685#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005686 }
5687 else
5688 ssl->state++;
5689
Paul Bakker48916f92012-09-16 19:57:18 +00005690 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005691 * Switch to our negotiated transform and session parameters for outbound
5692 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005696#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005697 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005698 {
5699 unsigned char i;
5700
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005701 /* Remember current epoch settings for resending */
5702 ssl->handshake->alt_transform_out = ssl->transform_out;
5703 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5704
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005705 /* Set sequence_number to zero */
5706 memset( ssl->out_ctr + 2, 0, 6 );
5707
5708 /* Increment epoch */
5709 for( i = 2; i > 0; i-- )
5710 if( ++ssl->out_ctr[i - 1] != 0 )
5711 break;
5712
5713 /* The loop goes to its end iff the counter is wrapping */
5714 if( i == 0 )
5715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5717 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005718 }
5719 }
5720 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005722 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005723
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005724 ssl->transform_out = ssl->transform_negotiate;
5725 ssl->session_out = ssl->session_negotiate;
5726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5728 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5733 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005734 }
5735 }
5736#endif
5737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005739 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005741#endif
5742
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005743 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005744 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005745 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005746 return( ret );
5747 }
5748
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005749#if defined(MBEDTLS_SSL_PROTO_DTLS)
5750 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5751 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
5752 {
5753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
5754 return( ret );
5755 }
5756#endif
5757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005759
5760 return( 0 );
5761}
5762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005764#define SSL_MAX_HASH_LEN 36
5765#else
5766#define SSL_MAX_HASH_LEN 12
5767#endif
5768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005770{
Paul Bakker23986e52011-04-24 08:57:21 +00005771 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005772 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005773 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005776
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005777 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005781 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005782 return( ret );
5783 }
5784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005788 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5789 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005791 }
5792
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005793 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005794#if defined(MBEDTLS_SSL_PROTO_SSL3)
5795 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005796 hash_len = 36;
5797 else
5798#endif
5799 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005801 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5802 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005805 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5806 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005808 }
5809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005811 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005814 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5815 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005816 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005817 }
5818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005820 ssl->verify_data_len = hash_len;
5821 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005822#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005823
Paul Bakker0a597072012-09-25 21:55:46 +00005824 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005826#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005827 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005828 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005829#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005831 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005833#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005834 }
5835 else
5836 ssl->state++;
5837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005839 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005841#endif
5842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005844
5845 return( 0 );
5846}
5847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005849{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005850 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005852#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5853 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5854 mbedtls_md5_init( &handshake->fin_md5 );
5855 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005856 mbedtls_md5_starts_ret( &handshake->fin_md5 );
5857 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005858#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5860#if defined(MBEDTLS_SHA256_C)
5861 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005862 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005863#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864#if defined(MBEDTLS_SHA512_C)
5865 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005866 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005867#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005869
5870 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005871
5872#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5873 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5874 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5875#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877#if defined(MBEDTLS_DHM_C)
5878 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005879#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880#if defined(MBEDTLS_ECDH_C)
5881 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005882#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02005883#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005884 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02005885#if defined(MBEDTLS_SSL_CLI_C)
5886 handshake->ecjpake_cache = NULL;
5887 handshake->ecjpake_cache_len = 0;
5888#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005889#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005890
5891#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5892 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5893#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005894}
5895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005896static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005897{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005898 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5901 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005903 mbedtls_md_init( &transform->md_ctx_enc );
5904 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005905}
5906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005907void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005908{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005910}
5911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005913{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005914 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005915 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005917 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005918 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005919 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02005920 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005921
5922 /*
5923 * Either the pointers are now NULL or cleared properly and can be freed.
5924 * Now allocate missing structures.
5925 */
5926 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005927 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005928 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005929 }
Paul Bakker48916f92012-09-16 19:57:18 +00005930
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005931 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005932 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005933 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005934 }
Paul Bakker48916f92012-09-16 19:57:18 +00005935
Paul Bakker82788fb2014-10-20 13:59:19 +02005936 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005937 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005938 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005939 }
Paul Bakker48916f92012-09-16 19:57:18 +00005940
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005941 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005942 if( ssl->handshake == NULL ||
5943 ssl->transform_negotiate == NULL ||
5944 ssl->session_negotiate == NULL )
5945 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948 mbedtls_free( ssl->handshake );
5949 mbedtls_free( ssl->transform_negotiate );
5950 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005951
5952 ssl->handshake = NULL;
5953 ssl->transform_negotiate = NULL;
5954 ssl->session_negotiate = NULL;
5955
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005956 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005957 }
5958
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005959 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005960 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005961 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005962 ssl_handshake_params_init( ssl->handshake );
5963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005965 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5966 {
5967 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005968
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005969 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5970 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5971 else
5972 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005973
5974 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005975 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005976#endif
5977
Paul Bakker48916f92012-09-16 19:57:18 +00005978 return( 0 );
5979}
5980
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005981#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005982/* Dummy cookie callbacks for defaults */
5983static int ssl_cookie_write_dummy( void *ctx,
5984 unsigned char **p, unsigned char *end,
5985 const unsigned char *cli_id, size_t cli_id_len )
5986{
5987 ((void) ctx);
5988 ((void) p);
5989 ((void) end);
5990 ((void) cli_id);
5991 ((void) cli_id_len);
5992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005994}
5995
5996static int ssl_cookie_check_dummy( void *ctx,
5997 const unsigned char *cookie, size_t cookie_len,
5998 const unsigned char *cli_id, size_t cli_id_len )
5999{
6000 ((void) ctx);
6001 ((void) cookie);
6002 ((void) cookie_len);
6003 ((void) cli_id);
6004 ((void) cli_id_len);
6005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006006 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006007}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006008#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006009
Paul Bakker5121ce52009-01-03 21:22:43 +00006010/*
6011 * Initialize an SSL context
6012 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006013void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6014{
6015 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6016}
6017
6018/*
6019 * Setup an SSL context
6020 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006021int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006022 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006023{
Paul Bakker48916f92012-09-16 19:57:18 +00006024 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006025
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006026 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006027
6028 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006029 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006030 */
Angus Grattond8213d02016-05-25 20:56:48 +10006031 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6032 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006033 {
Angus Grattond8213d02016-05-25 20:56:48 +10006034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6035 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6036 }
6037
6038 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6039 if( ssl->out_buf == NULL )
6040 {
6041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006042 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006043 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006044 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006045 }
6046
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006047#if defined(MBEDTLS_SSL_PROTO_DTLS)
6048 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6049 {
6050 ssl->out_hdr = ssl->out_buf;
6051 ssl->out_ctr = ssl->out_buf + 3;
6052 ssl->out_len = ssl->out_buf + 11;
6053 ssl->out_iv = ssl->out_buf + 13;
6054 ssl->out_msg = ssl->out_buf + 13;
6055
6056 ssl->in_hdr = ssl->in_buf;
6057 ssl->in_ctr = ssl->in_buf + 3;
6058 ssl->in_len = ssl->in_buf + 11;
6059 ssl->in_iv = ssl->in_buf + 13;
6060 ssl->in_msg = ssl->in_buf + 13;
6061 }
6062 else
6063#endif
6064 {
6065 ssl->out_ctr = ssl->out_buf;
6066 ssl->out_hdr = ssl->out_buf + 8;
6067 ssl->out_len = ssl->out_buf + 11;
6068 ssl->out_iv = ssl->out_buf + 13;
6069 ssl->out_msg = ssl->out_buf + 13;
6070
6071 ssl->in_ctr = ssl->in_buf;
6072 ssl->in_hdr = ssl->in_buf + 8;
6073 ssl->in_len = ssl->in_buf + 11;
6074 ssl->in_iv = ssl->in_buf + 13;
6075 ssl->in_msg = ssl->in_buf + 13;
6076 }
6077
Paul Bakker48916f92012-09-16 19:57:18 +00006078 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6079 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006080
6081 return( 0 );
6082}
6083
6084/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006085 * Reset an initialized and used SSL context for re-use while retaining
6086 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006087 *
6088 * If partial is non-zero, keep data in the input buffer and client ID.
6089 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006090 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006091static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006092{
Paul Bakker48916f92012-09-16 19:57:18 +00006093 int ret;
6094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006095 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006096
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006097 /* Cancel any possibly running timer */
6098 ssl_set_timer( ssl, 0 );
6099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100#if defined(MBEDTLS_SSL_RENEGOTIATION)
6101 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006102 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006103
6104 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6106 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006107#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006109
Paul Bakker7eb013f2011-10-06 12:37:39 +00006110 ssl->in_offt = NULL;
6111
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006112 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006113 ssl->in_msgtype = 0;
6114 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006115 if( partial == 0 )
6116 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006117#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006118 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006119 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006122 ssl_dtls_replay_reset( ssl );
6123#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006124
6125 ssl->in_hslen = 0;
6126 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006127
6128 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006129
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006130 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006131 ssl->out_msgtype = 0;
6132 ssl->out_msglen = 0;
6133 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6135 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006136 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006137#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006138
Paul Bakker48916f92012-09-16 19:57:18 +00006139 ssl->transform_in = NULL;
6140 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006141
Hanno Becker78640902018-08-13 16:35:15 +01006142 ssl->session_in = NULL;
6143 ssl->session_out = NULL;
6144
Angus Grattond8213d02016-05-25 20:56:48 +10006145 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006146 if( partial == 0 )
Angus Grattond8213d02016-05-25 20:56:48 +10006147 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00006148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6150 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6153 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6156 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006157 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006158 }
6159#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006160
Paul Bakker48916f92012-09-16 19:57:18 +00006161 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163 mbedtls_ssl_transform_free( ssl->transform );
6164 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006165 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006166 }
Paul Bakker48916f92012-09-16 19:57:18 +00006167
Paul Bakkerc0463502013-02-14 11:19:38 +01006168 if( ssl->session )
6169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170 mbedtls_ssl_session_free( ssl->session );
6171 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006172 ssl->session = NULL;
6173 }
6174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006176 ssl->alpn_chosen = NULL;
6177#endif
6178
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006179#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006180 if( partial == 0 )
6181 {
6182 mbedtls_free( ssl->cli_id );
6183 ssl->cli_id = NULL;
6184 ssl->cli_id_len = 0;
6185 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006186#endif
6187
Paul Bakker48916f92012-09-16 19:57:18 +00006188 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6189 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006190
6191 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006192}
6193
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006194/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006195 * Reset an initialized and used SSL context for re-use while retaining
6196 * all application-set variables, function pointers and data.
6197 */
6198int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6199{
6200 return( ssl_session_reset_int( ssl, 0 ) );
6201}
6202
6203/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006204 * SSL set accessors
6205 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006206void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006207{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006208 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006209}
6210
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006211void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006212{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006213 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006214}
6215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006216#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006217void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006218{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006219 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006220}
6221#endif
6222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006224void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006225{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006226 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006227}
6228#endif
6229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006231void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006232{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006233 conf->hs_timeout_min = min;
6234 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006235}
6236#endif
6237
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006238void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006239{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006240 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006241}
6242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006244void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006245 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006246 void *p_vrfy )
6247{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006248 conf->f_vrfy = f_vrfy;
6249 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006252
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006253void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006254 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006255 void *p_rng )
6256{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006257 conf->f_rng = f_rng;
6258 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006259}
6260
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006261void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006262 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006263 void *p_dbg )
6264{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006265 conf->f_dbg = f_dbg;
6266 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006267}
6268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006270 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006271 mbedtls_ssl_send_t *f_send,
6272 mbedtls_ssl_recv_t *f_recv,
6273 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006274{
6275 ssl->p_bio = p_bio;
6276 ssl->f_send = f_send;
6277 ssl->f_recv = f_recv;
6278 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006279}
6280
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02006281#if defined(MBEDTLS_SSL_PROTO_DTLS)
6282void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
6283{
6284 ssl->mtu = mtu;
6285}
6286#endif
6287
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006288void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006289{
6290 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006291}
6292
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006293void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6294 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006295 mbedtls_ssl_set_timer_t *f_set_timer,
6296 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006297{
6298 ssl->p_timer = p_timer;
6299 ssl->f_set_timer = f_set_timer;
6300 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006301
6302 /* Make sure we start with no timer running */
6303 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006304}
6305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006307void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006308 void *p_cache,
6309 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6310 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006311{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006312 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006313 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006314 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006315}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318#if defined(MBEDTLS_SSL_CLI_C)
6319int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006320{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006321 int ret;
6322
6323 if( ssl == NULL ||
6324 session == NULL ||
6325 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006326 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006328 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006329 }
6330
6331 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6332 return( ret );
6333
Paul Bakker0a597072012-09-25 21:55:46 +00006334 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006335
6336 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006337}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006338#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006339
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006340void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006341 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006342{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006343 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6344 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6345 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6346 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006347}
6348
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006349void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006350 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006351 int major, int minor )
6352{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006353 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006354 return;
6355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006357 return;
6358
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006359 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006360}
6361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006363void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006364 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006365{
6366 conf->cert_profile = profile;
6367}
6368
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006369/* Append a new keycert entry to a (possibly empty) list */
6370static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6371 mbedtls_x509_crt *cert,
6372 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006373{
niisato8ee24222018-06-25 19:05:48 +09006374 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006375
niisato8ee24222018-06-25 19:05:48 +09006376 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6377 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006378 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006379
niisato8ee24222018-06-25 19:05:48 +09006380 new_cert->cert = cert;
6381 new_cert->key = key;
6382 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006383
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006384 /* Update head is the list was null, else add to the end */
6385 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006386 {
niisato8ee24222018-06-25 19:05:48 +09006387 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006388 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006389 else
6390 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006391 mbedtls_ssl_key_cert *cur = *head;
6392 while( cur->next != NULL )
6393 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09006394 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006395 }
6396
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006397 return( 0 );
6398}
6399
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006400int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006401 mbedtls_x509_crt *own_cert,
6402 mbedtls_pk_context *pk_key )
6403{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006404 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006405}
6406
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006407void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006408 mbedtls_x509_crt *ca_chain,
6409 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006410{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006411 conf->ca_chain = ca_chain;
6412 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006413}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006415
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006416#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6417int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6418 mbedtls_x509_crt *own_cert,
6419 mbedtls_pk_context *pk_key )
6420{
6421 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6422 own_cert, pk_key ) );
6423}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006424
6425void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6426 mbedtls_x509_crt *ca_chain,
6427 mbedtls_x509_crl *ca_crl )
6428{
6429 ssl->handshake->sni_ca_chain = ca_chain;
6430 ssl->handshake->sni_ca_crl = ca_crl;
6431}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006432
6433void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6434 int authmode )
6435{
6436 ssl->handshake->sni_authmode = authmode;
6437}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006438#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6439
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006440#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006441/*
6442 * Set EC J-PAKE password for current handshake
6443 */
6444int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6445 const unsigned char *pw,
6446 size_t pw_len )
6447{
6448 mbedtls_ecjpake_role role;
6449
Janos Follath8eb64132016-06-03 15:40:57 +01006450 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6452
6453 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6454 role = MBEDTLS_ECJPAKE_SERVER;
6455 else
6456 role = MBEDTLS_ECJPAKE_CLIENT;
6457
6458 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6459 role,
6460 MBEDTLS_MD_SHA256,
6461 MBEDTLS_ECP_DP_SECP256R1,
6462 pw, pw_len ) );
6463}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006464#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006467int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006468 const unsigned char *psk, size_t psk_len,
6469 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006470{
Paul Bakker6db455e2013-09-18 17:29:31 +02006471 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006476
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006477 /* Identity len will be encoded on two bytes */
6478 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10006479 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006480 {
6481 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6482 }
6483
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006484 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006485 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006486 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006487
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006488 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006489 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006490 conf->psk_len = 0;
6491 }
6492 if( conf->psk_identity != NULL )
6493 {
6494 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006495 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006496 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006497 }
6498
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006499 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6500 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006501 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006502 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006503 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006504 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006505 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006506 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006507 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006508
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006509 conf->psk_len = psk_len;
6510 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006511
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006512 memcpy( conf->psk, psk, conf->psk_len );
6513 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006514
6515 return( 0 );
6516}
6517
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006518int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6519 const unsigned char *psk, size_t psk_len )
6520{
6521 if( psk == NULL || ssl->handshake == NULL )
6522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6523
6524 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6526
6527 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006528 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006529 mbedtls_platform_zeroize( ssl->handshake->psk,
6530 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006531 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006532 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006533 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006534
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006535 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006536 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006537
6538 ssl->handshake->psk_len = psk_len;
6539 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6540
6541 return( 0 );
6542}
6543
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006544void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006546 size_t),
6547 void *p_psk )
6548{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006549 conf->f_psk = f_psk;
6550 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006551}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006553
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006554#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006555
6556#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006557int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006558{
6559 int ret;
6560
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006561 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6562 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6563 {
6564 mbedtls_mpi_free( &conf->dhm_P );
6565 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006566 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006567 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006568
6569 return( 0 );
6570}
Hanno Becker470a8c42017-10-04 15:28:46 +01006571#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006572
Hanno Beckera90658f2017-10-04 15:29:08 +01006573int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6574 const unsigned char *dhm_P, size_t P_len,
6575 const unsigned char *dhm_G, size_t G_len )
6576{
6577 int ret;
6578
6579 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6580 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6581 {
6582 mbedtls_mpi_free( &conf->dhm_P );
6583 mbedtls_mpi_free( &conf->dhm_G );
6584 return( ret );
6585 }
6586
6587 return( 0 );
6588}
Paul Bakker5121ce52009-01-03 21:22:43 +00006589
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006590int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006591{
6592 int ret;
6593
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006594 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6595 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6596 {
6597 mbedtls_mpi_free( &conf->dhm_P );
6598 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006599 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006600 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006601
6602 return( 0 );
6603}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006604#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006605
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006606#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6607/*
6608 * Set the minimum length for Diffie-Hellman parameters
6609 */
6610void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6611 unsigned int bitlen )
6612{
6613 conf->dhm_min_bitlen = bitlen;
6614}
6615#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6616
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006617#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006618/*
6619 * Set allowed/preferred hashes for handshake signatures
6620 */
6621void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6622 const int *hashes )
6623{
6624 conf->sig_hashes = hashes;
6625}
Hanno Becker947194e2017-04-07 13:25:49 +01006626#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006627
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006628#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006629/*
6630 * Set the allowed elliptic curves
6631 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006632void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006633 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006634{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006635 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006636}
Hanno Becker947194e2017-04-07 13:25:49 +01006637#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006638
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006639#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006640int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006641{
Hanno Becker947194e2017-04-07 13:25:49 +01006642 /* Initialize to suppress unnecessary compiler warning */
6643 size_t hostname_len = 0;
6644
6645 /* Check if new hostname is valid before
6646 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006647 if( hostname != NULL )
6648 {
6649 hostname_len = strlen( hostname );
6650
6651 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6653 }
6654
6655 /* Now it's clear that we will overwrite the old hostname,
6656 * so we can free it safely */
6657
6658 if( ssl->hostname != NULL )
6659 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006660 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01006661 mbedtls_free( ssl->hostname );
6662 }
6663
6664 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006665
Paul Bakker5121ce52009-01-03 21:22:43 +00006666 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006667 {
6668 ssl->hostname = NULL;
6669 }
6670 else
6671 {
6672 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006673 if( ssl->hostname == NULL )
6674 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006675
Hanno Becker947194e2017-04-07 13:25:49 +01006676 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006677
Hanno Becker947194e2017-04-07 13:25:49 +01006678 ssl->hostname[hostname_len] = '\0';
6679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006680
6681 return( 0 );
6682}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006683#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006684
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006685#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006686void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006688 const unsigned char *, size_t),
6689 void *p_sni )
6690{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006691 conf->f_sni = f_sni;
6692 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006693}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006697int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006698{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006699 size_t cur_len, tot_len;
6700 const char **p;
6701
6702 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006703 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6704 * MUST NOT be truncated."
6705 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006706 */
6707 tot_len = 0;
6708 for( p = protos; *p != NULL; p++ )
6709 {
6710 cur_len = strlen( *p );
6711 tot_len += cur_len;
6712
6713 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006715 }
6716
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006717 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006718
6719 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006720}
6721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006723{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006724 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006727
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006728void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006729{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006730 conf->max_major_ver = major;
6731 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006732}
6733
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006734void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006735{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006736 conf->min_major_ver = major;
6737 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006738}
6739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006741void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006742{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006743 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006744}
6745#endif
6746
Janos Follath088ce432017-04-10 12:42:31 +01006747#if defined(MBEDTLS_SSL_SRV_C)
6748void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6749 char cert_req_ca_list )
6750{
6751 conf->cert_req_ca_list = cert_req_ca_list;
6752}
6753#endif
6754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006756void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006757{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006758 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006759}
6760#endif
6761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006763void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006764{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006765 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006766}
6767#endif
6768
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006769#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006770void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006771{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006772 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006773}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006774#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006777int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006778{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10006780 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006783 }
6784
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006785 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006786
6787 return( 0 );
6788}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006792void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006793{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006794 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006795}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006798#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006799void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006800{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006801 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006802}
6803#endif
6804
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006805void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006806{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006807 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006808}
6809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006811void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006812{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006813 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006814}
6815
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006816void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006817{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006818 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006819}
6820
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006821void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006822 const unsigned char period[8] )
6823{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006824 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006825}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006829#if defined(MBEDTLS_SSL_CLI_C)
6830void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006831{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006832 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006833}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006834#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006835
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006836#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006837void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6838 mbedtls_ssl_ticket_write_t *f_ticket_write,
6839 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6840 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006841{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006842 conf->f_ticket_write = f_ticket_write;
6843 conf->f_ticket_parse = f_ticket_parse;
6844 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006845}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006846#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006848
Robert Cragie4feb7ae2015-10-02 13:33:37 +01006849#if defined(MBEDTLS_SSL_EXPORT_KEYS)
6850void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
6851 mbedtls_ssl_export_keys_t *f_export_keys,
6852 void *p_export_keys )
6853{
6854 conf->f_export_keys = f_export_keys;
6855 conf->p_export_keys = p_export_keys;
6856}
6857#endif
6858
Gilles Peskineb74a1c72018-04-24 13:09:22 +02006859#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006860void mbedtls_ssl_conf_async_private_cb(
6861 mbedtls_ssl_config *conf,
6862 mbedtls_ssl_async_sign_t *f_async_sign,
6863 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
6864 mbedtls_ssl_async_resume_t *f_async_resume,
6865 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006866 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006867{
6868 conf->f_async_sign_start = f_async_sign;
6869 conf->f_async_decrypt_start = f_async_decrypt;
6870 conf->f_async_resume = f_async_resume;
6871 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006872 conf->p_async_config_data = async_config_data;
6873}
6874
Gilles Peskine8f97af72018-04-26 11:46:10 +02006875void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
6876{
6877 return( conf->p_async_config_data );
6878}
6879
Gilles Peskine1febfef2018-04-30 11:54:39 +02006880void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006881{
6882 if( ssl->handshake == NULL )
6883 return( NULL );
6884 else
6885 return( ssl->handshake->user_async_ctx );
6886}
6887
Gilles Peskine1febfef2018-04-30 11:54:39 +02006888void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006889 void *ctx )
6890{
6891 if( ssl->handshake != NULL )
6892 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006893}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02006894#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006895
Paul Bakker5121ce52009-01-03 21:22:43 +00006896/*
6897 * SSL get accessors
6898 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006900{
6901 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6902}
6903
Hanno Becker8b170a02017-10-10 11:51:19 +01006904int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
6905{
6906 /*
6907 * Case A: We're currently holding back
6908 * a message for further processing.
6909 */
6910
6911 if( ssl->keep_current_message == 1 )
6912 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006913 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006914 return( 1 );
6915 }
6916
6917 /*
6918 * Case B: Further records are pending in the current datagram.
6919 */
6920
6921#if defined(MBEDTLS_SSL_PROTO_DTLS)
6922 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6923 ssl->in_left > ssl->next_record_offset )
6924 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006925 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006926 return( 1 );
6927 }
6928#endif /* MBEDTLS_SSL_PROTO_DTLS */
6929
6930 /*
6931 * Case C: A handshake message is being processed.
6932 */
6933
Hanno Becker8b170a02017-10-10 11:51:19 +01006934 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
6935 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006936 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006937 return( 1 );
6938 }
6939
6940 /*
6941 * Case D: An application data message is being processed
6942 */
6943 if( ssl->in_offt != NULL )
6944 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006945 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006946 return( 1 );
6947 }
6948
6949 /*
6950 * In all other cases, the rest of the message can be dropped.
6951 * As in ssl_read_record_layer, this needs to be adapted if
6952 * we implement support for multiple alerts in single records.
6953 */
6954
6955 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
6956 return( 0 );
6957}
6958
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006959uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006960{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006961 if( ssl->session != NULL )
6962 return( ssl->session->verify_result );
6963
6964 if( ssl->session_negotiate != NULL )
6965 return( ssl->session_negotiate->verify_result );
6966
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006967 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006968}
6969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006971{
Paul Bakker926c8e42013-03-06 10:23:34 +01006972 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006973 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006976}
6977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006979{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006981 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006982 {
6983 switch( ssl->minor_ver )
6984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006986 return( "DTLSv1.0" );
6987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006989 return( "DTLSv1.2" );
6990
6991 default:
6992 return( "unknown (DTLS)" );
6993 }
6994 }
6995#endif
6996
Paul Bakker43ca69c2011-01-15 17:35:19 +00006997 switch( ssl->minor_ver )
6998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007000 return( "SSLv3.0" );
7001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007003 return( "TLSv1.0" );
7004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007006 return( "TLSv1.1" );
7007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007009 return( "TLSv1.2" );
7010
Paul Bakker43ca69c2011-01-15 17:35:19 +00007011 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007012 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007013 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007014}
7015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007017{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007018 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007020 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007021
Hanno Becker78640902018-08-13 16:35:15 +01007022 if( transform == NULL )
7023 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025#if defined(MBEDTLS_ZLIB_SUPPORT)
7026 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7027 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007028 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007029#endif
7030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007033 case MBEDTLS_MODE_GCM:
7034 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007035 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007037 transform_expansion = transform->minlen;
7038 break;
7039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007040 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007041
7042 block_size = mbedtls_cipher_get_block_size(
7043 &transform->cipher_ctx_enc );
7044
7045#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7046 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7047 {
7048 /* Expansion due to addition of
7049 * - MAC
7050 * - CBC padding (theoretically up to 256 bytes, but
7051 * we never use more than block_size)
7052 * - explicit IV
7053 */
7054 transform_expansion = transform->maclen + 2 * block_size;
7055 }
7056 else
7057#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7058 {
7059 /* No explicit IV prior to TLS 1.1. */
7060 transform_expansion = transform->maclen + block_size;
7061 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007062 break;
7063
7064 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007067 }
7068
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007069 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007070}
7071
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007072#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7073size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7074{
7075 size_t max_len;
7076
7077 /*
7078 * Assume mfl_code is correct since it was checked when set
7079 */
Angus Grattond8213d02016-05-25 20:56:48 +10007080 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007081
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007082 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007083 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007084 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007085 {
Angus Grattond8213d02016-05-25 20:56:48 +10007086 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007087 }
7088
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007089 /* During a handshake, use the value being negotiated */
7090 if( ssl->session_negotiate != NULL &&
7091 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7092 {
7093 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7094 }
7095
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007096 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007097}
7098#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7099
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007100int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7101{
7102 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7103
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02007104#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7105 !defined(MBEDTLS_SSL_PROTO_DTLS)
7106 (void) ssl;
7107#endif
7108
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007109#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7110 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7111
7112 if( max_len > mfl )
7113 max_len = mfl;
7114#endif
7115
7116#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007117 if( ssl->mtu != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007118 {
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007119 const size_t mtu = ssl->mtu;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007120 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7121 const size_t overhead = (size_t) ret;
7122
7123 if( ret < 0 )
7124 return( ret );
7125
7126 if( mtu <= overhead )
7127 {
7128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7129 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7130 }
7131
7132 if( max_len > mtu - overhead )
7133 max_len = mtu - overhead;
7134 }
7135#endif
7136
7137 return( (int) max_len );
7138}
7139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140#if defined(MBEDTLS_X509_CRT_PARSE_C)
7141const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007142{
7143 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007144 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007145
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007146 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007147}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150#if defined(MBEDTLS_SSL_CLI_C)
7151int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007152{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007153 if( ssl == NULL ||
7154 dst == NULL ||
7155 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007156 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007159 }
7160
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007161 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007162}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007164
Paul Bakker5121ce52009-01-03 21:22:43 +00007165/*
Paul Bakker1961b702013-01-25 14:49:24 +01007166 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007169{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007171
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007172 if( ssl == NULL || ssl->conf == NULL )
7173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007176 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007177 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007178#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007179#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007180 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007181 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007182#endif
7183
Paul Bakker1961b702013-01-25 14:49:24 +01007184 return( ret );
7185}
7186
7187/*
7188 * Perform the SSL handshake
7189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007190int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007191{
7192 int ret = 0;
7193
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007194 if( ssl == NULL || ssl->conf == NULL )
7195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007202
7203 if( ret != 0 )
7204 break;
7205 }
7206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007208
7209 return( ret );
7210}
7211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212#if defined(MBEDTLS_SSL_RENEGOTIATION)
7213#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007214/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007215 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007218{
7219 int ret;
7220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007222
7223 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7225 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007226
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007227 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007228 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007229 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007230 return( ret );
7231 }
7232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007234
7235 return( 0 );
7236}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007237#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007238
7239/*
7240 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241 * - any side: calling mbedtls_ssl_renegotiate(),
7242 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7243 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007244 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007245 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007249{
7250 int ret;
7251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007253
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007254 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7255 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007256
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007257 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7258 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007260 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007262 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007263 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007264 ssl->handshake->out_msg_seq = 1;
7265 else
7266 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007267 }
7268#endif
7269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7271 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007276 return( ret );
7277 }
7278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007280
7281 return( 0 );
7282}
7283
7284/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007285 * Renegotiate current connection on client,
7286 * or request renegotiation on server
7287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007289{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007291
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007292 if( ssl == NULL || ssl->conf == NULL )
7293 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007296 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007297 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007303
7304 /* Did we already try/start sending HelloRequest? */
7305 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007307
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007308 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007309 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007313 /*
7314 * On client, either start the renegotiation process or,
7315 * if already in progress, continue the handshake
7316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007321
7322 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007325 return( ret );
7326 }
7327 }
7328 else
7329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007333 return( ret );
7334 }
7335 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007337
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007338 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007339}
7340
7341/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007342 * Check record counters and renegotiate if they're above the limit.
7343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007345{
Andres AG2196c7f2016-12-15 17:01:16 +00007346 size_t ep_len = ssl_ep_len( ssl );
7347 int in_ctr_cmp;
7348 int out_ctr_cmp;
7349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7351 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007352 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007353 {
7354 return( 0 );
7355 }
7356
Andres AG2196c7f2016-12-15 17:01:16 +00007357 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7358 ssl->conf->renego_period + ep_len, 8 - ep_len );
7359 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
7360 ssl->conf->renego_period + ep_len, 8 - ep_len );
7361
7362 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007363 {
7364 return( 0 );
7365 }
7366
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007369}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007371
7372/*
7373 * Receive application data decrypted from the SSL layer
7374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007376{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007377 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007378 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007379
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007380 if( ssl == NULL || ssl->conf == NULL )
7381 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007386 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007389 return( ret );
7390
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007391 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007393 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007394 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007395 return( ret );
7396 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007397 }
7398#endif
7399
Hanno Becker4a810fb2017-05-24 16:27:30 +01007400 /*
7401 * Check if renegotiation is necessary and/or handshake is
7402 * in process. If yes, perform/continue, and fall through
7403 * if an unexpected packet is received while the client
7404 * is waiting for the ServerHello.
7405 *
7406 * (There is no equivalent to the last condition on
7407 * the server-side as it is not treated as within
7408 * a handshake while waiting for the ClientHello
7409 * after a renegotiation request.)
7410 */
7411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007412#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007413 ret = ssl_check_ctr_renegotiate( ssl );
7414 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7415 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007418 return( ret );
7419 }
7420#endif
7421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007424 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007425 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7426 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007429 return( ret );
7430 }
7431 }
7432
Hanno Beckere41158b2017-10-23 13:30:32 +01007433 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01007434 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007435 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007436 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007437 if( ssl->f_get_timer != NULL &&
7438 ssl->f_get_timer( ssl->p_timer ) == -1 )
7439 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007440 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007441 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007442
Hanno Becker4a810fb2017-05-24 16:27:30 +01007443 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007444 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007445 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7446 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007447
Hanno Becker4a810fb2017-05-24 16:27:30 +01007448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7449 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007450 }
7451
7452 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007453 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007454 {
7455 /*
7456 * OpenSSL sends empty messages to randomize the IV
7457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007460 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007461 return( 0 );
7462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007464 return( ret );
7465 }
7466 }
7467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007471
Hanno Becker4a810fb2017-05-24 16:27:30 +01007472 /*
7473 * - For client-side, expect SERVER_HELLO_REQUEST.
7474 * - For server-side, expect CLIENT_HELLO.
7475 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7476 */
7477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007479 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007481 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007484
7485 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007486#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007487 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007488 {
7489 continue;
7490 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007491#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007492 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007493 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007494#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007495
Hanno Becker4a810fb2017-05-24 16:27:30 +01007496#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007497 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007501
7502 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007503#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007504 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007505 {
7506 continue;
7507 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007508#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007510 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007511#endif /* MBEDTLS_SSL_SRV_C */
7512
Hanno Becker21df7f92017-10-17 11:03:26 +01007513#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007514 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007515 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7516 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7517 ssl->conf->allow_legacy_renegotiation ==
7518 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7519 {
7520 /*
7521 * Accept renegotiation request
7522 */
Paul Bakker48916f92012-09-16 19:57:18 +00007523
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007524 /* DTLS clients need to know renego is server-initiated */
7525#if defined(MBEDTLS_SSL_PROTO_DTLS)
7526 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7527 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7528 {
7529 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7530 }
7531#endif
7532 ret = ssl_start_renegotiation( ssl );
7533 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7534 ret != 0 )
7535 {
7536 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7537 return( ret );
7538 }
7539 }
7540 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007541#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007542 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007543 /*
7544 * Refuse renegotiation
7545 */
7546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549#if defined(MBEDTLS_SSL_PROTO_SSL3)
7550 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007551 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007552 /* SSLv3 does not have a "no_renegotiation" warning, so
7553 we send a fatal alert and abort the connection. */
7554 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7555 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7556 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007557 }
7558 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7560#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7561 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7562 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7565 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7566 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007567 {
7568 return( ret );
7569 }
Paul Bakker48916f92012-09-16 19:57:18 +00007570 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007571 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7573 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7576 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007577 }
Paul Bakker48916f92012-09-16 19:57:18 +00007578 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007579
Hanno Becker90333da2017-10-10 11:27:13 +01007580 /* At this point, we don't know whether the renegotiation has been
7581 * completed or not. The cases to consider are the following:
7582 * 1) The renegotiation is complete. In this case, no new record
7583 * has been read yet.
7584 * 2) The renegotiation is incomplete because the client received
7585 * an application data record while awaiting the ServerHello.
7586 * 3) The renegotiation is incomplete because the client received
7587 * a non-handshake, non-application data message while awaiting
7588 * the ServerHello.
7589 * In each of these case, looping will be the proper action:
7590 * - For 1), the next iteration will read a new record and check
7591 * if it's application data.
7592 * - For 2), the loop condition isn't satisfied as application data
7593 * is present, hence continue is the same as break
7594 * - For 3), the loop condition is satisfied and read_record
7595 * will re-deliver the message that was held back by the client
7596 * when expecting the ServerHello.
7597 */
7598 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00007599 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007600#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007602 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007603 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007604 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007605 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007608 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007610 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007611 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007612 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7616 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007619 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007620 }
7621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7625 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007626 }
7627
7628 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007629
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007630 /* We're going to return something now, cancel timer,
7631 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007633 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007634
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007635#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007636 /* If we requested renego but received AppData, resend HelloRequest.
7637 * Do it now, after setting in_offt, to avoid taking this branch
7638 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007640 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007642 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007643 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007646 return( ret );
7647 }
7648 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007650#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007651 }
7652
7653 n = ( len < ssl->in_msglen )
7654 ? len : ssl->in_msglen;
7655
7656 memcpy( buf, ssl->in_offt, n );
7657 ssl->in_msglen -= n;
7658
7659 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007660 {
7661 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007662 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007663 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007664 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007665 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007666 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007667 /* more data available */
7668 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007669 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007672
Paul Bakker23986e52011-04-24 08:57:21 +00007673 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007674}
7675
7676/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007677 * Send application data to be encrypted by the SSL layer, taking care of max
7678 * fragment length and buffer size.
7679 *
7680 * According to RFC 5246 Section 6.2.1:
7681 *
7682 * Zero-length fragments of Application data MAY be sent as they are
7683 * potentially useful as a traffic analysis countermeasure.
7684 *
7685 * Therefore, it is possible that the input message length is 0 and the
7686 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007687 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007688static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007689 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007690{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007691 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
7692 const size_t max_len = (size_t) ret;
7693
7694 if( ret < 0 )
7695 {
7696 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
7697 return( ret );
7698 }
7699
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007700 if( len > max_len )
7701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007703 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007706 "maximum fragment length: %d > %d",
7707 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007709 }
7710 else
7711#endif
7712 len = max_len;
7713 }
Paul Bakker887bd502011-06-08 13:10:54 +00007714
Paul Bakker5121ce52009-01-03 21:22:43 +00007715 if( ssl->out_left != 0 )
7716 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007717 /*
7718 * The user has previously tried to send the data and
7719 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7720 * written. In this case, we expect the high-level write function
7721 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007726 return( ret );
7727 }
7728 }
Paul Bakker887bd502011-06-08 13:10:54 +00007729 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007730 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007731 /*
7732 * The user is trying to send a message the first time, so we need to
7733 * copy the data into the internal buffers and setup the data structure
7734 * to keep track of partial writes
7735 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007736 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007738 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007743 return( ret );
7744 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007745 }
7746
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007747 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007748}
7749
7750/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007751 * Write application data, doing 1/n-1 splitting if necessary.
7752 *
7753 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007754 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01007755 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007756 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007758static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007759 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007760{
7761 int ret;
7762
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007763 if( ssl->conf->cbc_record_splitting ==
7764 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007765 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7767 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7768 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007769 {
7770 return( ssl_write_real( ssl, buf, len ) );
7771 }
7772
7773 if( ssl->split_done == 0 )
7774 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007775 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007776 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007777 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007778 }
7779
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007780 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7781 return( ret );
7782 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007783
7784 return( ret + 1 );
7785}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007787
7788/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007789 * Write application data (public-facing wrapper)
7790 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007791int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007792{
7793 int ret;
7794
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007796
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007797 if( ssl == NULL || ssl->conf == NULL )
7798 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7799
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007800#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007801 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7802 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007803 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007804 return( ret );
7805 }
7806#endif
7807
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007808 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007809 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007810 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007811 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007812 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007813 return( ret );
7814 }
7815 }
7816
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007817#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007818 ret = ssl_write_split( ssl, buf, len );
7819#else
7820 ret = ssl_write_real( ssl, buf, len );
7821#endif
7822
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007824
7825 return( ret );
7826}
7827
7828/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007829 * Notify the peer that the connection is being closed
7830 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007832{
7833 int ret;
7834
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007835 if( ssl == NULL || ssl->conf == NULL )
7836 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007839
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007840 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7846 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7847 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007850 return( ret );
7851 }
7852 }
7853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007855
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007856 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007857}
7858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00007860{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007861 if( transform == NULL )
7862 return;
7863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00007865 deflateEnd( &transform->ctx_deflate );
7866 inflateEnd( &transform->ctx_inflate );
7867#endif
7868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869 mbedtls_cipher_free( &transform->cipher_ctx_enc );
7870 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02007871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872 mbedtls_md_free( &transform->md_ctx_enc );
7873 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02007874
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007875 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007876}
7877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878#if defined(MBEDTLS_X509_CRT_PARSE_C)
7879static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007880{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007882
7883 while( cur != NULL )
7884 {
7885 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007887 cur = next;
7888 }
7889}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007891
Gilles Peskine9b562d52018-04-25 20:32:43 +02007892void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007893{
Gilles Peskine9b562d52018-04-25 20:32:43 +02007894 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
7895
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007896 if( handshake == NULL )
7897 return;
7898
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007899#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
7900 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
7901 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02007902 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007903 handshake->async_in_progress = 0;
7904 }
7905#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
7906
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007907#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7908 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7909 mbedtls_md5_free( &handshake->fin_md5 );
7910 mbedtls_sha1_free( &handshake->fin_sha1 );
7911#endif
7912#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7913#if defined(MBEDTLS_SHA256_C)
7914 mbedtls_sha256_free( &handshake->fin_sha256 );
7915#endif
7916#if defined(MBEDTLS_SHA512_C)
7917 mbedtls_sha512_free( &handshake->fin_sha512 );
7918#endif
7919#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921#if defined(MBEDTLS_DHM_C)
7922 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007923#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924#if defined(MBEDTLS_ECDH_C)
7925 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007926#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007927#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007928 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007929#if defined(MBEDTLS_SSL_CLI_C)
7930 mbedtls_free( handshake->ecjpake_cache );
7931 handshake->ecjpake_cache = NULL;
7932 handshake->ecjpake_cache_len = 0;
7933#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007934#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02007935
Janos Follath4ae5c292016-02-10 11:27:43 +00007936#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
7937 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02007938 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007940#endif
7941
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007942#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7943 if( handshake->psk != NULL )
7944 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007945 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007946 mbedtls_free( handshake->psk );
7947 }
7948#endif
7949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7951 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007952 /*
7953 * Free only the linked list wrapper, not the keys themselves
7954 * since the belong to the SNI callback
7955 */
7956 if( handshake->sni_key_cert != NULL )
7957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007959
7960 while( cur != NULL )
7961 {
7962 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007964 cur = next;
7965 }
7966 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007969#if defined(MBEDTLS_SSL_PROTO_DTLS)
7970 mbedtls_free( handshake->verify_cookie );
7971 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007972 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007973#endif
7974
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007975 mbedtls_platform_zeroize( handshake,
7976 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007977}
7978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007980{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007981 if( session == NULL )
7982 return;
7983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007985 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987 mbedtls_x509_crt_free( session->peer_cert );
7988 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007989 }
Paul Bakkered27a042013-04-18 22:46:23 +02007990#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007991
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007992#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007994#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007995
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007996 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007997}
7998
Paul Bakker5121ce52009-01-03 21:22:43 +00007999/*
8000 * Free an SSL context
8001 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008003{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008004 if( ssl == NULL )
8005 return;
8006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008008
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008009 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008010 {
Angus Grattond8213d02016-05-25 20:56:48 +10008011 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008013 }
8014
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008015 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008016 {
Angus Grattond8213d02016-05-25 20:56:48 +10008017 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008019 }
8020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008021#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008022 if( ssl->compress_buf != NULL )
8023 {
Angus Grattond8213d02016-05-25 20:56:48 +10008024 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008026 }
8027#endif
8028
Paul Bakker48916f92012-09-16 19:57:18 +00008029 if( ssl->transform )
8030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008031 mbedtls_ssl_transform_free( ssl->transform );
8032 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008033 }
8034
8035 if( ssl->handshake )
8036 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008037 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8039 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008041 mbedtls_free( ssl->handshake );
8042 mbedtls_free( ssl->transform_negotiate );
8043 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008044 }
8045
Paul Bakkerc0463502013-02-14 11:19:38 +01008046 if( ssl->session )
8047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048 mbedtls_ssl_session_free( ssl->session );
8049 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008050 }
8051
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008052#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008053 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008054 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008055 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008057 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8061 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8064 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008065 }
8066#endif
8067
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008068#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008069 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008070#endif
8071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008073
Paul Bakker86f04f42013-02-14 11:20:09 +01008074 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008075 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008076}
8077
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008078/*
8079 * Initialze mbedtls_ssl_config
8080 */
8081void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8082{
8083 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8084}
8085
Simon Butcherc97b6972015-12-27 23:48:17 +00008086#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008087static int ssl_preset_default_hashes[] = {
8088#if defined(MBEDTLS_SHA512_C)
8089 MBEDTLS_MD_SHA512,
8090 MBEDTLS_MD_SHA384,
8091#endif
8092#if defined(MBEDTLS_SHA256_C)
8093 MBEDTLS_MD_SHA256,
8094 MBEDTLS_MD_SHA224,
8095#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008096#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008097 MBEDTLS_MD_SHA1,
8098#endif
8099 MBEDTLS_MD_NONE
8100};
Simon Butcherc97b6972015-12-27 23:48:17 +00008101#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008102
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008103static int ssl_preset_suiteb_ciphersuites[] = {
8104 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8105 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8106 0
8107};
8108
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008109#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008110static int ssl_preset_suiteb_hashes[] = {
8111 MBEDTLS_MD_SHA256,
8112 MBEDTLS_MD_SHA384,
8113 MBEDTLS_MD_NONE
8114};
8115#endif
8116
8117#if defined(MBEDTLS_ECP_C)
8118static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8119 MBEDTLS_ECP_DP_SECP256R1,
8120 MBEDTLS_ECP_DP_SECP384R1,
8121 MBEDTLS_ECP_DP_NONE
8122};
8123#endif
8124
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008125/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008126 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008127 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008128int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008129 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008130{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008131#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008132 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008133#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008134
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008135 /* Use the functions here so that they are covered in tests,
8136 * but otherwise access member directly for efficiency */
8137 mbedtls_ssl_conf_endpoint( conf, endpoint );
8138 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008139
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008140 /*
8141 * Things that are common to all presets
8142 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008143#if defined(MBEDTLS_SSL_CLI_C)
8144 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8145 {
8146 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8147#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8148 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8149#endif
8150 }
8151#endif
8152
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008153#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008154 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008155#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008156
8157#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8158 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8159#endif
8160
8161#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8162 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8163#endif
8164
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008165#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8166 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8167#endif
8168
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008169#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008170 conf->f_cookie_write = ssl_cookie_write_dummy;
8171 conf->f_cookie_check = ssl_cookie_check_dummy;
8172#endif
8173
8174#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8175 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8176#endif
8177
Janos Follath088ce432017-04-10 12:42:31 +01008178#if defined(MBEDTLS_SSL_SRV_C)
8179 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8180#endif
8181
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008182#if defined(MBEDTLS_SSL_PROTO_DTLS)
8183 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8184 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8185#endif
8186
8187#if defined(MBEDTLS_SSL_RENEGOTIATION)
8188 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008189 memset( conf->renego_period, 0x00, 2 );
8190 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008191#endif
8192
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008193#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8194 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8195 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008196 const unsigned char dhm_p[] =
8197 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8198 const unsigned char dhm_g[] =
8199 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8200
Hanno Beckera90658f2017-10-04 15:29:08 +01008201 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8202 dhm_p, sizeof( dhm_p ),
8203 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008204 {
8205 return( ret );
8206 }
8207 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008208#endif
8209
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008210 /*
8211 * Preset-specific defaults
8212 */
8213 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008214 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008215 /*
8216 * NSA Suite B
8217 */
8218 case MBEDTLS_SSL_PRESET_SUITEB:
8219 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8220 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8221 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8222 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8223
8224 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8225 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8226 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8227 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8228 ssl_preset_suiteb_ciphersuites;
8229
8230#if defined(MBEDTLS_X509_CRT_PARSE_C)
8231 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008232#endif
8233
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008234#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008235 conf->sig_hashes = ssl_preset_suiteb_hashes;
8236#endif
8237
8238#if defined(MBEDTLS_ECP_C)
8239 conf->curve_list = ssl_preset_suiteb_curves;
8240#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008241 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008242
8243 /*
8244 * Default
8245 */
8246 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008247 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8248 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8249 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8250 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8251 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8252 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8253 MBEDTLS_SSL_MIN_MINOR_VERSION :
8254 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008255 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8256 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8257
8258#if defined(MBEDTLS_SSL_PROTO_DTLS)
8259 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8260 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8261#endif
8262
8263 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8264 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8265 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8266 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8267 mbedtls_ssl_list_ciphersuites();
8268
8269#if defined(MBEDTLS_X509_CRT_PARSE_C)
8270 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8271#endif
8272
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008273#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008274 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008275#endif
8276
8277#if defined(MBEDTLS_ECP_C)
8278 conf->curve_list = mbedtls_ecp_grp_id_list();
8279#endif
8280
8281#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8282 conf->dhm_min_bitlen = 1024;
8283#endif
8284 }
8285
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008286 return( 0 );
8287}
8288
8289/*
8290 * Free mbedtls_ssl_config
8291 */
8292void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8293{
8294#if defined(MBEDTLS_DHM_C)
8295 mbedtls_mpi_free( &conf->dhm_P );
8296 mbedtls_mpi_free( &conf->dhm_G );
8297#endif
8298
8299#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8300 if( conf->psk != NULL )
8301 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008302 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008303 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00008304 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008305 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09008306 }
8307
8308 if( conf->psk_identity != NULL )
8309 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008310 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09008311 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00008312 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008313 conf->psk_identity_len = 0;
8314 }
8315#endif
8316
8317#if defined(MBEDTLS_X509_CRT_PARSE_C)
8318 ssl_key_cert_free( conf->key_cert );
8319#endif
8320
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008321 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008322}
8323
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008324#if defined(MBEDTLS_PK_C) && \
8325 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008326/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008329unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008330{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331#if defined(MBEDTLS_RSA_C)
8332 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8333 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008334#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008335#if defined(MBEDTLS_ECDSA_C)
8336 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8337 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008338#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008340}
8341
Hanno Becker7e5437a2017-04-28 17:15:26 +01008342unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8343{
8344 switch( type ) {
8345 case MBEDTLS_PK_RSA:
8346 return( MBEDTLS_SSL_SIG_RSA );
8347 case MBEDTLS_PK_ECDSA:
8348 case MBEDTLS_PK_ECKEY:
8349 return( MBEDTLS_SSL_SIG_ECDSA );
8350 default:
8351 return( MBEDTLS_SSL_SIG_ANON );
8352 }
8353}
8354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008356{
8357 switch( sig )
8358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359#if defined(MBEDTLS_RSA_C)
8360 case MBEDTLS_SSL_SIG_RSA:
8361 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008362#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008363#if defined(MBEDTLS_ECDSA_C)
8364 case MBEDTLS_SSL_SIG_ECDSA:
8365 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008366#endif
8367 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008368 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008369 }
8370}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008371#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008372
Hanno Becker7e5437a2017-04-28 17:15:26 +01008373#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8374 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8375
8376/* Find an entry in a signature-hash set matching a given hash algorithm. */
8377mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8378 mbedtls_pk_type_t sig_alg )
8379{
8380 switch( sig_alg )
8381 {
8382 case MBEDTLS_PK_RSA:
8383 return( set->rsa );
8384 case MBEDTLS_PK_ECDSA:
8385 return( set->ecdsa );
8386 default:
8387 return( MBEDTLS_MD_NONE );
8388 }
8389}
8390
8391/* Add a signature-hash-pair to a signature-hash set */
8392void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8393 mbedtls_pk_type_t sig_alg,
8394 mbedtls_md_type_t md_alg )
8395{
8396 switch( sig_alg )
8397 {
8398 case MBEDTLS_PK_RSA:
8399 if( set->rsa == MBEDTLS_MD_NONE )
8400 set->rsa = md_alg;
8401 break;
8402
8403 case MBEDTLS_PK_ECDSA:
8404 if( set->ecdsa == MBEDTLS_MD_NONE )
8405 set->ecdsa = md_alg;
8406 break;
8407
8408 default:
8409 break;
8410 }
8411}
8412
8413/* Allow exactly one hash algorithm for each signature. */
8414void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8415 mbedtls_md_type_t md_alg )
8416{
8417 set->rsa = md_alg;
8418 set->ecdsa = md_alg;
8419}
8420
8421#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8422 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8423
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008424/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008425 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008428{
8429 switch( hash )
8430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008431#if defined(MBEDTLS_MD5_C)
8432 case MBEDTLS_SSL_HASH_MD5:
8433 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008434#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008435#if defined(MBEDTLS_SHA1_C)
8436 case MBEDTLS_SSL_HASH_SHA1:
8437 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008438#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008439#if defined(MBEDTLS_SHA256_C)
8440 case MBEDTLS_SSL_HASH_SHA224:
8441 return( MBEDTLS_MD_SHA224 );
8442 case MBEDTLS_SSL_HASH_SHA256:
8443 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008444#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008445#if defined(MBEDTLS_SHA512_C)
8446 case MBEDTLS_SSL_HASH_SHA384:
8447 return( MBEDTLS_MD_SHA384 );
8448 case MBEDTLS_SSL_HASH_SHA512:
8449 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008450#endif
8451 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008453 }
8454}
8455
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008456/*
8457 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8458 */
8459unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8460{
8461 switch( md )
8462 {
8463#if defined(MBEDTLS_MD5_C)
8464 case MBEDTLS_MD_MD5:
8465 return( MBEDTLS_SSL_HASH_MD5 );
8466#endif
8467#if defined(MBEDTLS_SHA1_C)
8468 case MBEDTLS_MD_SHA1:
8469 return( MBEDTLS_SSL_HASH_SHA1 );
8470#endif
8471#if defined(MBEDTLS_SHA256_C)
8472 case MBEDTLS_MD_SHA224:
8473 return( MBEDTLS_SSL_HASH_SHA224 );
8474 case MBEDTLS_MD_SHA256:
8475 return( MBEDTLS_SSL_HASH_SHA256 );
8476#endif
8477#if defined(MBEDTLS_SHA512_C)
8478 case MBEDTLS_MD_SHA384:
8479 return( MBEDTLS_SSL_HASH_SHA384 );
8480 case MBEDTLS_MD_SHA512:
8481 return( MBEDTLS_SSL_HASH_SHA512 );
8482#endif
8483 default:
8484 return( MBEDTLS_SSL_HASH_NONE );
8485 }
8486}
8487
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008488#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008489/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008490 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008491 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008492 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008493int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008494{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008495 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008496
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008497 if( ssl->conf->curve_list == NULL )
8498 return( -1 );
8499
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008500 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008501 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008502 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008503
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008504 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008505}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008506#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008507
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008508#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008509/*
8510 * Check if a hash proposed by the peer is in our list.
8511 * Return 0 if we're willing to use it, -1 otherwise.
8512 */
8513int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8514 mbedtls_md_type_t md )
8515{
8516 const int *cur;
8517
8518 if( ssl->conf->sig_hashes == NULL )
8519 return( -1 );
8520
8521 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8522 if( *cur == (int) md )
8523 return( 0 );
8524
8525 return( -1 );
8526}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008527#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008529#if defined(MBEDTLS_X509_CRT_PARSE_C)
8530int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8531 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008532 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008533 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008534{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008535 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008536#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008537 int usage = 0;
8538#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008540 const char *ext_oid;
8541 size_t ext_len;
8542#endif
8543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008544#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8545 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008546 ((void) cert);
8547 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008548 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008549#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008551#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8552 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008553 {
8554 /* Server part of the key exchange */
8555 switch( ciphersuite->key_exchange )
8556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557 case MBEDTLS_KEY_EXCHANGE_RSA:
8558 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008559 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008560 break;
8561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008562 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8563 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8564 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8565 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008566 break;
8567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008568 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8569 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008570 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008571 break;
8572
8573 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008574 case MBEDTLS_KEY_EXCHANGE_NONE:
8575 case MBEDTLS_KEY_EXCHANGE_PSK:
8576 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8577 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008578 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008579 usage = 0;
8580 }
8581 }
8582 else
8583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008584 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8585 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008586 }
8587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008588 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008589 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008590 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008591 ret = -1;
8592 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008593#else
8594 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008597#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8598 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8601 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008602 }
8603 else
8604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8606 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008607 }
8608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008610 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008611 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008612 ret = -1;
8613 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008615
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008616 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008617}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008619
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008620/*
8621 * Convert version numbers to/from wire format
8622 * and, for DTLS, to/from TLS equivalent.
8623 *
8624 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008625 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008626 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8627 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008629void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008630 unsigned char ver[2] )
8631{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632#if defined(MBEDTLS_SSL_PROTO_DTLS)
8633 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008636 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8637
8638 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8639 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8640 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008641 else
8642#else
8643 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008644#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008645 {
8646 ver[0] = (unsigned char) major;
8647 ver[1] = (unsigned char) minor;
8648 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008649}
8650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008651void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008652 const unsigned char ver[2] )
8653{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654#if defined(MBEDTLS_SSL_PROTO_DTLS)
8655 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008656 {
8657 *major = 255 - ver[0] + 2;
8658 *minor = 255 - ver[1] + 1;
8659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008661 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8662 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008663 else
8664#else
8665 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008666#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008667 {
8668 *major = ver[0];
8669 *minor = ver[1];
8670 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008671}
8672
Simon Butcher99000142016-10-13 17:21:01 +01008673int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8674{
8675#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8676 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8677 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8678
8679 switch( md )
8680 {
8681#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8682#if defined(MBEDTLS_MD5_C)
8683 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008684 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008685#endif
8686#if defined(MBEDTLS_SHA1_C)
8687 case MBEDTLS_SSL_HASH_SHA1:
8688 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8689 break;
8690#endif
8691#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8692#if defined(MBEDTLS_SHA512_C)
8693 case MBEDTLS_SSL_HASH_SHA384:
8694 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8695 break;
8696#endif
8697#if defined(MBEDTLS_SHA256_C)
8698 case MBEDTLS_SSL_HASH_SHA256:
8699 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8700 break;
8701#endif
8702 default:
8703 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8704 }
8705
8706 return 0;
8707#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8708 (void) ssl;
8709 (void) md;
8710
8711 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8712#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8713}
8714
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008715#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8716 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8717int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8718 unsigned char *output,
8719 unsigned char *data, size_t data_len )
8720{
8721 int ret = 0;
8722 mbedtls_md5_context mbedtls_md5;
8723 mbedtls_sha1_context mbedtls_sha1;
8724
8725 mbedtls_md5_init( &mbedtls_md5 );
8726 mbedtls_sha1_init( &mbedtls_sha1 );
8727
8728 /*
8729 * digitally-signed struct {
8730 * opaque md5_hash[16];
8731 * opaque sha_hash[20];
8732 * };
8733 *
8734 * md5_hash
8735 * MD5(ClientHello.random + ServerHello.random
8736 * + ServerParams);
8737 * sha_hash
8738 * SHA(ClientHello.random + ServerHello.random
8739 * + ServerParams);
8740 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008741 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008742 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008743 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008744 goto exit;
8745 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008746 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008747 ssl->handshake->randbytes, 64 ) ) != 0 )
8748 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008749 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008750 goto exit;
8751 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008752 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008753 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008754 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008755 goto exit;
8756 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008757 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008758 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008759 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008760 goto exit;
8761 }
8762
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008763 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008764 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008765 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008766 goto exit;
8767 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008768 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008769 ssl->handshake->randbytes, 64 ) ) != 0 )
8770 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008771 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008772 goto exit;
8773 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008774 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008775 data_len ) ) != 0 )
8776 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008777 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008778 goto exit;
8779 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008780 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008781 output + 16 ) ) != 0 )
8782 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008783 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008784 goto exit;
8785 }
8786
8787exit:
8788 mbedtls_md5_free( &mbedtls_md5 );
8789 mbedtls_sha1_free( &mbedtls_sha1 );
8790
8791 if( ret != 0 )
8792 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8793 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8794
8795 return( ret );
8796
8797}
8798#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
8799 MBEDTLS_SSL_PROTO_TLS1_1 */
8800
8801#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8802 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8803int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02008804 unsigned char *hash, size_t *hashlen,
8805 unsigned char *data, size_t data_len,
8806 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008807{
8808 int ret = 0;
8809 mbedtls_md_context_t ctx;
8810 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02008811 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008812
8813 mbedtls_md_init( &ctx );
8814
8815 /*
8816 * digitally-signed struct {
8817 * opaque client_random[32];
8818 * opaque server_random[32];
8819 * ServerDHParams params;
8820 * };
8821 */
8822 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8823 {
8824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8825 goto exit;
8826 }
8827 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8828 {
8829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8830 goto exit;
8831 }
8832 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8833 {
8834 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8835 goto exit;
8836 }
8837 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8838 {
8839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8840 goto exit;
8841 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02008842 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008843 {
8844 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8845 goto exit;
8846 }
8847
8848exit:
8849 mbedtls_md_free( &ctx );
8850
8851 if( ret != 0 )
8852 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8853 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8854
8855 return( ret );
8856}
8857#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
8858 MBEDTLS_SSL_PROTO_TLS1_2 */
8859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860#endif /* MBEDTLS_SSL_TLS_C */