blob: 530f283b47d84f5686b7eb644016488f1a09e683 [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é-Gonnard9468ff12017-09-21 13:49:50 +02002848 const int ret_payload = mbedtls_ssl_get_max_out_record_payload( ssl );
2849 const size_t max_record_payload = (size_t) ret_payload;
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002850 /* DTLS handshake headers are 12 bytes */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002851 const size_t max_hs_fragment_len = max_record_payload - 12;
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002852
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002854
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002855 if( ret_payload < 0 )
2856 {
2857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload",
2858 ret_payload );
2859 return( ret_payload );
2860 }
2861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002863 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002865
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002866 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002867 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002868 ssl_swap_epochs( ssl );
2869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002871 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002872
2873 while( ssl->handshake->cur_msg != NULL )
2874 {
2875 int ret;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002876 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002877 /* Swap epochs before sending Finished: we can't do it after
2878 * sending ChangeCipherSpec, in case write returns WANT_READ.
2879 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2881 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002882 {
2883 ssl_swap_epochs( ssl );
2884 }
2885
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002886 /* CCS is copied as is, while HS messages may need fragmentation */
2887 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2888 {
2889 memcpy( ssl->out_msg, cur->p, cur->len );
2890 ssl->out_msglen = cur->len;
2891 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002892
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002893 /* Update position inside current message */
2894 ssl->handshake->cur_msg_p += cur->len;
2895 }
2896 else
2897 {
2898 const unsigned char * const p = ssl->handshake->cur_msg_p;
2899 const size_t hs_len = cur->len - 12;
2900 const size_t frag_off = p - ( cur->p + 12 );
2901 const size_t rem_len = hs_len - frag_off;
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002902 const size_t frag_len = rem_len > max_hs_fragment_len
2903 ? max_hs_fragment_len : rem_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002904
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002905 if( frag_off == 0 && frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002906 {
2907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
2908 (unsigned) hs_len,
2909 (unsigned) max_hs_fragment_len ) );
2910 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002911
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002912 /* Messages are stored with handshake headers as if not fragmented,
2913 * copy beginning of headers then fill fragmentation fields.
2914 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2915 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002916
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002917 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2918 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2919 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2920
2921 ssl->out_msg[ 9] = ( ( frag_len >> 16 ) & 0xff );
2922 ssl->out_msg[10] = ( ( frag_len >> 8 ) & 0xff );
2923 ssl->out_msg[11] = ( ( frag_len ) & 0xff );
2924
2925 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2926
Manuel Pégourié-Gonnard7e89c172018-08-13 12:45:26 +02002927 /* Copy the handshake message content and set records fields */
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002928 memcpy( ssl->out_msg + 12, p, frag_len );
2929 ssl->out_msglen = frag_len + 12;
2930 ssl->out_msgtype = cur->type;
2931
2932 /* Update position inside current message */
2933 ssl->handshake->cur_msg_p += frag_len;
2934 }
2935
2936 /* If done with the current message move to the next one if any */
2937 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2938 {
2939 if( cur->next != NULL )
2940 {
2941 ssl->handshake->cur_msg = cur->next;
2942 ssl->handshake->cur_msg_p = cur->next->p + 12;
2943 }
2944 else
2945 {
2946 ssl->handshake->cur_msg = NULL;
2947 ssl->handshake->cur_msg_p = NULL;
2948 }
2949 }
2950
2951 /* Actually send the message out */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002955 return( ret );
2956 }
2957 }
2958
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002959 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2961 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002962 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002965 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2966 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002967
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002969
2970 return( 0 );
2971}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002972
2973/*
2974 * To be called when the last message of an incoming flight is received.
2975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002977{
2978 /* We won't need to resend that one any more */
2979 ssl_flight_free( ssl->handshake->flight );
2980 ssl->handshake->flight = NULL;
2981 ssl->handshake->cur_msg = NULL;
2982
2983 /* The next incoming flight will start with this msg_seq */
2984 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2985
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002986 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002987 ssl_set_timer( ssl, 0 );
2988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2990 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002993 }
2994 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002996}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002997
2998/*
2999 * To be called when the last message of an outgoing flight is send.
3000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003002{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003003 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003004 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3007 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003010 }
3011 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003013}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003015
Paul Bakker5121ce52009-01-03 21:22:43 +00003016/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003017 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003018 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003019
3020/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003021 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003022 *
3023 * - fill in handshake headers
3024 * - update handshake checksum
3025 * - DTLS: save message for resending
3026 * - then pass to the record layer
3027 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003028 * DTLS: except for HelloRequest, messages are only queued, and will only be
3029 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003030 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003031 * Inputs:
3032 * - ssl->out_msglen: 4 + actual handshake message len
3033 * (4 is the size of handshake headers for TLS)
3034 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3035 * - ssl->out_msg + 4: the handshake message body
3036 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003037 * Ouputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003038 * - ssl->out_msglen: the length of the record contents
3039 * (including handshake headers but excluding record headers)
3040 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003041 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003042int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003043{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003044 int ret;
3045 const size_t hs_len = ssl->out_msglen - 4;
3046 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3049
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003050 /*
3051 * Sanity checks
3052 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003053 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3054 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3055 {
3056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3058 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003059
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003060 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3061 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3062 ssl->handshake == NULL )
3063 {
3064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3065 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3066 }
3067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003069 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003070 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003072 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3074 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003075 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003076#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003077
3078 /*
3079 * Fill handshake headers
3080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003082 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003083 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3084 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3085 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003086
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003087 /*
3088 * DTLS has additional fields in the Handshake layer,
3089 * between the length field and the actual payload:
3090 * uint16 message_seq;
3091 * uint24 fragment_offset;
3092 * uint24 fragment_length;
3093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003095 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003096 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003097 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003098 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003099 {
3100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3101 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003102 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003103 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003104 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3105 }
3106
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003107 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003108 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003109
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003110 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003111 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003112 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003113 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3114 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3115 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003116 }
3117 else
3118 {
3119 ssl->out_msg[4] = 0;
3120 ssl->out_msg[5] = 0;
3121 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003122
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003123 /* Handshake hashes are computed without fragmentation,
3124 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003125 memset( ssl->out_msg + 6, 0x00, 3 );
3126 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003127 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003129
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003130 /* Update running hashes of hanshake messages seen */
3131 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3132 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003133 }
3134
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003135 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003137 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003138 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003139 {
3140 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003143 return( ret );
3144 }
3145 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003146 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003147#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003148 {
3149 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
3150 {
3151 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3152 return( ret );
3153 }
3154 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003155
3156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3157
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003158 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003159}
3160
3161/*
3162 * Record layer functions
3163 */
3164
3165/*
3166 * Write current record.
3167 *
3168 * Uses:
3169 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3170 * - ssl->out_msglen: length of the record content (excl headers)
3171 * - ssl->out_msg: record content
3172 */
3173int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
3174{
3175 int ret, done = 0;
3176 size_t len = ssl->out_msglen;
3177
3178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003181 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003183 {
3184 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003187 return( ret );
3188 }
3189
3190 len = ssl->out_msglen;
3191 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3195 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 ret = mbedtls_ssl_hw_record_write( ssl );
3200 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3203 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003204 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003205
3206 if( ret == 0 )
3207 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003208 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003210 if( !done )
3211 {
3212 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003214 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003215
3216 ssl->out_len[0] = (unsigned char)( len >> 8 );
3217 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003218
Paul Bakker48916f92012-09-16 19:57:18 +00003219 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003220 {
3221 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003224 return( ret );
3225 }
3226
3227 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003228 ssl->out_len[0] = (unsigned char)( len >> 8 );
3229 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003230 }
3231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00003233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00003235 "version = [%d:%d], msglen = %d",
3236 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003237 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3240 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003241 }
3242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003246 return( ret );
3247 }
3248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003250
3251 return( 0 );
3252}
3253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003255/*
3256 * Mark bits in bitmask (used for DTLS HS reassembly)
3257 */
3258static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3259{
3260 unsigned int start_bits, end_bits;
3261
3262 start_bits = 8 - ( offset % 8 );
3263 if( start_bits != 8 )
3264 {
3265 size_t first_byte_idx = offset / 8;
3266
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003267 /* Special case */
3268 if( len <= start_bits )
3269 {
3270 for( ; len != 0; len-- )
3271 mask[first_byte_idx] |= 1 << ( start_bits - len );
3272
3273 /* Avoid potential issues with offset or len becoming invalid */
3274 return;
3275 }
3276
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003277 offset += start_bits; /* Now offset % 8 == 0 */
3278 len -= start_bits;
3279
3280 for( ; start_bits != 0; start_bits-- )
3281 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3282 }
3283
3284 end_bits = len % 8;
3285 if( end_bits != 0 )
3286 {
3287 size_t last_byte_idx = ( offset + len ) / 8;
3288
3289 len -= end_bits; /* Now len % 8 == 0 */
3290
3291 for( ; end_bits != 0; end_bits-- )
3292 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3293 }
3294
3295 memset( mask + offset / 8, 0xFF, len / 8 );
3296}
3297
3298/*
3299 * Check that bitmask is full
3300 */
3301static int ssl_bitmask_check( unsigned char *mask, size_t len )
3302{
3303 size_t i;
3304
3305 for( i = 0; i < len / 8; i++ )
3306 if( mask[i] != 0xFF )
3307 return( -1 );
3308
3309 for( i = 0; i < len % 8; i++ )
3310 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3311 return( -1 );
3312
3313 return( 0 );
3314}
3315
3316/*
3317 * Reassemble fragmented DTLS handshake messages.
3318 *
3319 * Use a temporary buffer for reassembly, divided in two parts:
3320 * - the first holds the reassembled message (including handshake header),
3321 * - the second holds a bitmask indicating which parts of the message
3322 * (excluding headers) have been received so far.
3323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003325{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003326 unsigned char *msg, *bitmask;
3327 size_t frag_len, frag_off;
3328 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3329
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003330 if( ssl->handshake == NULL )
3331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3333 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003334 }
3335
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003336 /*
3337 * For first fragment, check size and allocate buffer
3338 */
3339 if( ssl->handshake->hs_msg == NULL )
3340 {
3341 size_t alloc_len;
3342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003344 msg_len ) );
3345
Angus Grattond8213d02016-05-25 20:56:48 +10003346 if( ssl->in_hslen > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3349 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003350 }
3351
3352 /* The bitmask needs one bit per byte of message excluding header */
3353 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3354
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003355 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003356 if( ssl->handshake->hs_msg == NULL )
3357 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003359 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003360 }
3361
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003362 /* Prepare final header: copy msg_type, length and message_seq,
3363 * then add standardised fragment_offset and fragment_length */
3364 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3365 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3366 memcpy( ssl->handshake->hs_msg + 9,
3367 ssl->handshake->hs_msg + 1, 3 );
3368 }
3369 else
3370 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003371 /* Make sure msg_type and length are consistent */
3372 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3375 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003376 }
3377 }
3378
3379 msg = ssl->handshake->hs_msg + 12;
3380 bitmask = msg + msg_len;
3381
3382 /*
3383 * Check and copy current fragment
3384 */
3385 frag_off = ( ssl->in_msg[6] << 16 ) |
3386 ( ssl->in_msg[7] << 8 ) |
3387 ssl->in_msg[8];
3388 frag_len = ( ssl->in_msg[9] << 16 ) |
3389 ( ssl->in_msg[10] << 8 ) |
3390 ssl->in_msg[11];
3391
3392 if( frag_off + frag_len > msg_len )
3393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003395 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003397 }
3398
3399 if( frag_len + 12 > ssl->in_msglen )
3400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003402 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003404 }
3405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003407 frag_off, frag_len ) );
3408
3409 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3410 ssl_bitmask_set( bitmask, frag_off, frag_len );
3411
3412 /*
3413 * Do we have the complete message by now?
3414 * If yes, finalize it, else ask to read the next record.
3415 */
3416 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01003419 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003420 }
3421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003423
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003424 if( frag_len + 12 < ssl->in_msglen )
3425 {
3426 /*
3427 * We'got more handshake messages in the same record.
3428 * This case is not handled now because no know implementation does
3429 * that and it's hard to test, so we prefer to fail cleanly for now.
3430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3432 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003433 }
3434
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003435 if( ssl->in_left > ssl->next_record_offset )
3436 {
3437 /*
3438 * We've got more data in the buffer after the current record,
3439 * that we don't want to overwrite. Move it before writing the
3440 * reassembled message, and adjust in_left and next_record_offset.
3441 */
3442 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3443 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3444 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3445
3446 /* First compute and check new lengths */
3447 ssl->next_record_offset = new_remain - ssl->in_hdr;
3448 ssl->in_left = ssl->next_record_offset + remain_len;
3449
Angus Grattond8213d02016-05-25 20:56:48 +10003450 if( ssl->in_left > MBEDTLS_SSL_IN_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003451 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3454 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003455 }
3456
3457 memmove( new_remain, cur_remain, remain_len );
3458 }
3459
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003460 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003463 ssl->handshake->hs_msg = NULL;
3464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003466 ssl->in_msg, ssl->in_hslen );
3467
3468 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003469}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003471
Simon Butcher99000142016-10-13 17:21:01 +01003472int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003473{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003477 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003479 }
3480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003482 ( ssl->in_msg[1] << 16 ) |
3483 ( ssl->in_msg[2] << 8 ) |
3484 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003487 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003488 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003491 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003492 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003493 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003494 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003495
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003496 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003497 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3498 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3499 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3500 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003501 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003502 /* Retransmit only on last message from previous flight, to avoid
3503 * too many retransmissions.
3504 * Besides, No sane server ever retransmits HelloVerifyRequest */
3505 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003509 "message_seq = %d, start_of_flight = %d",
3510 recv_msg_seq,
3511 ssl->handshake->in_flight_start_seq ) );
3512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003516 return( ret );
3517 }
3518 }
3519 else
3520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003522 "message_seq = %d, expected = %d",
3523 recv_msg_seq,
3524 ssl->handshake->in_msg_seq ) );
3525 }
3526
Hanno Becker90333da2017-10-10 11:27:13 +01003527 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003528 }
3529 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003530
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003531 /* Reassemble if current message is fragmented or reassembly is
3532 * already in progress */
3533 if( ssl->in_msglen < ssl->in_hslen ||
3534 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3535 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3536 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003539
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003540 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003543 return( ret );
3544 }
3545 }
3546 }
3547 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003549 /* With TLS we don't handle fragmentation (for now) */
3550 if( ssl->in_msglen < ssl->in_hslen )
3551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3553 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003554 }
3555
Simon Butcher99000142016-10-13 17:21:01 +01003556 return( 0 );
3557}
3558
3559void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3560{
3561
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003562 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3563 ssl->handshake != NULL )
3564 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003565 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003566 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003567
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003568 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003570 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003571 ssl->handshake != NULL )
3572 {
3573 ssl->handshake->in_msg_seq++;
3574 }
3575#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003576}
3577
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003578/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003579 * DTLS anti-replay: RFC 6347 4.1.2.6
3580 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003581 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3582 * Bit n is set iff record number in_window_top - n has been seen.
3583 *
3584 * Usually, in_window_top is the last record number seen and the lsb of
3585 * in_window is set. The only exception is the initial state (record number 0
3586 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3589static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003590{
3591 ssl->in_window_top = 0;
3592 ssl->in_window = 0;
3593}
3594
3595static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3596{
3597 return( ( (uint64_t) buf[0] << 40 ) |
3598 ( (uint64_t) buf[1] << 32 ) |
3599 ( (uint64_t) buf[2] << 24 ) |
3600 ( (uint64_t) buf[3] << 16 ) |
3601 ( (uint64_t) buf[4] << 8 ) |
3602 ( (uint64_t) buf[5] ) );
3603}
3604
3605/*
3606 * Return 0 if sequence number is acceptable, -1 otherwise
3607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003609{
3610 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3611 uint64_t bit;
3612
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003613 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003614 return( 0 );
3615
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003616 if( rec_seqnum > ssl->in_window_top )
3617 return( 0 );
3618
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003619 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003620
3621 if( bit >= 64 )
3622 return( -1 );
3623
3624 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3625 return( -1 );
3626
3627 return( 0 );
3628}
3629
3630/*
3631 * Update replay window on new validated record
3632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003634{
3635 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3636
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003637 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003638 return;
3639
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003640 if( rec_seqnum > ssl->in_window_top )
3641 {
3642 /* Update window_top and the contents of the window */
3643 uint64_t shift = rec_seqnum - ssl->in_window_top;
3644
3645 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003646 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003647 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003648 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003649 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003650 ssl->in_window |= 1;
3651 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003652
3653 ssl->in_window_top = rec_seqnum;
3654 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003655 else
3656 {
3657 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003658 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003659
3660 if( bit < 64 ) /* Always true, but be extra sure */
3661 ssl->in_window |= (uint64_t) 1 << bit;
3662 }
3663}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003665
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003666#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003667/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003668static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3669
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003670/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003671 * Without any SSL context, check if a datagram looks like a ClientHello with
3672 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003673 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003674 *
3675 * - if cookie is valid, return 0
3676 * - if ClientHello looks superficially valid but cookie is not,
3677 * fill obuf and set olen, then
3678 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3679 * - otherwise return a specific error code
3680 */
3681static int ssl_check_dtls_clihlo_cookie(
3682 mbedtls_ssl_cookie_write_t *f_cookie_write,
3683 mbedtls_ssl_cookie_check_t *f_cookie_check,
3684 void *p_cookie,
3685 const unsigned char *cli_id, size_t cli_id_len,
3686 const unsigned char *in, size_t in_len,
3687 unsigned char *obuf, size_t buf_len, size_t *olen )
3688{
3689 size_t sid_len, cookie_len;
3690 unsigned char *p;
3691
3692 if( f_cookie_write == NULL || f_cookie_check == NULL )
3693 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3694
3695 /*
3696 * Structure of ClientHello with record and handshake headers,
3697 * and expected values. We don't need to check a lot, more checks will be
3698 * done when actually parsing the ClientHello - skipping those checks
3699 * avoids code duplication and does not make cookie forging any easier.
3700 *
3701 * 0-0 ContentType type; copied, must be handshake
3702 * 1-2 ProtocolVersion version; copied
3703 * 3-4 uint16 epoch; copied, must be 0
3704 * 5-10 uint48 sequence_number; copied
3705 * 11-12 uint16 length; (ignored)
3706 *
3707 * 13-13 HandshakeType msg_type; (ignored)
3708 * 14-16 uint24 length; (ignored)
3709 * 17-18 uint16 message_seq; copied
3710 * 19-21 uint24 fragment_offset; copied, must be 0
3711 * 22-24 uint24 fragment_length; (ignored)
3712 *
3713 * 25-26 ProtocolVersion client_version; (ignored)
3714 * 27-58 Random random; (ignored)
3715 * 59-xx SessionID session_id; 1 byte len + sid_len content
3716 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3717 * ...
3718 *
3719 * Minimum length is 61 bytes.
3720 */
3721 if( in_len < 61 ||
3722 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3723 in[3] != 0 || in[4] != 0 ||
3724 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3725 {
3726 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3727 }
3728
3729 sid_len = in[59];
3730 if( sid_len > in_len - 61 )
3731 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3732
3733 cookie_len = in[60 + sid_len];
3734 if( cookie_len > in_len - 60 )
3735 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3736
3737 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3738 cli_id, cli_id_len ) == 0 )
3739 {
3740 /* Valid cookie */
3741 return( 0 );
3742 }
3743
3744 /*
3745 * If we get here, we've got an invalid cookie, let's prepare HVR.
3746 *
3747 * 0-0 ContentType type; copied
3748 * 1-2 ProtocolVersion version; copied
3749 * 3-4 uint16 epoch; copied
3750 * 5-10 uint48 sequence_number; copied
3751 * 11-12 uint16 length; olen - 13
3752 *
3753 * 13-13 HandshakeType msg_type; hello_verify_request
3754 * 14-16 uint24 length; olen - 25
3755 * 17-18 uint16 message_seq; copied
3756 * 19-21 uint24 fragment_offset; copied
3757 * 22-24 uint24 fragment_length; olen - 25
3758 *
3759 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3760 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3761 *
3762 * Minimum length is 28.
3763 */
3764 if( buf_len < 28 )
3765 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3766
3767 /* Copy most fields and adapt others */
3768 memcpy( obuf, in, 25 );
3769 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3770 obuf[25] = 0xfe;
3771 obuf[26] = 0xff;
3772
3773 /* Generate and write actual cookie */
3774 p = obuf + 28;
3775 if( f_cookie_write( p_cookie,
3776 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3777 {
3778 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3779 }
3780
3781 *olen = p - obuf;
3782
3783 /* Go back and fill length fields */
3784 obuf[27] = (unsigned char)( *olen - 28 );
3785
3786 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3787 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3788 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3789
3790 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3791 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3792
3793 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3794}
3795
3796/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003797 * Handle possible client reconnect with the same UDP quadruplet
3798 * (RFC 6347 Section 4.2.8).
3799 *
3800 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3801 * that looks like a ClientHello.
3802 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003803 * - if the input looks like a ClientHello without cookies,
3804 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003805 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003806 * - if the input looks like a ClientHello with a valid cookie,
3807 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003808 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003809 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003810 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003811 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003812 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3813 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003814 */
3815static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3816{
3817 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003818 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003819
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003820 ret = ssl_check_dtls_clihlo_cookie(
3821 ssl->conf->f_cookie_write,
3822 ssl->conf->f_cookie_check,
3823 ssl->conf->p_cookie,
3824 ssl->cli_id, ssl->cli_id_len,
3825 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003826 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003827
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003828 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3829
3830 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003831 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003832 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003833 * If the error is permanent we'll catch it later,
3834 * if it's not, then hopefully it'll work next time. */
3835 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3836
3837 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003838 }
3839
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003840 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003841 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003842 /* Got a valid cookie, partially reset context */
3843 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3844 {
3845 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3846 return( ret );
3847 }
3848
3849 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003850 }
3851
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003852 return( ret );
3853}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003854#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003855
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003856/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003857 * ContentType type;
3858 * ProtocolVersion version;
3859 * uint16 epoch; // DTLS only
3860 * uint48 sequence_number; // DTLS only
3861 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003862 *
3863 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003864 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003865 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3866 *
3867 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003868 * 1. proceed with the record if this function returns 0
3869 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3870 * 3. return CLIENT_RECONNECT if this function return that value
3871 * 4. drop the whole datagram if this function returns anything else.
3872 * Point 2 is needed when the peer is resending, and we have already received
3873 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003875static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003876{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003877 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879 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 +02003880
Paul Bakker5121ce52009-01-03 21:22:43 +00003881 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003882 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003883 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003886 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003887 ssl->in_msgtype,
3888 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003889
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003890 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3892 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3893 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3894 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003897
3898#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01003899 /* Silently ignore invalid DTLS records as recommended by RFC 6347
3900 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003901 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3902#endif /* MBEDTLS_SSL_PROTO_DTLS */
3903 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3904 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
3905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003906 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003907 }
3908
3909 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003910 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3913 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003914 }
3915
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003916 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3919 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003920 }
3921
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003922 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10003923 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003924 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3927 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003928 }
3929
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003930 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003931 * DTLS-related tests.
3932 * Check epoch before checking length constraint because
3933 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3934 * message gets duplicated before the corresponding Finished message,
3935 * the second ChangeCipherSpec should be discarded because it belongs
3936 * to an old epoch, but not because its length is shorter than
3937 * the minimum record length for packets using the new record transform.
3938 * Note that these two kinds of failures are handled differently,
3939 * as an unexpected record is silently skipped but an invalid
3940 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003941 */
3942#if defined(MBEDTLS_SSL_PROTO_DTLS)
3943 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3944 {
3945 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3946
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003947 /* Check epoch (and sequence number) with DTLS */
3948 if( rec_epoch != ssl->in_epoch )
3949 {
3950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3951 "expected %d, received %d",
3952 ssl->in_epoch, rec_epoch ) );
3953
3954#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3955 /*
3956 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3957 * access the first byte of record content (handshake type), as we
3958 * have an active transform (possibly iv_len != 0), so use the
3959 * fact that the record header len is 13 instead.
3960 */
3961 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3962 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3963 rec_epoch == 0 &&
3964 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3965 ssl->in_left > 13 &&
3966 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3967 {
3968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3969 "from the same port" ) );
3970 return( ssl_handle_possible_reconnect( ssl ) );
3971 }
3972 else
3973#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3974 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3975 }
3976
3977#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3978 /* Replay detection only works for the current epoch */
3979 if( rec_epoch == ssl->in_epoch &&
3980 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3981 {
3982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3983 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3984 }
3985#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01003986
3987 /* Drop unexpected ChangeCipherSpec messages */
3988 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3989 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3990 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3991 {
3992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3993 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3994 }
3995
3996 /* Drop unexpected ApplicationData records,
3997 * except at the beginning of renegotiations */
3998 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
3999 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4000#if defined(MBEDTLS_SSL_RENEGOTIATION)
4001 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4002 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4003#endif
4004 )
4005 {
4006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4007 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4008 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004009 }
4010#endif /* MBEDTLS_SSL_PROTO_DTLS */
4011
Hanno Becker52c6dc62017-05-26 16:07:36 +01004012
4013 /* Check length against bounds of the current transform and version */
4014 if( ssl->transform_in == NULL )
4015 {
4016 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004017 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004018 {
4019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4020 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4021 }
4022 }
4023 else
4024 {
4025 if( ssl->in_msglen < ssl->transform_in->minlen )
4026 {
4027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4028 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4029 }
4030
4031#if defined(MBEDTLS_SSL_PROTO_SSL3)
4032 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004033 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004034 {
4035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4036 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4037 }
4038#endif
4039#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4040 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4041 /*
4042 * TLS encrypted messages can have up to 256 bytes of padding
4043 */
4044 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4045 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004046 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004047 {
4048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4049 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4050 }
4051#endif
4052 }
4053
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004054 return( 0 );
4055}
Paul Bakker5121ce52009-01-03 21:22:43 +00004056
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004057/*
4058 * If applicable, decrypt (and decompress) record content
4059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004061{
4062 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4065 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004067#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4068 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072 ret = mbedtls_ssl_hw_record_read( ssl );
4073 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4076 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004077 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004078
4079 if( ret == 0 )
4080 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004081 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004082#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004083 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004084 {
4085 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004088 return( ret );
4089 }
4090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004091 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004092 ssl->in_msg, ssl->in_msglen );
4093
Angus Grattond8213d02016-05-25 20:56:48 +10004094 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4097 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004098 }
4099 }
4100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004101#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004102 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004104 {
4105 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004108 return( ret );
4109 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004110 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004111#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004113#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004114 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004116 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004117 }
4118#endif
4119
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004120 return( 0 );
4121}
4122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004124
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004125/*
4126 * Read a record.
4127 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004128 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4129 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4130 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004133{
4134 int ret;
4135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004137
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004138 if( ssl->keep_current_message == 0 )
4139 {
4140 do {
Simon Butcher99000142016-10-13 17:21:01 +01004141
Hanno Becker90333da2017-10-10 11:27:13 +01004142 do ret = mbedtls_ssl_read_record_layer( ssl );
4143 while( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
4144
4145 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004146 {
4147 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4148 return( ret );
4149 }
4150
4151 ret = mbedtls_ssl_handle_message_type( ssl );
4152
Hanno Becker90333da2017-10-10 11:27:13 +01004153 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4154 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004155
4156 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004157 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004158 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004159 return( ret );
4160 }
4161
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004162 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4163 {
4164 mbedtls_ssl_update_handshake_status( ssl );
4165 }
Simon Butcher99000142016-10-13 17:21:01 +01004166 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004167 else
Simon Butcher99000142016-10-13 17:21:01 +01004168 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4170 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004171 }
4172
4173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4174
4175 return( 0 );
4176}
4177
4178int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4179{
4180 int ret;
4181
Hanno Becker4a810fb2017-05-24 16:27:30 +01004182 /*
4183 * Step A
4184 *
4185 * Consume last content-layer message and potentially
4186 * update in_msglen which keeps track of the contents'
4187 * consumption state.
4188 *
4189 * (1) Handshake messages:
4190 * Remove last handshake message, move content
4191 * and adapt in_msglen.
4192 *
4193 * (2) Alert messages:
4194 * Consume whole record content, in_msglen = 0.
4195 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004196 * (3) Change cipher spec:
4197 * Consume whole record content, in_msglen = 0.
4198 *
4199 * (4) Application data:
4200 * Don't do anything - the record layer provides
4201 * the application data as a stream transport
4202 * and consumes through mbedtls_ssl_read only.
4203 *
4204 */
4205
4206 /* Case (1): Handshake messages */
4207 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004208 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004209 /* Hard assertion to be sure that no application data
4210 * is in flight, as corrupting ssl->in_msglen during
4211 * ssl->in_offt != NULL is fatal. */
4212 if( ssl->in_offt != NULL )
4213 {
4214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4215 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4216 }
4217
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004218 /*
4219 * Get next Handshake message in the current record
4220 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004221
Hanno Becker4a810fb2017-05-24 16:27:30 +01004222 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004223 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004224 * current handshake content: If DTLS handshake
4225 * fragmentation is used, that's the fragment
4226 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004227 * size here is faulty and should be changed at
4228 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004229 * (2) While it doesn't seem to cause problems, one
4230 * has to be very careful not to assume that in_hslen
4231 * is always <= in_msglen in a sensible communication.
4232 * Again, it's wrong for DTLS handshake fragmentation.
4233 * The following check is therefore mandatory, and
4234 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004235 * Additionally, ssl->in_hslen might be arbitrarily out of
4236 * bounds after handling a DTLS message with an unexpected
4237 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004238 */
4239 if( ssl->in_hslen < ssl->in_msglen )
4240 {
4241 ssl->in_msglen -= ssl->in_hslen;
4242 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4243 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004244
Hanno Becker4a810fb2017-05-24 16:27:30 +01004245 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4246 ssl->in_msg, ssl->in_msglen );
4247 }
4248 else
4249 {
4250 ssl->in_msglen = 0;
4251 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004252
Hanno Becker4a810fb2017-05-24 16:27:30 +01004253 ssl->in_hslen = 0;
4254 }
4255 /* Case (4): Application data */
4256 else if( ssl->in_offt != NULL )
4257 {
4258 return( 0 );
4259 }
4260 /* Everything else (CCS & Alerts) */
4261 else
4262 {
4263 ssl->in_msglen = 0;
4264 }
4265
4266 /*
4267 * Step B
4268 *
4269 * Fetch and decode new record if current one is fully consumed.
4270 *
4271 */
4272
4273 if( ssl->in_msglen > 0 )
4274 {
4275 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004276 return( 0 );
4277 }
4278
Hanno Becker4a810fb2017-05-24 16:27:30 +01004279 /* Current record either fully processed or to be discarded. */
4280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004284 return( ret );
4285 }
4286
4287 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004290 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4291 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004292 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004293 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4294 {
4295 /* Skip unexpected record (but not whole datagram) */
4296 ssl->next_record_offset = ssl->in_msglen
4297 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004298
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4300 "(header)" ) );
4301 }
4302 else
4303 {
4304 /* Skip invalid record and the rest of the datagram */
4305 ssl->next_record_offset = 0;
4306 ssl->in_left = 0;
4307
4308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4309 "(header)" ) );
4310 }
4311
4312 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004313 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004314 }
4315#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004316 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004317 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004318
4319 /*
4320 * Read and optionally decrypt the message contents
4321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004322 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4323 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004325 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004326 return( ret );
4327 }
4328
4329 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004330#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004331 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004334 if( ssl->next_record_offset < ssl->in_left )
4335 {
4336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4337 }
4338 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004339 else
4340#endif
4341 ssl->in_left = 0;
4342
4343 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004346 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004347 {
4348 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004349 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4350 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004351 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004352 /* Except when waiting for Finished as a bad mac here
4353 * probably means something went wrong in the handshake
4354 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4355 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4356 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4357 {
4358#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4359 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4360 {
4361 mbedtls_ssl_send_alert_message( ssl,
4362 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4363 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4364 }
4365#endif
4366 return( ret );
4367 }
4368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004370 if( ssl->conf->badmac_limit != 0 &&
4371 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4374 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004375 }
4376#endif
4377
Hanno Becker4a810fb2017-05-24 16:27:30 +01004378 /* As above, invalid records cause
4379 * dismissal of the whole datagram. */
4380
4381 ssl->next_record_offset = 0;
4382 ssl->in_left = 0;
4383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004385 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004386 }
4387
4388 return( ret );
4389 }
4390 else
4391#endif
4392 {
4393 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004394#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4395 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397 mbedtls_ssl_send_alert_message( ssl,
4398 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4399 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004400 }
4401#endif
4402 return( ret );
4403 }
4404 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004405
Simon Butcher99000142016-10-13 17:21:01 +01004406 return( 0 );
4407}
4408
4409int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4410{
4411 int ret;
4412
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004413 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004414 * Handle particular types of records
4415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004416 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004417 {
Simon Butcher99000142016-10-13 17:21:01 +01004418 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4419 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004420 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004422 }
4423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004424 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004425 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004426 if( ssl->in_msglen != 2 )
4427 {
4428 /* Note: Standard allows for more than one 2 byte alert
4429 to be packed in a single message, but Mbed TLS doesn't
4430 currently support this. */
4431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4432 ssl->in_msglen ) );
4433 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4434 }
4435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004437 ssl->in_msg[0], ssl->in_msg[1] ) );
4438
4439 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004440 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004445 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004446 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004447 }
4448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4450 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4453 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004454 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004455
4456#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4457 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4458 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4459 {
Hanno Becker90333da2017-10-10 11:27:13 +01004460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004461 /* Will be handled when trying to parse ServerHello */
4462 return( 0 );
4463 }
4464#endif
4465
4466#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4467 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4468 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4469 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4470 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4471 {
4472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4473 /* Will be handled in mbedtls_ssl_parse_certificate() */
4474 return( 0 );
4475 }
4476#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4477
4478 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004479 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004480 }
4481
Hanno Beckerc76c6192017-06-06 10:03:17 +01004482#if defined(MBEDTLS_SSL_PROTO_DTLS)
4483 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4484 ssl->handshake != NULL &&
4485 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4486 {
4487 ssl_handshake_wrapup_free_hs_transform( ssl );
4488 }
4489#endif
4490
Paul Bakker5121ce52009-01-03 21:22:43 +00004491 return( 0 );
4492}
4493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004494int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004495{
4496 int ret;
4497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004498 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4499 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4500 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004501 {
4502 return( ret );
4503 }
4504
4505 return( 0 );
4506}
4507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004509 unsigned char level,
4510 unsigned char message )
4511{
4512 int ret;
4513
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004514 if( ssl == NULL || ssl->conf == NULL )
4515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004518 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004520 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004521 ssl->out_msglen = 2;
4522 ssl->out_msg[0] = level;
4523 ssl->out_msg[1] = message;
4524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004525 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004528 return( ret );
4529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004531
4532 return( 0 );
4533}
4534
Paul Bakker5121ce52009-01-03 21:22:43 +00004535/*
4536 * Handshake functions
4537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004538#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4539 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4540 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4541 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4542 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4543 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4544 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004545/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004547{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4553 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004554 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4555 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004558 ssl->state++;
4559 return( 0 );
4560 }
4561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004564}
4565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004567{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004572 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4573 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004574 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4575 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004578 ssl->state++;
4579 return( 0 );
4580 }
4581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4583 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004584}
Gilles Peskinef9828522017-05-03 12:28:43 +02004585
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004586#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004587/* Some certificate support -> implement write and parse */
4588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004589int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004590{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004591 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004592 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593 const mbedtls_x509_crt *crt;
4594 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004598 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4599 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004600 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4601 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004604 ssl->state++;
4605 return( 0 );
4606 }
4607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004608#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004609 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004610 {
4611 if( ssl->client_auth == 0 )
4612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004614 ssl->state++;
4615 return( 0 );
4616 }
4617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004618#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004619 /*
4620 * If using SSLv3 and got no cert, send an Alert message
4621 * (otherwise an empty Certificate message will be sent).
4622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4624 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004625 {
4626 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004627 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4628 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4629 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004632 goto write_msg;
4633 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004635 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004636#endif /* MBEDTLS_SSL_CLI_C */
4637#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004638 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4643 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004644 }
4645 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004646#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004649
4650 /*
4651 * 0 . 0 handshake type
4652 * 1 . 3 handshake length
4653 * 4 . 6 length of all certs
4654 * 7 . 9 length of cert. 1
4655 * 10 . n-1 peer certificate
4656 * n . n+2 length of cert. 2
4657 * n+3 . ... upper level cert, etc.
4658 */
4659 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004660 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004661
Paul Bakker29087132010-03-21 21:03:34 +00004662 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004663 {
4664 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10004665 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10004668 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004669 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004670 }
4671
4672 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4673 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4674 ssl->out_msg[i + 2] = (unsigned char)( n );
4675
4676 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4677 i += n; crt = crt->next;
4678 }
4679
4680 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4681 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4682 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4683
4684 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004685 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4686 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004687
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004688#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004689write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004690#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004691
4692 ssl->state++;
4693
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004694 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004695 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004696 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004697 return( ret );
4698 }
4699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004701
Paul Bakkered27a042013-04-18 22:46:23 +02004702 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004703}
4704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004705int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004706{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004707 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004708 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004709 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004710 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004711 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004715 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4716 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004717 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4718 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004721 ssl->state++;
4722 return( 0 );
4723 }
4724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004725#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004726 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004727 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4728 {
4729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4730 ssl->state++;
4731 return( 0 );
4732 }
4733
4734#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4735 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4736 authmode = ssl->handshake->sni_authmode;
4737#endif
4738
4739 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4740 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004741 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004742 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004744 ssl->state++;
4745 return( 0 );
4746 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004747#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004750 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004751 /* mbedtls_ssl_read_record may have sent an alert already. We
4752 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004754 return( ret );
4755 }
4756
4757 ssl->state++;
4758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759#if defined(MBEDTLS_SSL_SRV_C)
4760#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004761 /*
4762 * Check if the client sent an empty certificate
4763 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004764 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004766 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004767 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004768 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4769 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4770 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004773
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004774 /* The client was asked for a certificate but didn't send
4775 one. The client should know what's going on, so we
4776 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004777 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004778 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004779 return( 0 );
4780 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004781 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004782 }
4783 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004784#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004786#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4787 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004788 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004791 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4792 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4793 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4794 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004797
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004798 /* The client was asked for a certificate but didn't send
4799 one. The client should know what's going on, so we
4800 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004801 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004802 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004803 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004804 else
4805 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004806 }
4807 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4809 MBEDTLS_SSL_PROTO_TLS1_2 */
4810#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004815 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4816 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004818 }
4819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4821 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004824 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4825 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004826 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004827 }
4828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004829 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004830
Paul Bakker5121ce52009-01-03 21:22:43 +00004831 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004832 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004833 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004834 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004835
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004836 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004840 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4841 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004842 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004843 }
4844
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004845 /* In case we tried to reuse a session but it failed */
4846 if( ssl->session_negotiate->peer_cert != NULL )
4847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004848 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4849 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004850 }
4851
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004852 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004853 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004854 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004857 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4858 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004859 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004860 }
4861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004862 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004863
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004864 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004865
4866 while( i < ssl->in_hslen )
4867 {
Philippe Antoine747fd532018-05-30 09:13:21 +02004868 if ( i + 3 > ssl->in_hslen ) {
4869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4870 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4871 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4872 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
4873 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004874 if( ssl->in_msg[i] != 0 )
4875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004877 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4878 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004879 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004880 }
4881
4882 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4883 | (unsigned int) ssl->in_msg[i + 2];
4884 i += 3;
4885
4886 if( n < 128 || i + n > ssl->in_hslen )
4887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004889 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4890 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004891 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004892 }
4893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004894 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004895 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004896 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004897 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004898 case 0: /*ok*/
4899 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
4900 /* Ignore certificate with an unknown algorithm: maybe a
4901 prior certificate was already trusted. */
4902 break;
4903
4904 case MBEDTLS_ERR_X509_ALLOC_FAILED:
4905 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
4906 goto crt_parse_der_failed;
4907
4908 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
4909 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4910 goto crt_parse_der_failed;
4911
4912 default:
4913 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4914 crt_parse_der_failed:
4915 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004916 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004917 return( ret );
4918 }
4919
4920 i += n;
4921 }
4922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004923 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004924
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004925 /*
4926 * On client, make sure the server cert doesn't change during renego to
4927 * avoid "triple handshake" attack: https://secure-resumption.com/
4928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004929#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004930 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004931 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004932 {
4933 if( ssl->session->peer_cert == NULL )
4934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004936 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4937 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004938 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004939 }
4940
4941 if( ssl->session->peer_cert->raw.len !=
4942 ssl->session_negotiate->peer_cert->raw.len ||
4943 memcmp( ssl->session->peer_cert->raw.p,
4944 ssl->session_negotiate->peer_cert->raw.p,
4945 ssl->session->peer_cert->raw.len ) != 0 )
4946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004948 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4949 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004951 }
4952 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004953#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004954
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004955 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004956 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004957 mbedtls_x509_crt *ca_chain;
4958 mbedtls_x509_crl *ca_crl;
4959
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004960#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004961 if( ssl->handshake->sni_ca_chain != NULL )
4962 {
4963 ca_chain = ssl->handshake->sni_ca_chain;
4964 ca_crl = ssl->handshake->sni_ca_crl;
4965 }
4966 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004967#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004968 {
4969 ca_chain = ssl->conf->ca_chain;
4970 ca_crl = ssl->conf->ca_crl;
4971 }
4972
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004973 /*
4974 * Main check: verify certificate
4975 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004976 ret = mbedtls_x509_crt_verify_with_profile(
4977 ssl->session_negotiate->peer_cert,
4978 ca_chain, ca_crl,
4979 ssl->conf->cert_profile,
4980 ssl->hostname,
4981 &ssl->session_negotiate->verify_result,
4982 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004983
4984 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004986 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004987 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004988
4989 /*
4990 * Secondary checks: always done, but change 'ret' only if it was 0
4991 */
4992
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004993#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004995 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004996
4997 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004998 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004999 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005000 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005001 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005004 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005005 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005006 }
5007 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005008#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005010 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005011 ciphersuite_info,
5012 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005013 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005016 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005017 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005018 }
5019
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005020 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5021 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5022 * with details encoded in the verification flags. All other kinds
5023 * of error codes, including those from the user provided f_vrfy
5024 * functions, are treated as fatal and lead to a failure of
5025 * ssl_parse_certificate even if verification was optional. */
5026 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5027 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5028 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5029 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005030 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005031 }
5032
5033 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5034 {
5035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5036 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5037 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005038
5039 if( ret != 0 )
5040 {
5041 /* The certificate may have been rejected for several reasons.
5042 Pick one and send the corresponding alert. Which alert to send
5043 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005044 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5045 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5046 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5047 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5048 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5049 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5050 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5051 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5052 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5053 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5054 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5055 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5056 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5057 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5058 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5059 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5060 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5061 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5062 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5063 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005064 else
5065 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005066 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5067 alert );
5068 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005069
Hanno Beckere6706e62017-05-15 16:05:15 +01005070#if defined(MBEDTLS_DEBUG_C)
5071 if( ssl->session_negotiate->verify_result != 0 )
5072 {
5073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5074 ssl->session_negotiate->verify_result ) );
5075 }
5076 else
5077 {
5078 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5079 }
5080#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005081 }
5082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005084
5085 return( ret );
5086}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005087#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5088 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5089 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5090 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5091 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5092 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5093 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005095int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005096{
5097 int ret;
5098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005101 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005102 ssl->out_msglen = 1;
5103 ssl->out_msg[0] = 1;
5104
Paul Bakker5121ce52009-01-03 21:22:43 +00005105 ssl->state++;
5106
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005107 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005108 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005110 return( ret );
5111 }
5112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005113 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005114
5115 return( 0 );
5116}
5117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005118int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005119{
5120 int ret;
5121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005124 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005127 return( ret );
5128 }
5129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005133 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5134 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005135 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005136 }
5137
5138 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5139 {
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_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005143 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005144 }
5145
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005146 /*
5147 * Switch to our negotiated transform and session parameters for inbound
5148 * data.
5149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005150 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005151 ssl->transform_in = ssl->transform_negotiate;
5152 ssl->session_in = ssl->session_negotiate;
5153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005154#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005155 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005157#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005158 ssl_dtls_replay_reset( ssl );
5159#endif
5160
5161 /* Increment epoch */
5162 if( ++ssl->in_epoch == 0 )
5163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005165 /* This is highly unlikely to happen for legitimate reasons, so
5166 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005167 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005168 }
5169 }
5170 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005171#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005172 memset( ssl->in_ctr, 0, 8 );
5173
5174 /*
5175 * Set the in_msg pointer to the correct location based on IV length
5176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005177 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005178 {
5179 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
5180 ssl->transform_negotiate->fixed_ivlen;
5181 }
5182 else
5183 ssl->in_msg = ssl->in_iv;
5184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005185#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5186 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005188 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005190 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005191 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5192 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005193 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005194 }
5195 }
5196#endif
5197
Paul Bakker5121ce52009-01-03 21:22:43 +00005198 ssl->state++;
5199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005201
5202 return( 0 );
5203}
5204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005205void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5206 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005207{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005208 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005210#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5211 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5212 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005213 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005214 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005215#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005216#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5217#if defined(MBEDTLS_SHA512_C)
5218 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005219 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5220 else
5221#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005222#if defined(MBEDTLS_SHA256_C)
5223 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005224 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005225 else
5226#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005227#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005230 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005231 }
Paul Bakker380da532012-04-18 16:10:25 +00005232}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005234void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005236#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5237 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005238 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5239 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005240#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005241#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5242#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005243 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005244#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005245#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005246 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005247#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005248#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005249}
5250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005251static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005252 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005253{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005254#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5255 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005256 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5257 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005258#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005259#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5260#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005261 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005262#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005263#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005264 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005265#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005266#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005267}
5268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005269#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5270 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5271static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005272 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005273{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005274 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5275 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005276}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005277#endif
Paul Bakker380da532012-04-18 16:10:25 +00005278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005279#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5280#if defined(MBEDTLS_SHA256_C)
5281static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005282 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005283{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005284 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005285}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005286#endif
Paul Bakker380da532012-04-18 16:10:25 +00005287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005288#if defined(MBEDTLS_SHA512_C)
5289static void ssl_update_checksum_sha384( 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_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005293}
Paul Bakker769075d2012-11-24 11:26:46 +01005294#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005297#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005298static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005300{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005301 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005302 mbedtls_md5_context md5;
5303 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005304
Paul Bakker5121ce52009-01-03 21:22:43 +00005305 unsigned char padbuf[48];
5306 unsigned char md5sum[16];
5307 unsigned char sha1sum[20];
5308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005310 if( !session )
5311 session = ssl->session;
5312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005314
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005315 mbedtls_md5_init( &md5 );
5316 mbedtls_sha1_init( &sha1 );
5317
5318 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5319 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005320
5321 /*
5322 * SSLv3:
5323 * hash =
5324 * MD5( master + pad2 +
5325 * MD5( handshake + sender + master + pad1 ) )
5326 * + SHA1( master + pad2 +
5327 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005328 */
5329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005330#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005331 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5332 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005336 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5337 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005338#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005340 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005341 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005342
Paul Bakker1ef83d62012-04-11 12:09:53 +00005343 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005344
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005345 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5346 mbedtls_md5_update_ret( &md5, session->master, 48 );
5347 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5348 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005349
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005350 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5351 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5352 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5353 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005354
Paul Bakker1ef83d62012-04-11 12:09:53 +00005355 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005356
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005357 mbedtls_md5_starts_ret( &md5 );
5358 mbedtls_md5_update_ret( &md5, session->master, 48 );
5359 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5360 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5361 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005362
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005363 mbedtls_sha1_starts_ret( &sha1 );
5364 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5365 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5366 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5367 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005370
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005371 mbedtls_md5_free( &md5 );
5372 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005373
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005374 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5375 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
5376 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005379}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005380#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005383static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005384 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005385{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005386 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005387 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005388 mbedtls_md5_context md5;
5389 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005390 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005393 if( !session )
5394 session = ssl->session;
5395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005397
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005398 mbedtls_md5_init( &md5 );
5399 mbedtls_sha1_init( &sha1 );
5400
5401 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5402 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005403
Paul Bakker1ef83d62012-04-11 12:09:53 +00005404 /*
5405 * TLSv1:
5406 * hash = PRF( master, finished_label,
5407 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5408 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005411 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5412 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005413#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005416 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5417 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005418#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005420 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005421 ? "client finished"
5422 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005423
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005424 mbedtls_md5_finish_ret( &md5, padbuf );
5425 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005426
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005427 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005428 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005430 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005431
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005432 mbedtls_md5_free( &md5 );
5433 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005434
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005435 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005439#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005441#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5442#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005443static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005444 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005445{
5446 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005447 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005448 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005449 unsigned char padbuf[32];
5450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005452 if( !session )
5453 session = ssl->session;
5454
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005455 mbedtls_sha256_init( &sha256 );
5456
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005458
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005459 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005460
5461 /*
5462 * TLSv1.2:
5463 * hash = PRF( master, finished_label,
5464 * Hash( handshake ) )[0.11]
5465 */
5466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467#if !defined(MBEDTLS_SHA256_ALT)
5468 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005469 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005470#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005473 ? "client finished"
5474 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005475
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005476 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005477
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005478 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005479 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005481 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005482
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005483 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005484
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005485 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005488}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005491#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005492static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005493 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005494{
5495 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005496 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005497 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005498 unsigned char padbuf[48];
5499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005500 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005501 if( !session )
5502 session = ssl->session;
5503
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005504 mbedtls_sha512_init( &sha512 );
5505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005507
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005508 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005509
5510 /*
5511 * TLSv1.2:
5512 * hash = PRF( master, finished_label,
5513 * Hash( handshake ) )[0.11]
5514 */
5515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005516#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005517 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5518 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005519#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005522 ? "client finished"
5523 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005524
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005525 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005526
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005527 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005528 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005531
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005532 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005533
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005534 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005537}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538#endif /* MBEDTLS_SHA512_C */
5539#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005541static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005542{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005544
5545 /*
5546 * Free our handshake params
5547 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02005548 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005550 ssl->handshake = NULL;
5551
5552 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005553 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005554 */
5555 if( ssl->transform )
5556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 mbedtls_ssl_transform_free( ssl->transform );
5558 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005559 }
5560 ssl->transform = ssl->transform_negotiate;
5561 ssl->transform_negotiate = NULL;
5562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005564}
5565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005567{
5568 int resume = ssl->handshake->resume;
5569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005572#if defined(MBEDTLS_SSL_RENEGOTIATION)
5573 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005575 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005576 ssl->renego_records_seen = 0;
5577 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005578#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005579
5580 /*
5581 * Free the previous session and switch in the current one
5582 */
Paul Bakker0a597072012-09-25 21:55:46 +00005583 if( ssl->session )
5584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005586 /* RFC 7366 3.1: keep the EtM state */
5587 ssl->session_negotiate->encrypt_then_mac =
5588 ssl->session->encrypt_then_mac;
5589#endif
5590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005591 mbedtls_ssl_session_free( ssl->session );
5592 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005593 }
5594 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005595 ssl->session_negotiate = NULL;
5596
Paul Bakker0a597072012-09-25 21:55:46 +00005597 /*
5598 * Add cache entry
5599 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005600 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005601 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005602 resume == 0 )
5603 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005604 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005606 }
Paul Bakker0a597072012-09-25 21:55:46 +00005607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005609 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005610 ssl->handshake->flight != NULL )
5611 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005612 /* Cancel handshake timer */
5613 ssl_set_timer( ssl, 0 );
5614
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005615 /* Keep last flight around in case we need to resend it:
5616 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005618 }
5619 else
5620#endif
5621 ssl_handshake_wrapup_free_hs_transform( ssl );
5622
Paul Bakker48916f92012-09-16 19:57:18 +00005623 ssl->state++;
5624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005626}
5627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005628int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005629{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005630 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005633
Paul Bakker92be97b2013-01-02 17:30:03 +01005634 /*
5635 * Set the out_msg pointer to the correct location based on IV length
5636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005638 {
5639 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5640 ssl->transform_negotiate->fixed_ivlen;
5641 }
5642 else
5643 ssl->out_msg = ssl->out_iv;
5644
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005645 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005646
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005647 /*
5648 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5649 * may define some other value. Currently (early 2016), no defined
5650 * ciphersuite does this (and this is unlikely to change as activity has
5651 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005653 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005656 ssl->verify_data_len = hash_len;
5657 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005658#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005659
Paul Bakker5121ce52009-01-03 21:22:43 +00005660 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5662 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005663
5664 /*
5665 * In case of session resuming, invert the client and server
5666 * ChangeCipherSpec messages order.
5667 */
Paul Bakker0a597072012-09-25 21:55:46 +00005668 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005671 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005672 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005673#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005674#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005675 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005677#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005678 }
5679 else
5680 ssl->state++;
5681
Paul Bakker48916f92012-09-16 19:57:18 +00005682 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005683 * Switch to our negotiated transform and session parameters for outbound
5684 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005686 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005688#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005689 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005690 {
5691 unsigned char i;
5692
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005693 /* Remember current epoch settings for resending */
5694 ssl->handshake->alt_transform_out = ssl->transform_out;
5695 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5696
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005697 /* Set sequence_number to zero */
5698 memset( ssl->out_ctr + 2, 0, 6 );
5699
5700 /* Increment epoch */
5701 for( i = 2; i > 0; i-- )
5702 if( ++ssl->out_ctr[i - 1] != 0 )
5703 break;
5704
5705 /* The loop goes to its end iff the counter is wrapping */
5706 if( i == 0 )
5707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5709 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005710 }
5711 }
5712 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005713#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005714 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005715
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005716 ssl->transform_out = ssl->transform_negotiate;
5717 ssl->session_out = ssl->session_negotiate;
5718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5720 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005724 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5725 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005726 }
5727 }
5728#endif
5729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005731 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005733#endif
5734
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005735 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005736 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005737 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005738 return( ret );
5739 }
5740
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005741#if defined(MBEDTLS_SSL_PROTO_DTLS)
5742 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5743 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
5744 {
5745 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
5746 return( ret );
5747 }
5748#endif
5749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005751
5752 return( 0 );
5753}
5754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005755#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005756#define SSL_MAX_HASH_LEN 36
5757#else
5758#define SSL_MAX_HASH_LEN 12
5759#endif
5760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005761int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005762{
Paul Bakker23986e52011-04-24 08:57:21 +00005763 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005764 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005765 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005768
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005769 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005771 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005774 return( ret );
5775 }
5776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005780 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5781 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005783 }
5784
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005785 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005786#if defined(MBEDTLS_SSL_PROTO_SSL3)
5787 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005788 hash_len = 36;
5789 else
5790#endif
5791 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5794 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005797 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5798 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005800 }
5801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005803 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005806 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5807 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005808 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005809 }
5810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005812 ssl->verify_data_len = hash_len;
5813 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005814#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005815
Paul Bakker0a597072012-09-25 21:55:46 +00005816 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005819 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005821#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005822#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005823 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005825#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005826 }
5827 else
5828 ssl->state++;
5829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005831 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005833#endif
5834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005836
5837 return( 0 );
5838}
5839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005841{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005842 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5845 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5846 mbedtls_md5_init( &handshake->fin_md5 );
5847 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005848 mbedtls_md5_starts_ret( &handshake->fin_md5 );
5849 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005850#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5852#if defined(MBEDTLS_SHA256_C)
5853 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005854 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005855#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856#if defined(MBEDTLS_SHA512_C)
5857 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005858 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005859#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005860#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005861
5862 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005863
5864#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5865 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5866 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5867#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869#if defined(MBEDTLS_DHM_C)
5870 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005871#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872#if defined(MBEDTLS_ECDH_C)
5873 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005874#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02005875#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005876 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02005877#if defined(MBEDTLS_SSL_CLI_C)
5878 handshake->ecjpake_cache = NULL;
5879 handshake->ecjpake_cache_len = 0;
5880#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005881#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005882
5883#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5884 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5885#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005886}
5887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005889{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005890 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5893 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005895 mbedtls_md_init( &transform->md_ctx_enc );
5896 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005897}
5898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005899void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005900{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005901 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005902}
5903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005904static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005905{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005906 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005907 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005908 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005909 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005910 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005911 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02005912 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005913
5914 /*
5915 * Either the pointers are now NULL or cleared properly and can be freed.
5916 * Now allocate missing structures.
5917 */
5918 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005919 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005920 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005921 }
Paul Bakker48916f92012-09-16 19:57:18 +00005922
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005923 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005924 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005925 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005926 }
Paul Bakker48916f92012-09-16 19:57:18 +00005927
Paul Bakker82788fb2014-10-20 13:59:19 +02005928 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005929 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005930 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005931 }
Paul Bakker48916f92012-09-16 19:57:18 +00005932
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005933 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005934 if( ssl->handshake == NULL ||
5935 ssl->transform_negotiate == NULL ||
5936 ssl->session_negotiate == NULL )
5937 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005940 mbedtls_free( ssl->handshake );
5941 mbedtls_free( ssl->transform_negotiate );
5942 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005943
5944 ssl->handshake = NULL;
5945 ssl->transform_negotiate = NULL;
5946 ssl->session_negotiate = NULL;
5947
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005948 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005949 }
5950
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005951 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005952 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005953 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005954 ssl_handshake_params_init( ssl->handshake );
5955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005957 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5958 {
5959 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005960
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005961 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5962 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5963 else
5964 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005965
5966 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005967 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005968#endif
5969
Paul Bakker48916f92012-09-16 19:57:18 +00005970 return( 0 );
5971}
5972
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005973#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005974/* Dummy cookie callbacks for defaults */
5975static int ssl_cookie_write_dummy( void *ctx,
5976 unsigned char **p, unsigned char *end,
5977 const unsigned char *cli_id, size_t cli_id_len )
5978{
5979 ((void) ctx);
5980 ((void) p);
5981 ((void) end);
5982 ((void) cli_id);
5983 ((void) cli_id_len);
5984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005985 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005986}
5987
5988static int ssl_cookie_check_dummy( void *ctx,
5989 const unsigned char *cookie, size_t cookie_len,
5990 const unsigned char *cli_id, size_t cli_id_len )
5991{
5992 ((void) ctx);
5993 ((void) cookie);
5994 ((void) cookie_len);
5995 ((void) cli_id);
5996 ((void) cli_id_len);
5997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005999}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006000#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006001
Paul Bakker5121ce52009-01-03 21:22:43 +00006002/*
6003 * Initialize an SSL context
6004 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006005void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6006{
6007 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6008}
6009
6010/*
6011 * Setup an SSL context
6012 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006013int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006014 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006015{
Paul Bakker48916f92012-09-16 19:57:18 +00006016 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006017
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006018 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006019
6020 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006021 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006022 */
Angus Grattond8213d02016-05-25 20:56:48 +10006023 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6024 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006025 {
Angus Grattond8213d02016-05-25 20:56:48 +10006026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6027 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6028 }
6029
6030 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6031 if( ssl->out_buf == NULL )
6032 {
6033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006035 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006036 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006037 }
6038
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006039#if defined(MBEDTLS_SSL_PROTO_DTLS)
6040 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6041 {
6042 ssl->out_hdr = ssl->out_buf;
6043 ssl->out_ctr = ssl->out_buf + 3;
6044 ssl->out_len = ssl->out_buf + 11;
6045 ssl->out_iv = ssl->out_buf + 13;
6046 ssl->out_msg = ssl->out_buf + 13;
6047
6048 ssl->in_hdr = ssl->in_buf;
6049 ssl->in_ctr = ssl->in_buf + 3;
6050 ssl->in_len = ssl->in_buf + 11;
6051 ssl->in_iv = ssl->in_buf + 13;
6052 ssl->in_msg = ssl->in_buf + 13;
6053 }
6054 else
6055#endif
6056 {
6057 ssl->out_ctr = ssl->out_buf;
6058 ssl->out_hdr = ssl->out_buf + 8;
6059 ssl->out_len = ssl->out_buf + 11;
6060 ssl->out_iv = ssl->out_buf + 13;
6061 ssl->out_msg = ssl->out_buf + 13;
6062
6063 ssl->in_ctr = ssl->in_buf;
6064 ssl->in_hdr = ssl->in_buf + 8;
6065 ssl->in_len = ssl->in_buf + 11;
6066 ssl->in_iv = ssl->in_buf + 13;
6067 ssl->in_msg = ssl->in_buf + 13;
6068 }
6069
Paul Bakker48916f92012-09-16 19:57:18 +00006070 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6071 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006072
6073 return( 0 );
6074}
6075
6076/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006077 * Reset an initialized and used SSL context for re-use while retaining
6078 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006079 *
6080 * If partial is non-zero, keep data in the input buffer and client ID.
6081 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006082 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006083static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006084{
Paul Bakker48916f92012-09-16 19:57:18 +00006085 int ret;
6086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006087 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006088
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006089 /* Cancel any possibly running timer */
6090 ssl_set_timer( ssl, 0 );
6091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006092#if defined(MBEDTLS_SSL_RENEGOTIATION)
6093 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006094 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006095
6096 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6098 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006099#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006101
Paul Bakker7eb013f2011-10-06 12:37:39 +00006102 ssl->in_offt = NULL;
6103
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006104 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006105 ssl->in_msgtype = 0;
6106 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006107 if( partial == 0 )
6108 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006110 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006111 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006112#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006114 ssl_dtls_replay_reset( ssl );
6115#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006116
6117 ssl->in_hslen = 0;
6118 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006119
6120 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006121
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006122 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006123 ssl->out_msgtype = 0;
6124 ssl->out_msglen = 0;
6125 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006126#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6127 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006128 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006129#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006130
Paul Bakker48916f92012-09-16 19:57:18 +00006131 ssl->transform_in = NULL;
6132 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006133
Hanno Becker78640902018-08-13 16:35:15 +01006134 ssl->session_in = NULL;
6135 ssl->session_out = NULL;
6136
Angus Grattond8213d02016-05-25 20:56:48 +10006137 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006138 if( partial == 0 )
Angus Grattond8213d02016-05-25 20:56:48 +10006139 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00006140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006141#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6142 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6145 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6148 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006149 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006150 }
6151#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006152
Paul Bakker48916f92012-09-16 19:57:18 +00006153 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155 mbedtls_ssl_transform_free( ssl->transform );
6156 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006157 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006158 }
Paul Bakker48916f92012-09-16 19:57:18 +00006159
Paul Bakkerc0463502013-02-14 11:19:38 +01006160 if( ssl->session )
6161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006162 mbedtls_ssl_session_free( ssl->session );
6163 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006164 ssl->session = NULL;
6165 }
6166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006168 ssl->alpn_chosen = NULL;
6169#endif
6170
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006171#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006172 if( partial == 0 )
6173 {
6174 mbedtls_free( ssl->cli_id );
6175 ssl->cli_id = NULL;
6176 ssl->cli_id_len = 0;
6177 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006178#endif
6179
Paul Bakker48916f92012-09-16 19:57:18 +00006180 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6181 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006182
6183 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006184}
6185
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006186/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006187 * Reset an initialized and used SSL context for re-use while retaining
6188 * all application-set variables, function pointers and data.
6189 */
6190int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6191{
6192 return( ssl_session_reset_int( ssl, 0 ) );
6193}
6194
6195/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006196 * SSL set accessors
6197 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006198void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006199{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006200 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006201}
6202
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006203void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006204{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006205 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006206}
6207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006208#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006209void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006210{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006211 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006212}
6213#endif
6214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006216void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006217{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006218 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006219}
6220#endif
6221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006223void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006224{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006225 conf->hs_timeout_min = min;
6226 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006227}
6228#endif
6229
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006230void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006231{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006232 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006233}
6234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006236void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006237 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006238 void *p_vrfy )
6239{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006240 conf->f_vrfy = f_vrfy;
6241 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006242}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006244
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006245void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006246 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006247 void *p_rng )
6248{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006249 conf->f_rng = f_rng;
6250 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006251}
6252
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006253void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006254 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006255 void *p_dbg )
6256{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006257 conf->f_dbg = f_dbg;
6258 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006259}
6260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006262 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006263 mbedtls_ssl_send_t *f_send,
6264 mbedtls_ssl_recv_t *f_recv,
6265 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006266{
6267 ssl->p_bio = p_bio;
6268 ssl->f_send = f_send;
6269 ssl->f_recv = f_recv;
6270 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006271}
6272
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006273void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006274{
6275 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006276}
6277
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006278void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6279 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006280 mbedtls_ssl_set_timer_t *f_set_timer,
6281 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006282{
6283 ssl->p_timer = p_timer;
6284 ssl->f_set_timer = f_set_timer;
6285 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006286
6287 /* Make sure we start with no timer running */
6288 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006289}
6290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006292void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006293 void *p_cache,
6294 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6295 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006296{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006297 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006298 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006299 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303#if defined(MBEDTLS_SSL_CLI_C)
6304int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006305{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006306 int ret;
6307
6308 if( ssl == NULL ||
6309 session == NULL ||
6310 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006311 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006313 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006314 }
6315
6316 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6317 return( ret );
6318
Paul Bakker0a597072012-09-25 21:55:46 +00006319 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006320
6321 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006322}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006324
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006325void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006326 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006327{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006328 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6329 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6330 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6331 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006332}
6333
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006334void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006335 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006336 int major, int minor )
6337{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006338 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006339 return;
6340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006342 return;
6343
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006344 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006345}
6346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006348void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006349 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006350{
6351 conf->cert_profile = profile;
6352}
6353
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006354/* Append a new keycert entry to a (possibly empty) list */
6355static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6356 mbedtls_x509_crt *cert,
6357 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006358{
niisato8ee24222018-06-25 19:05:48 +09006359 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006360
niisato8ee24222018-06-25 19:05:48 +09006361 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6362 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006363 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006364
niisato8ee24222018-06-25 19:05:48 +09006365 new_cert->cert = cert;
6366 new_cert->key = key;
6367 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006368
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006369 /* Update head is the list was null, else add to the end */
6370 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006371 {
niisato8ee24222018-06-25 19:05:48 +09006372 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006373 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006374 else
6375 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006376 mbedtls_ssl_key_cert *cur = *head;
6377 while( cur->next != NULL )
6378 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09006379 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006380 }
6381
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006382 return( 0 );
6383}
6384
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006385int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006386 mbedtls_x509_crt *own_cert,
6387 mbedtls_pk_context *pk_key )
6388{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006389 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006390}
6391
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006392void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006393 mbedtls_x509_crt *ca_chain,
6394 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006395{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006396 conf->ca_chain = ca_chain;
6397 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006398}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006399#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006400
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006401#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6402int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6403 mbedtls_x509_crt *own_cert,
6404 mbedtls_pk_context *pk_key )
6405{
6406 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6407 own_cert, pk_key ) );
6408}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006409
6410void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6411 mbedtls_x509_crt *ca_chain,
6412 mbedtls_x509_crl *ca_crl )
6413{
6414 ssl->handshake->sni_ca_chain = ca_chain;
6415 ssl->handshake->sni_ca_crl = ca_crl;
6416}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006417
6418void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6419 int authmode )
6420{
6421 ssl->handshake->sni_authmode = authmode;
6422}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006423#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6424
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006425#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006426/*
6427 * Set EC J-PAKE password for current handshake
6428 */
6429int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6430 const unsigned char *pw,
6431 size_t pw_len )
6432{
6433 mbedtls_ecjpake_role role;
6434
Janos Follath8eb64132016-06-03 15:40:57 +01006435 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6437
6438 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6439 role = MBEDTLS_ECJPAKE_SERVER;
6440 else
6441 role = MBEDTLS_ECJPAKE_CLIENT;
6442
6443 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6444 role,
6445 MBEDTLS_MD_SHA256,
6446 MBEDTLS_ECP_DP_SECP256R1,
6447 pw, pw_len ) );
6448}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006449#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006451#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006452int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006453 const unsigned char *psk, size_t psk_len,
6454 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006455{
Paul Bakker6db455e2013-09-18 17:29:31 +02006456 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006459 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6460 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006461
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006462 /* Identity len will be encoded on two bytes */
6463 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10006464 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006465 {
6466 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6467 }
6468
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006469 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006470 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006471 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006472
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006473 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006474 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006475 conf->psk_len = 0;
6476 }
6477 if( conf->psk_identity != NULL )
6478 {
6479 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006480 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006481 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006482 }
6483
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006484 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6485 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006486 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006487 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006488 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006489 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006490 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006491 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006492 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006493
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006494 conf->psk_len = psk_len;
6495 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006496
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006497 memcpy( conf->psk, psk, conf->psk_len );
6498 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006499
6500 return( 0 );
6501}
6502
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006503int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6504 const unsigned char *psk, size_t psk_len )
6505{
6506 if( psk == NULL || ssl->handshake == NULL )
6507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6508
6509 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6511
6512 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006513 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006514 mbedtls_platform_zeroize( ssl->handshake->psk,
6515 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006516 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006517 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006518 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006519
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006520 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006521 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006522
6523 ssl->handshake->psk_len = psk_len;
6524 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6525
6526 return( 0 );
6527}
6528
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006529void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006531 size_t),
6532 void *p_psk )
6533{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006534 conf->f_psk = f_psk;
6535 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006536}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006538
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006539#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006540
6541#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006542int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006543{
6544 int ret;
6545
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006546 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6547 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6548 {
6549 mbedtls_mpi_free( &conf->dhm_P );
6550 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006551 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006552 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006553
6554 return( 0 );
6555}
Hanno Becker470a8c42017-10-04 15:28:46 +01006556#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006557
Hanno Beckera90658f2017-10-04 15:29:08 +01006558int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6559 const unsigned char *dhm_P, size_t P_len,
6560 const unsigned char *dhm_G, size_t G_len )
6561{
6562 int ret;
6563
6564 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6565 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6566 {
6567 mbedtls_mpi_free( &conf->dhm_P );
6568 mbedtls_mpi_free( &conf->dhm_G );
6569 return( ret );
6570 }
6571
6572 return( 0 );
6573}
Paul Bakker5121ce52009-01-03 21:22:43 +00006574
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006575int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006576{
6577 int ret;
6578
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006579 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6580 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6581 {
6582 mbedtls_mpi_free( &conf->dhm_P );
6583 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006584 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006585 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006586
6587 return( 0 );
6588}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006589#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006590
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006591#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6592/*
6593 * Set the minimum length for Diffie-Hellman parameters
6594 */
6595void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6596 unsigned int bitlen )
6597{
6598 conf->dhm_min_bitlen = bitlen;
6599}
6600#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6601
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006602#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006603/*
6604 * Set allowed/preferred hashes for handshake signatures
6605 */
6606void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6607 const int *hashes )
6608{
6609 conf->sig_hashes = hashes;
6610}
Hanno Becker947194e2017-04-07 13:25:49 +01006611#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006612
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006613#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006614/*
6615 * Set the allowed elliptic curves
6616 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006617void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006618 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006619{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006620 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006621}
Hanno Becker947194e2017-04-07 13:25:49 +01006622#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006623
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006624#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006626{
Hanno Becker947194e2017-04-07 13:25:49 +01006627 /* Initialize to suppress unnecessary compiler warning */
6628 size_t hostname_len = 0;
6629
6630 /* Check if new hostname is valid before
6631 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006632 if( hostname != NULL )
6633 {
6634 hostname_len = strlen( hostname );
6635
6636 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6637 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6638 }
6639
6640 /* Now it's clear that we will overwrite the old hostname,
6641 * so we can free it safely */
6642
6643 if( ssl->hostname != NULL )
6644 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006645 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01006646 mbedtls_free( ssl->hostname );
6647 }
6648
6649 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006650
Paul Bakker5121ce52009-01-03 21:22:43 +00006651 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006652 {
6653 ssl->hostname = NULL;
6654 }
6655 else
6656 {
6657 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006658 if( ssl->hostname == NULL )
6659 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006660
Hanno Becker947194e2017-04-07 13:25:49 +01006661 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006662
Hanno Becker947194e2017-04-07 13:25:49 +01006663 ssl->hostname[hostname_len] = '\0';
6664 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006665
6666 return( 0 );
6667}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006668#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006669
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006670#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006671void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006673 const unsigned char *, size_t),
6674 void *p_sni )
6675{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006676 conf->f_sni = f_sni;
6677 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006678}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006681#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006682int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006683{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006684 size_t cur_len, tot_len;
6685 const char **p;
6686
6687 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006688 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6689 * MUST NOT be truncated."
6690 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006691 */
6692 tot_len = 0;
6693 for( p = protos; *p != NULL; p++ )
6694 {
6695 cur_len = strlen( *p );
6696 tot_len += cur_len;
6697
6698 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006700 }
6701
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006702 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006703
6704 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006705}
6706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006708{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006709 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006710}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006712
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006713void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006714{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006715 conf->max_major_ver = major;
6716 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006717}
6718
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006719void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006720{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006721 conf->min_major_ver = major;
6722 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006723}
6724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006726void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006727{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006728 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006729}
6730#endif
6731
Janos Follath088ce432017-04-10 12:42:31 +01006732#if defined(MBEDTLS_SSL_SRV_C)
6733void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6734 char cert_req_ca_list )
6735{
6736 conf->cert_req_ca_list = cert_req_ca_list;
6737}
6738#endif
6739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006741void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006742{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006743 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006744}
6745#endif
6746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006748void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006749{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006750 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006751}
6752#endif
6753
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006754#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006755void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006756{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006757 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006758}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006759#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006760
Manuel Pégourié-Gonnard0b1d9b22017-09-21 13:15:27 +02006761#if defined(MBEDTLS_SSL_PROTO_DTLS)
6762void mbedtls_ssl_conf_mtu( mbedtls_ssl_config *conf, uint16_t mtu )
6763{
6764 conf->mtu = mtu;
6765}
6766#endif
6767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006769int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006770{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10006772 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006775 }
6776
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006777 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006778
6779 return( 0 );
6780}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006781#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006784void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006785{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006786 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006787}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006788#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006791void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006792{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006793 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006794}
6795#endif
6796
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006797void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006798{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006799 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006800}
6801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006803void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006804{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006805 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006806}
6807
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006808void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006809{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006810 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006811}
6812
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006813void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006814 const unsigned char period[8] )
6815{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006816 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006817}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006818#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006821#if defined(MBEDTLS_SSL_CLI_C)
6822void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006823{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006824 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006825}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006826#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006827
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006828#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006829void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6830 mbedtls_ssl_ticket_write_t *f_ticket_write,
6831 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6832 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006833{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006834 conf->f_ticket_write = f_ticket_write;
6835 conf->f_ticket_parse = f_ticket_parse;
6836 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006837}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006838#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006840
Robert Cragie4feb7ae2015-10-02 13:33:37 +01006841#if defined(MBEDTLS_SSL_EXPORT_KEYS)
6842void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
6843 mbedtls_ssl_export_keys_t *f_export_keys,
6844 void *p_export_keys )
6845{
6846 conf->f_export_keys = f_export_keys;
6847 conf->p_export_keys = p_export_keys;
6848}
6849#endif
6850
Gilles Peskineb74a1c72018-04-24 13:09:22 +02006851#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006852void mbedtls_ssl_conf_async_private_cb(
6853 mbedtls_ssl_config *conf,
6854 mbedtls_ssl_async_sign_t *f_async_sign,
6855 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
6856 mbedtls_ssl_async_resume_t *f_async_resume,
6857 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006858 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006859{
6860 conf->f_async_sign_start = f_async_sign;
6861 conf->f_async_decrypt_start = f_async_decrypt;
6862 conf->f_async_resume = f_async_resume;
6863 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006864 conf->p_async_config_data = async_config_data;
6865}
6866
Gilles Peskine8f97af72018-04-26 11:46:10 +02006867void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
6868{
6869 return( conf->p_async_config_data );
6870}
6871
Gilles Peskine1febfef2018-04-30 11:54:39 +02006872void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006873{
6874 if( ssl->handshake == NULL )
6875 return( NULL );
6876 else
6877 return( ssl->handshake->user_async_ctx );
6878}
6879
Gilles Peskine1febfef2018-04-30 11:54:39 +02006880void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006881 void *ctx )
6882{
6883 if( ssl->handshake != NULL )
6884 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006885}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02006886#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006887
Paul Bakker5121ce52009-01-03 21:22:43 +00006888/*
6889 * SSL get accessors
6890 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006892{
6893 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6894}
6895
Hanno Becker8b170a02017-10-10 11:51:19 +01006896int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
6897{
6898 /*
6899 * Case A: We're currently holding back
6900 * a message for further processing.
6901 */
6902
6903 if( ssl->keep_current_message == 1 )
6904 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006905 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006906 return( 1 );
6907 }
6908
6909 /*
6910 * Case B: Further records are pending in the current datagram.
6911 */
6912
6913#if defined(MBEDTLS_SSL_PROTO_DTLS)
6914 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6915 ssl->in_left > ssl->next_record_offset )
6916 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006917 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006918 return( 1 );
6919 }
6920#endif /* MBEDTLS_SSL_PROTO_DTLS */
6921
6922 /*
6923 * Case C: A handshake message is being processed.
6924 */
6925
Hanno Becker8b170a02017-10-10 11:51:19 +01006926 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
6927 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006929 return( 1 );
6930 }
6931
6932 /*
6933 * Case D: An application data message is being processed
6934 */
6935 if( ssl->in_offt != NULL )
6936 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006938 return( 1 );
6939 }
6940
6941 /*
6942 * In all other cases, the rest of the message can be dropped.
6943 * As in ssl_read_record_layer, this needs to be adapted if
6944 * we implement support for multiple alerts in single records.
6945 */
6946
6947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
6948 return( 0 );
6949}
6950
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006951uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006952{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006953 if( ssl->session != NULL )
6954 return( ssl->session->verify_result );
6955
6956 if( ssl->session_negotiate != NULL )
6957 return( ssl->session_negotiate->verify_result );
6958
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006959 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006960}
6961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006963{
Paul Bakker926c8e42013-03-06 10:23:34 +01006964 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006965 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006968}
6969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006971{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006973 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006974 {
6975 switch( ssl->minor_ver )
6976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006978 return( "DTLSv1.0" );
6979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006981 return( "DTLSv1.2" );
6982
6983 default:
6984 return( "unknown (DTLS)" );
6985 }
6986 }
6987#endif
6988
Paul Bakker43ca69c2011-01-15 17:35:19 +00006989 switch( ssl->minor_ver )
6990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006992 return( "SSLv3.0" );
6993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006995 return( "TLSv1.0" );
6996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006998 return( "TLSv1.1" );
6999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007001 return( "TLSv1.2" );
7002
Paul Bakker43ca69c2011-01-15 17:35:19 +00007003 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007004 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007005 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007006}
7007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007009{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007010 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007012 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007013
Hanno Becker78640902018-08-13 16:35:15 +01007014 if( transform == NULL )
7015 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017#if defined(MBEDTLS_ZLIB_SUPPORT)
7018 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7019 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007020 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007021#endif
7022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025 case MBEDTLS_MODE_GCM:
7026 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007027 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007029 transform_expansion = transform->minlen;
7030 break;
7031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007033
7034 block_size = mbedtls_cipher_get_block_size(
7035 &transform->cipher_ctx_enc );
7036
7037#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7038 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7039 {
7040 /* Expansion due to addition of
7041 * - MAC
7042 * - CBC padding (theoretically up to 256 bytes, but
7043 * we never use more than block_size)
7044 * - explicit IV
7045 */
7046 transform_expansion = transform->maclen + 2 * block_size;
7047 }
7048 else
7049#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7050 {
7051 /* No explicit IV prior to TLS 1.1. */
7052 transform_expansion = transform->maclen + block_size;
7053 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007054 break;
7055
7056 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007059 }
7060
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007061 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007062}
7063
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007064#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7065size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7066{
7067 size_t max_len;
7068
7069 /*
7070 * Assume mfl_code is correct since it was checked when set
7071 */
Angus Grattond8213d02016-05-25 20:56:48 +10007072 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007073
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007074 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007075 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007076 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007077 {
Angus Grattond8213d02016-05-25 20:56:48 +10007078 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007079 }
7080
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007081 /* During a handshake, use the value being negotiated */
7082 if( ssl->session_negotiate != NULL &&
7083 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7084 {
7085 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7086 }
7087
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007088 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007089}
7090#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7091
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007092int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7093{
7094 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7095
7096#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7097 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7098
7099 if( max_len > mfl )
7100 max_len = mfl;
7101#endif
7102
7103#if defined(MBEDTLS_SSL_PROTO_DTLS)
7104 if( ssl->conf->mtu != 0 )
7105 {
7106 const size_t mtu = ssl->conf->mtu;
7107 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7108 const size_t overhead = (size_t) ret;
7109
7110 if( ret < 0 )
7111 return( ret );
7112
7113 if( mtu <= overhead )
7114 {
7115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7116 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7117 }
7118
7119 if( max_len > mtu - overhead )
7120 max_len = mtu - overhead;
7121 }
7122#endif
7123
7124 return( (int) max_len );
7125}
7126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127#if defined(MBEDTLS_X509_CRT_PARSE_C)
7128const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007129{
7130 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007131 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007132
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007133 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007137#if defined(MBEDTLS_SSL_CLI_C)
7138int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007139{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007140 if( ssl == NULL ||
7141 dst == NULL ||
7142 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007143 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007146 }
7147
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007148 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007151
Paul Bakker5121ce52009-01-03 21:22:43 +00007152/*
Paul Bakker1961b702013-01-25 14:49:24 +01007153 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007158
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007159 if( ssl == NULL || ssl->conf == NULL )
7160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007163 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007165#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007167 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007169#endif
7170
Paul Bakker1961b702013-01-25 14:49:24 +01007171 return( ret );
7172}
7173
7174/*
7175 * Perform the SSL handshake
7176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007177int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007178{
7179 int ret = 0;
7180
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007181 if( ssl == NULL || ssl->conf == NULL )
7182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007189
7190 if( ret != 0 )
7191 break;
7192 }
7193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007195
7196 return( ret );
7197}
7198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199#if defined(MBEDTLS_SSL_RENEGOTIATION)
7200#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007201/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007202 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007205{
7206 int ret;
7207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007209
7210 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7212 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007213
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007214 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007215 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007217 return( ret );
7218 }
7219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007221
7222 return( 0 );
7223}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007225
7226/*
7227 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228 * - any side: calling mbedtls_ssl_renegotiate(),
7229 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7230 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007231 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007232 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007233 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007236{
7237 int ret;
7238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007240
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007241 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7242 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007243
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007244 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7245 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007247 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007249 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007250 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007251 ssl->handshake->out_msg_seq = 1;
7252 else
7253 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007254 }
7255#endif
7256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7258 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007263 return( ret );
7264 }
7265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007267
7268 return( 0 );
7269}
7270
7271/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007272 * Renegotiate current connection on client,
7273 * or request renegotiation on server
7274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007276{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007278
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007279 if( ssl == NULL || ssl->conf == NULL )
7280 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007283 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007284 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7287 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007290
7291 /* Did we already try/start sending HelloRequest? */
7292 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007294
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007295 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007296 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007300 /*
7301 * On client, either start the renegotiation process or,
7302 * if already in progress, continue the handshake
7303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007308
7309 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007312 return( ret );
7313 }
7314 }
7315 else
7316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007320 return( ret );
7321 }
7322 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007324
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007325 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007326}
7327
7328/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007329 * Check record counters and renegotiate if they're above the limit.
7330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007332{
Andres AG2196c7f2016-12-15 17:01:16 +00007333 size_t ep_len = ssl_ep_len( ssl );
7334 int in_ctr_cmp;
7335 int out_ctr_cmp;
7336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007337 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7338 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007339 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007340 {
7341 return( 0 );
7342 }
7343
Andres AG2196c7f2016-12-15 17:01:16 +00007344 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7345 ssl->conf->renego_period + ep_len, 8 - ep_len );
7346 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
7347 ssl->conf->renego_period + ep_len, 8 - ep_len );
7348
7349 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007350 {
7351 return( 0 );
7352 }
7353
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007356}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007358
7359/*
7360 * Receive application data decrypted from the SSL layer
7361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007363{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007364 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007365 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007366
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007367 if( ssl == NULL || ssl->conf == NULL )
7368 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007373 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007376 return( ret );
7377
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007378 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007380 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007381 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007382 return( ret );
7383 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007384 }
7385#endif
7386
Hanno Becker4a810fb2017-05-24 16:27:30 +01007387 /*
7388 * Check if renegotiation is necessary and/or handshake is
7389 * in process. If yes, perform/continue, and fall through
7390 * if an unexpected packet is received while the client
7391 * is waiting for the ServerHello.
7392 *
7393 * (There is no equivalent to the last condition on
7394 * the server-side as it is not treated as within
7395 * a handshake while waiting for the ClientHello
7396 * after a renegotiation request.)
7397 */
7398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007400 ret = ssl_check_ctr_renegotiate( ssl );
7401 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7402 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007405 return( ret );
7406 }
7407#endif
7408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007412 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7413 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007416 return( ret );
7417 }
7418 }
7419
Hanno Beckere41158b2017-10-23 13:30:32 +01007420 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01007421 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007422 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007423 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007424 if( ssl->f_get_timer != NULL &&
7425 ssl->f_get_timer( ssl->p_timer ) == -1 )
7426 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007427 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007428 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007429
Hanno Becker4a810fb2017-05-24 16:27:30 +01007430 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007431 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007432 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7433 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007434
Hanno Becker4a810fb2017-05-24 16:27:30 +01007435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7436 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007437 }
7438
7439 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007441 {
7442 /*
7443 * OpenSSL sends empty messages to randomize the IV
7444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007448 return( 0 );
7449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007451 return( ret );
7452 }
7453 }
7454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007458
Hanno Becker4a810fb2017-05-24 16:27:30 +01007459 /*
7460 * - For client-side, expect SERVER_HELLO_REQUEST.
7461 * - For server-side, expect CLIENT_HELLO.
7462 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7463 */
7464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007466 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007468 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007471
7472 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007473#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007474 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007475 {
7476 continue;
7477 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007478#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007480 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007481#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007482
Hanno Becker4a810fb2017-05-24 16:27:30 +01007483#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007484 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007485 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007488
7489 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007490#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007491 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007492 {
7493 continue;
7494 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007495#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007497 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007498#endif /* MBEDTLS_SSL_SRV_C */
7499
Hanno Becker21df7f92017-10-17 11:03:26 +01007500#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007501 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007502 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7503 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7504 ssl->conf->allow_legacy_renegotiation ==
7505 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7506 {
7507 /*
7508 * Accept renegotiation request
7509 */
Paul Bakker48916f92012-09-16 19:57:18 +00007510
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007511 /* DTLS clients need to know renego is server-initiated */
7512#if defined(MBEDTLS_SSL_PROTO_DTLS)
7513 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7514 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7515 {
7516 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7517 }
7518#endif
7519 ret = ssl_start_renegotiation( ssl );
7520 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7521 ret != 0 )
7522 {
7523 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7524 return( ret );
7525 }
7526 }
7527 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007528#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007529 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007530 /*
7531 * Refuse renegotiation
7532 */
7533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536#if defined(MBEDTLS_SSL_PROTO_SSL3)
7537 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007538 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007539 /* SSLv3 does not have a "no_renegotiation" warning, so
7540 we send a fatal alert and abort the connection. */
7541 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7542 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7543 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007544 }
7545 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7547#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7548 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7549 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7552 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7553 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007554 {
7555 return( ret );
7556 }
Paul Bakker48916f92012-09-16 19:57:18 +00007557 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007558 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7560 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007564 }
Paul Bakker48916f92012-09-16 19:57:18 +00007565 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007566
Hanno Becker90333da2017-10-10 11:27:13 +01007567 /* At this point, we don't know whether the renegotiation has been
7568 * completed or not. The cases to consider are the following:
7569 * 1) The renegotiation is complete. In this case, no new record
7570 * has been read yet.
7571 * 2) The renegotiation is incomplete because the client received
7572 * an application data record while awaiting the ServerHello.
7573 * 3) The renegotiation is incomplete because the client received
7574 * a non-handshake, non-application data message while awaiting
7575 * the ServerHello.
7576 * In each of these case, looping will be the proper action:
7577 * - For 1), the next iteration will read a new record and check
7578 * if it's application data.
7579 * - For 2), the loop condition isn't satisfied as application data
7580 * is present, hence continue is the same as break
7581 * - For 3), the loop condition is satisfied and read_record
7582 * will re-deliver the message that was held back by the client
7583 * when expecting the ServerHello.
7584 */
7585 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00007586 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007587#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007588 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007589 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007590 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007591 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007592 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007595 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007597 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007598 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007599 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7603 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007606 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007607 }
7608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7612 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007613 }
7614
7615 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007616
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007617 /* We're going to return something now, cancel timer,
7618 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007620 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007621
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007622#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007623 /* If we requested renego but received AppData, resend HelloRequest.
7624 * Do it now, after setting in_offt, to avoid taking this branch
7625 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007627 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007629 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007630 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007633 return( ret );
7634 }
7635 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007637#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007638 }
7639
7640 n = ( len < ssl->in_msglen )
7641 ? len : ssl->in_msglen;
7642
7643 memcpy( buf, ssl->in_offt, n );
7644 ssl->in_msglen -= n;
7645
7646 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007647 {
7648 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007649 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007650 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007651 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007652 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007653 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007654 /* more data available */
7655 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007656 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007659
Paul Bakker23986e52011-04-24 08:57:21 +00007660 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007661}
7662
7663/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007664 * Send application data to be encrypted by the SSL layer, taking care of max
7665 * fragment length and buffer size.
7666 *
7667 * According to RFC 5246 Section 6.2.1:
7668 *
7669 * Zero-length fragments of Application data MAY be sent as they are
7670 * potentially useful as a traffic analysis countermeasure.
7671 *
7672 * Therefore, it is possible that the input message length is 0 and the
7673 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007674 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007675static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007676 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007677{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007678 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
7679 const size_t max_len = (size_t) ret;
7680
7681 if( ret < 0 )
7682 {
7683 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
7684 return( ret );
7685 }
7686
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007687 if( len > max_len )
7688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007690 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007693 "maximum fragment length: %d > %d",
7694 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007696 }
7697 else
7698#endif
7699 len = max_len;
7700 }
Paul Bakker887bd502011-06-08 13:10:54 +00007701
Paul Bakker5121ce52009-01-03 21:22:43 +00007702 if( ssl->out_left != 0 )
7703 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007704 /*
7705 * The user has previously tried to send the data and
7706 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7707 * written. In this case, we expect the high-level write function
7708 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007713 return( ret );
7714 }
7715 }
Paul Bakker887bd502011-06-08 13:10:54 +00007716 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007717 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007718 /*
7719 * The user is trying to send a message the first time, so we need to
7720 * copy the data into the internal buffers and setup the data structure
7721 * to keep track of partial writes
7722 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007723 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007725 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007730 return( ret );
7731 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007732 }
7733
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007734 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007735}
7736
7737/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007738 * Write application data, doing 1/n-1 splitting if necessary.
7739 *
7740 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007741 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01007742 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007745static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007746 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007747{
7748 int ret;
7749
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007750 if( ssl->conf->cbc_record_splitting ==
7751 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007752 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007753 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7754 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7755 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007756 {
7757 return( ssl_write_real( ssl, buf, len ) );
7758 }
7759
7760 if( ssl->split_done == 0 )
7761 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007762 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007763 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007764 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007765 }
7766
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007767 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7768 return( ret );
7769 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007770
7771 return( ret + 1 );
7772}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007774
7775/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007776 * Write application data (public-facing wrapper)
7777 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007778int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007779{
7780 int ret;
7781
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007783
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007784 if( ssl == NULL || ssl->conf == NULL )
7785 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7786
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007787#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007788 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7789 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007790 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007791 return( ret );
7792 }
7793#endif
7794
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007795 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007796 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007797 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007798 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007800 return( ret );
7801 }
7802 }
7803
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007804#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007805 ret = ssl_write_split( ssl, buf, len );
7806#else
7807 ret = ssl_write_real( ssl, buf, len );
7808#endif
7809
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007811
7812 return( ret );
7813}
7814
7815/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007816 * Notify the peer that the connection is being closed
7817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007819{
7820 int ret;
7821
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007822 if( ssl == NULL || ssl->conf == NULL )
7823 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007826
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007827 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7833 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7834 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007837 return( ret );
7838 }
7839 }
7840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007842
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007843 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007844}
7845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00007847{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007848 if( transform == NULL )
7849 return;
7850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00007852 deflateEnd( &transform->ctx_deflate );
7853 inflateEnd( &transform->ctx_inflate );
7854#endif
7855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856 mbedtls_cipher_free( &transform->cipher_ctx_enc );
7857 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02007858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859 mbedtls_md_free( &transform->md_ctx_enc );
7860 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02007861
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007862 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007863}
7864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865#if defined(MBEDTLS_X509_CRT_PARSE_C)
7866static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007867{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007869
7870 while( cur != NULL )
7871 {
7872 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007874 cur = next;
7875 }
7876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007878
Gilles Peskine9b562d52018-04-25 20:32:43 +02007879void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007880{
Gilles Peskine9b562d52018-04-25 20:32:43 +02007881 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
7882
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007883 if( handshake == NULL )
7884 return;
7885
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007886#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
7887 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
7888 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02007889 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007890 handshake->async_in_progress = 0;
7891 }
7892#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
7893
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007894#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7895 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7896 mbedtls_md5_free( &handshake->fin_md5 );
7897 mbedtls_sha1_free( &handshake->fin_sha1 );
7898#endif
7899#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7900#if defined(MBEDTLS_SHA256_C)
7901 mbedtls_sha256_free( &handshake->fin_sha256 );
7902#endif
7903#if defined(MBEDTLS_SHA512_C)
7904 mbedtls_sha512_free( &handshake->fin_sha512 );
7905#endif
7906#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908#if defined(MBEDTLS_DHM_C)
7909 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007910#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911#if defined(MBEDTLS_ECDH_C)
7912 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007913#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007914#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007915 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007916#if defined(MBEDTLS_SSL_CLI_C)
7917 mbedtls_free( handshake->ecjpake_cache );
7918 handshake->ecjpake_cache = NULL;
7919 handshake->ecjpake_cache_len = 0;
7920#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007921#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02007922
Janos Follath4ae5c292016-02-10 11:27:43 +00007923#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
7924 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02007925 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007927#endif
7928
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007929#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7930 if( handshake->psk != NULL )
7931 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007932 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007933 mbedtls_free( handshake->psk );
7934 }
7935#endif
7936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7938 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007939 /*
7940 * Free only the linked list wrapper, not the keys themselves
7941 * since the belong to the SNI callback
7942 */
7943 if( handshake->sni_key_cert != NULL )
7944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007946
7947 while( cur != NULL )
7948 {
7949 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007951 cur = next;
7952 }
7953 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956#if defined(MBEDTLS_SSL_PROTO_DTLS)
7957 mbedtls_free( handshake->verify_cookie );
7958 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007959 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007960#endif
7961
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007962 mbedtls_platform_zeroize( handshake,
7963 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007964}
7965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007967{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007968 if( session == NULL )
7969 return;
7970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007972 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974 mbedtls_x509_crt_free( session->peer_cert );
7975 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007976 }
Paul Bakkered27a042013-04-18 22:46:23 +02007977#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007978
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007979#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007981#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007982
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007983 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007984}
7985
Paul Bakker5121ce52009-01-03 21:22:43 +00007986/*
7987 * Free an SSL context
7988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007990{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007991 if( ssl == NULL )
7992 return;
7993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007995
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007996 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007997 {
Angus Grattond8213d02016-05-25 20:56:48 +10007998 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008000 }
8001
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008002 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008003 {
Angus Grattond8213d02016-05-25 20:56:48 +10008004 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008006 }
8007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008008#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008009 if( ssl->compress_buf != NULL )
8010 {
Angus Grattond8213d02016-05-25 20:56:48 +10008011 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008013 }
8014#endif
8015
Paul Bakker48916f92012-09-16 19:57:18 +00008016 if( ssl->transform )
8017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018 mbedtls_ssl_transform_free( ssl->transform );
8019 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008020 }
8021
8022 if( ssl->handshake )
8023 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008024 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8026 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008028 mbedtls_free( ssl->handshake );
8029 mbedtls_free( ssl->transform_negotiate );
8030 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008031 }
8032
Paul Bakkerc0463502013-02-14 11:19:38 +01008033 if( ssl->session )
8034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035 mbedtls_ssl_session_free( ssl->session );
8036 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008037 }
8038
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008039#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008040 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008041 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008042 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008043 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008044 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008045#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008047#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8048 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8051 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008052 }
8053#endif
8054
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008055#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008057#endif
8058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008060
Paul Bakker86f04f42013-02-14 11:20:09 +01008061 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008062 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008063}
8064
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008065/*
8066 * Initialze mbedtls_ssl_config
8067 */
8068void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8069{
8070 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8071}
8072
Simon Butcherc97b6972015-12-27 23:48:17 +00008073#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008074static int ssl_preset_default_hashes[] = {
8075#if defined(MBEDTLS_SHA512_C)
8076 MBEDTLS_MD_SHA512,
8077 MBEDTLS_MD_SHA384,
8078#endif
8079#if defined(MBEDTLS_SHA256_C)
8080 MBEDTLS_MD_SHA256,
8081 MBEDTLS_MD_SHA224,
8082#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008083#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008084 MBEDTLS_MD_SHA1,
8085#endif
8086 MBEDTLS_MD_NONE
8087};
Simon Butcherc97b6972015-12-27 23:48:17 +00008088#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008089
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008090static int ssl_preset_suiteb_ciphersuites[] = {
8091 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8092 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8093 0
8094};
8095
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008096#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008097static int ssl_preset_suiteb_hashes[] = {
8098 MBEDTLS_MD_SHA256,
8099 MBEDTLS_MD_SHA384,
8100 MBEDTLS_MD_NONE
8101};
8102#endif
8103
8104#if defined(MBEDTLS_ECP_C)
8105static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8106 MBEDTLS_ECP_DP_SECP256R1,
8107 MBEDTLS_ECP_DP_SECP384R1,
8108 MBEDTLS_ECP_DP_NONE
8109};
8110#endif
8111
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008112/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008113 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008114 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008115int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008116 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008117{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008118#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008119 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008120#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008121
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008122 /* Use the functions here so that they are covered in tests,
8123 * but otherwise access member directly for efficiency */
8124 mbedtls_ssl_conf_endpoint( conf, endpoint );
8125 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008126
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008127 /*
8128 * Things that are common to all presets
8129 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008130#if defined(MBEDTLS_SSL_CLI_C)
8131 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8132 {
8133 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8134#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8135 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8136#endif
8137 }
8138#endif
8139
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008140#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008141 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008142#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008143
8144#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8145 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8146#endif
8147
8148#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8149 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8150#endif
8151
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008152#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8153 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8154#endif
8155
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008156#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008157 conf->f_cookie_write = ssl_cookie_write_dummy;
8158 conf->f_cookie_check = ssl_cookie_check_dummy;
8159#endif
8160
8161#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8162 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8163#endif
8164
Janos Follath088ce432017-04-10 12:42:31 +01008165#if defined(MBEDTLS_SSL_SRV_C)
8166 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8167#endif
8168
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008169#if defined(MBEDTLS_SSL_PROTO_DTLS)
8170 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8171 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8172#endif
8173
8174#if defined(MBEDTLS_SSL_RENEGOTIATION)
8175 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008176 memset( conf->renego_period, 0x00, 2 );
8177 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008178#endif
8179
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008180#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8181 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8182 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008183 const unsigned char dhm_p[] =
8184 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8185 const unsigned char dhm_g[] =
8186 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8187
Hanno Beckera90658f2017-10-04 15:29:08 +01008188 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8189 dhm_p, sizeof( dhm_p ),
8190 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008191 {
8192 return( ret );
8193 }
8194 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008195#endif
8196
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008197 /*
8198 * Preset-specific defaults
8199 */
8200 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008201 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008202 /*
8203 * NSA Suite B
8204 */
8205 case MBEDTLS_SSL_PRESET_SUITEB:
8206 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8207 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8208 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8209 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8210
8211 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8212 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8213 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8214 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8215 ssl_preset_suiteb_ciphersuites;
8216
8217#if defined(MBEDTLS_X509_CRT_PARSE_C)
8218 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008219#endif
8220
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008221#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008222 conf->sig_hashes = ssl_preset_suiteb_hashes;
8223#endif
8224
8225#if defined(MBEDTLS_ECP_C)
8226 conf->curve_list = ssl_preset_suiteb_curves;
8227#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008228 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008229
8230 /*
8231 * Default
8232 */
8233 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008234 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8235 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8236 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8237 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8238 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8239 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8240 MBEDTLS_SSL_MIN_MINOR_VERSION :
8241 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008242 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8243 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8244
8245#if defined(MBEDTLS_SSL_PROTO_DTLS)
8246 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8247 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8248#endif
8249
8250 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8251 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8252 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8253 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8254 mbedtls_ssl_list_ciphersuites();
8255
8256#if defined(MBEDTLS_X509_CRT_PARSE_C)
8257 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8258#endif
8259
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008260#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008261 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008262#endif
8263
8264#if defined(MBEDTLS_ECP_C)
8265 conf->curve_list = mbedtls_ecp_grp_id_list();
8266#endif
8267
8268#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8269 conf->dhm_min_bitlen = 1024;
8270#endif
8271 }
8272
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008273 return( 0 );
8274}
8275
8276/*
8277 * Free mbedtls_ssl_config
8278 */
8279void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8280{
8281#if defined(MBEDTLS_DHM_C)
8282 mbedtls_mpi_free( &conf->dhm_P );
8283 mbedtls_mpi_free( &conf->dhm_G );
8284#endif
8285
8286#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8287 if( conf->psk != NULL )
8288 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008289 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008290 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00008291 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008292 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09008293 }
8294
8295 if( conf->psk_identity != NULL )
8296 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008297 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09008298 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00008299 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008300 conf->psk_identity_len = 0;
8301 }
8302#endif
8303
8304#if defined(MBEDTLS_X509_CRT_PARSE_C)
8305 ssl_key_cert_free( conf->key_cert );
8306#endif
8307
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008308 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008309}
8310
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008311#if defined(MBEDTLS_PK_C) && \
8312 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008313/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008314 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008317{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318#if defined(MBEDTLS_RSA_C)
8319 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8320 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008321#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322#if defined(MBEDTLS_ECDSA_C)
8323 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8324 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008325#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008327}
8328
Hanno Becker7e5437a2017-04-28 17:15:26 +01008329unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8330{
8331 switch( type ) {
8332 case MBEDTLS_PK_RSA:
8333 return( MBEDTLS_SSL_SIG_RSA );
8334 case MBEDTLS_PK_ECDSA:
8335 case MBEDTLS_PK_ECKEY:
8336 return( MBEDTLS_SSL_SIG_ECDSA );
8337 default:
8338 return( MBEDTLS_SSL_SIG_ANON );
8339 }
8340}
8341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008343{
8344 switch( sig )
8345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346#if defined(MBEDTLS_RSA_C)
8347 case MBEDTLS_SSL_SIG_RSA:
8348 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008349#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350#if defined(MBEDTLS_ECDSA_C)
8351 case MBEDTLS_SSL_SIG_ECDSA:
8352 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008353#endif
8354 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008356 }
8357}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008358#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008359
Hanno Becker7e5437a2017-04-28 17:15:26 +01008360#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8361 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8362
8363/* Find an entry in a signature-hash set matching a given hash algorithm. */
8364mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8365 mbedtls_pk_type_t sig_alg )
8366{
8367 switch( sig_alg )
8368 {
8369 case MBEDTLS_PK_RSA:
8370 return( set->rsa );
8371 case MBEDTLS_PK_ECDSA:
8372 return( set->ecdsa );
8373 default:
8374 return( MBEDTLS_MD_NONE );
8375 }
8376}
8377
8378/* Add a signature-hash-pair to a signature-hash set */
8379void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8380 mbedtls_pk_type_t sig_alg,
8381 mbedtls_md_type_t md_alg )
8382{
8383 switch( sig_alg )
8384 {
8385 case MBEDTLS_PK_RSA:
8386 if( set->rsa == MBEDTLS_MD_NONE )
8387 set->rsa = md_alg;
8388 break;
8389
8390 case MBEDTLS_PK_ECDSA:
8391 if( set->ecdsa == MBEDTLS_MD_NONE )
8392 set->ecdsa = md_alg;
8393 break;
8394
8395 default:
8396 break;
8397 }
8398}
8399
8400/* Allow exactly one hash algorithm for each signature. */
8401void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8402 mbedtls_md_type_t md_alg )
8403{
8404 set->rsa = md_alg;
8405 set->ecdsa = md_alg;
8406}
8407
8408#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8409 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8410
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008411/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008412 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008415{
8416 switch( hash )
8417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418#if defined(MBEDTLS_MD5_C)
8419 case MBEDTLS_SSL_HASH_MD5:
8420 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008421#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422#if defined(MBEDTLS_SHA1_C)
8423 case MBEDTLS_SSL_HASH_SHA1:
8424 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008425#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008426#if defined(MBEDTLS_SHA256_C)
8427 case MBEDTLS_SSL_HASH_SHA224:
8428 return( MBEDTLS_MD_SHA224 );
8429 case MBEDTLS_SSL_HASH_SHA256:
8430 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008431#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008432#if defined(MBEDTLS_SHA512_C)
8433 case MBEDTLS_SSL_HASH_SHA384:
8434 return( MBEDTLS_MD_SHA384 );
8435 case MBEDTLS_SSL_HASH_SHA512:
8436 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008437#endif
8438 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008439 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008440 }
8441}
8442
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008443/*
8444 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8445 */
8446unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8447{
8448 switch( md )
8449 {
8450#if defined(MBEDTLS_MD5_C)
8451 case MBEDTLS_MD_MD5:
8452 return( MBEDTLS_SSL_HASH_MD5 );
8453#endif
8454#if defined(MBEDTLS_SHA1_C)
8455 case MBEDTLS_MD_SHA1:
8456 return( MBEDTLS_SSL_HASH_SHA1 );
8457#endif
8458#if defined(MBEDTLS_SHA256_C)
8459 case MBEDTLS_MD_SHA224:
8460 return( MBEDTLS_SSL_HASH_SHA224 );
8461 case MBEDTLS_MD_SHA256:
8462 return( MBEDTLS_SSL_HASH_SHA256 );
8463#endif
8464#if defined(MBEDTLS_SHA512_C)
8465 case MBEDTLS_MD_SHA384:
8466 return( MBEDTLS_SSL_HASH_SHA384 );
8467 case MBEDTLS_MD_SHA512:
8468 return( MBEDTLS_SSL_HASH_SHA512 );
8469#endif
8470 default:
8471 return( MBEDTLS_SSL_HASH_NONE );
8472 }
8473}
8474
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008475#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008476/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008477 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008478 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008479 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008480int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008481{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008483
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008484 if( ssl->conf->curve_list == NULL )
8485 return( -1 );
8486
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008487 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008488 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008489 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008490
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008491 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008492}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008493#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008494
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008495#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008496/*
8497 * Check if a hash proposed by the peer is in our list.
8498 * Return 0 if we're willing to use it, -1 otherwise.
8499 */
8500int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8501 mbedtls_md_type_t md )
8502{
8503 const int *cur;
8504
8505 if( ssl->conf->sig_hashes == NULL )
8506 return( -1 );
8507
8508 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8509 if( *cur == (int) md )
8510 return( 0 );
8511
8512 return( -1 );
8513}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008514#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008516#if defined(MBEDTLS_X509_CRT_PARSE_C)
8517int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8518 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008519 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008520 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008521{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008522 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008523#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008524 int usage = 0;
8525#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008526#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008527 const char *ext_oid;
8528 size_t ext_len;
8529#endif
8530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008531#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8532 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008533 ((void) cert);
8534 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008535 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008536#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008538#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8539 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008540 {
8541 /* Server part of the key exchange */
8542 switch( ciphersuite->key_exchange )
8543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008544 case MBEDTLS_KEY_EXCHANGE_RSA:
8545 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008546 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008547 break;
8548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008549 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8550 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8551 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8552 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008553 break;
8554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008555 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8556 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008557 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008558 break;
8559
8560 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561 case MBEDTLS_KEY_EXCHANGE_NONE:
8562 case MBEDTLS_KEY_EXCHANGE_PSK:
8563 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8564 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008565 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008566 usage = 0;
8567 }
8568 }
8569 else
8570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008571 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8572 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008573 }
8574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008575 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008576 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008577 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008578 ret = -1;
8579 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008580#else
8581 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008584#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8585 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8588 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008589 }
8590 else
8591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008592 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8593 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008594 }
8595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008597 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008598 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008599 ret = -1;
8600 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008601#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008602
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008603 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008604}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008606
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008607/*
8608 * Convert version numbers to/from wire format
8609 * and, for DTLS, to/from TLS equivalent.
8610 *
8611 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008612 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008613 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8614 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008617 unsigned char ver[2] )
8618{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619#if defined(MBEDTLS_SSL_PROTO_DTLS)
8620 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008622 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008623 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8624
8625 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8626 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8627 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008628 else
8629#else
8630 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008631#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008632 {
8633 ver[0] = (unsigned char) major;
8634 ver[1] = (unsigned char) minor;
8635 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008636}
8637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008639 const unsigned char ver[2] )
8640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641#if defined(MBEDTLS_SSL_PROTO_DTLS)
8642 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008643 {
8644 *major = 255 - ver[0] + 2;
8645 *minor = 255 - ver[1] + 1;
8646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008647 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008648 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8649 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008650 else
8651#else
8652 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008653#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008654 {
8655 *major = ver[0];
8656 *minor = ver[1];
8657 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008658}
8659
Simon Butcher99000142016-10-13 17:21:01 +01008660int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8661{
8662#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8663 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8664 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8665
8666 switch( md )
8667 {
8668#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8669#if defined(MBEDTLS_MD5_C)
8670 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008671 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008672#endif
8673#if defined(MBEDTLS_SHA1_C)
8674 case MBEDTLS_SSL_HASH_SHA1:
8675 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8676 break;
8677#endif
8678#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8679#if defined(MBEDTLS_SHA512_C)
8680 case MBEDTLS_SSL_HASH_SHA384:
8681 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8682 break;
8683#endif
8684#if defined(MBEDTLS_SHA256_C)
8685 case MBEDTLS_SSL_HASH_SHA256:
8686 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8687 break;
8688#endif
8689 default:
8690 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8691 }
8692
8693 return 0;
8694#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8695 (void) ssl;
8696 (void) md;
8697
8698 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8699#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8700}
8701
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008702#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8703 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8704int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8705 unsigned char *output,
8706 unsigned char *data, size_t data_len )
8707{
8708 int ret = 0;
8709 mbedtls_md5_context mbedtls_md5;
8710 mbedtls_sha1_context mbedtls_sha1;
8711
8712 mbedtls_md5_init( &mbedtls_md5 );
8713 mbedtls_sha1_init( &mbedtls_sha1 );
8714
8715 /*
8716 * digitally-signed struct {
8717 * opaque md5_hash[16];
8718 * opaque sha_hash[20];
8719 * };
8720 *
8721 * md5_hash
8722 * MD5(ClientHello.random + ServerHello.random
8723 * + ServerParams);
8724 * sha_hash
8725 * SHA(ClientHello.random + ServerHello.random
8726 * + ServerParams);
8727 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008728 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008729 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008730 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008731 goto exit;
8732 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008733 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008734 ssl->handshake->randbytes, 64 ) ) != 0 )
8735 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008736 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008737 goto exit;
8738 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008739 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008740 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008741 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008742 goto exit;
8743 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008744 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008745 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008746 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008747 goto exit;
8748 }
8749
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008750 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008751 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008753 goto exit;
8754 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008755 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008756 ssl->handshake->randbytes, 64 ) ) != 0 )
8757 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008758 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008759 goto exit;
8760 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008761 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008762 data_len ) ) != 0 )
8763 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008764 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008765 goto exit;
8766 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008767 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008768 output + 16 ) ) != 0 )
8769 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008770 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008771 goto exit;
8772 }
8773
8774exit:
8775 mbedtls_md5_free( &mbedtls_md5 );
8776 mbedtls_sha1_free( &mbedtls_sha1 );
8777
8778 if( ret != 0 )
8779 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8780 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8781
8782 return( ret );
8783
8784}
8785#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
8786 MBEDTLS_SSL_PROTO_TLS1_1 */
8787
8788#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8789 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8790int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02008791 unsigned char *hash, size_t *hashlen,
8792 unsigned char *data, size_t data_len,
8793 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008794{
8795 int ret = 0;
8796 mbedtls_md_context_t ctx;
8797 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02008798 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008799
8800 mbedtls_md_init( &ctx );
8801
8802 /*
8803 * digitally-signed struct {
8804 * opaque client_random[32];
8805 * opaque server_random[32];
8806 * ServerDHParams params;
8807 * };
8808 */
8809 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8810 {
8811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8812 goto exit;
8813 }
8814 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8815 {
8816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8817 goto exit;
8818 }
8819 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8820 {
8821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8822 goto exit;
8823 }
8824 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8825 {
8826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8827 goto exit;
8828 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02008829 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008830 {
8831 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8832 goto exit;
8833 }
8834
8835exit:
8836 mbedtls_md_free( &ctx );
8837
8838 if( ret != 0 )
8839 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8840 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8841
8842 return( ret );
8843}
8844#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
8845 MBEDTLS_SSL_PROTO_TLS1_2 */
8846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847#endif /* MBEDTLS_SSL_TLS_C */