blob: 30c1a78f2ae5f0101a2017f2ab661c9bea151f5e [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
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200111 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
112 * in the following way: after the initial transmission and a first
113 * retransmission, back off to a temporary estimated MTU of 508 bytes.
114 * This value is guaranteed to be deliverable (if not guaranteed to be
115 * delivered) of any compliant IPv4 (and IPv6) network, and should work
116 * on most non-IP stacks too. */
117 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
118 ssl->handshake->mtu = 508;
119
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200120 new_timeout = 2 * ssl->handshake->retransmit_timeout;
121
122 /* Avoid arithmetic overflow and range overflow */
123 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200124 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200125 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200126 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200127 }
128
129 ssl->handshake->retransmit_timeout = new_timeout;
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
133 return( 0 );
134}
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200137{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200138 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200140 ssl->handshake->retransmit_timeout ) );
141}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200145/*
146 * Convert max_fragment_length codes to length.
147 * RFC 6066 says:
148 * enum{
149 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
150 * } MaxFragmentLength;
151 * and we add 0 -> extension unused
152 */
Angus Grattond8213d02016-05-25 20:56:48 +1000153static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200154{
Angus Grattond8213d02016-05-25 20:56:48 +1000155 switch( mfl )
156 {
157 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
158 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
159 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
160 return 512;
161 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
162 return 1024;
163 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
164 return 2048;
165 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
166 return 4096;
167 default:
168 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
169 }
170}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200172
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200173#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200175{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_ssl_session_free( dst );
177 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200180 if( src->peer_cert != NULL )
181 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200182 int ret;
183
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200184 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200185 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200186 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200191 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200194 dst->peer_cert = NULL;
195 return( ret );
196 }
197 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200199
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200200#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200201 if( src->ticket != NULL )
202 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200203 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200204 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200205 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200206
207 memcpy( dst->ticket, src->ticket, src->ticket_len );
208 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200209#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200210
211 return( 0 );
212}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200213#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
216int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200217 const unsigned char *key_enc, const unsigned char *key_dec,
218 size_t keylen,
219 const unsigned char *iv_enc, const unsigned char *iv_dec,
220 size_t ivlen,
221 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200222 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
224int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
225int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
226int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
227int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
228#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000229
Paul Bakker5121ce52009-01-03 21:22:43 +0000230/*
231 * Key material generation
232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200234static int ssl3_prf( const unsigned char *secret, size_t slen,
235 const char *label,
236 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000237 unsigned char *dstbuf, size_t dlen )
238{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100239 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000240 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200241 mbedtls_md5_context md5;
242 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000243 unsigned char padding[16];
244 unsigned char sha1sum[20];
245 ((void)label);
246
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200247 mbedtls_md5_init( &md5 );
248 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200249
Paul Bakker5f70b252012-09-13 14:23:06 +0000250 /*
251 * SSLv3:
252 * block =
253 * MD5( secret + SHA1( 'A' + secret + random ) ) +
254 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
255 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
256 * ...
257 */
258 for( i = 0; i < dlen / 16; i++ )
259 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200260 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000261
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100262 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100263 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100264 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100265 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100266 if( ( ret = mbedtls_sha1_update_ret( &sha1, 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_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100269 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100270 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100271 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000272
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100273 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100274 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100275 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100276 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100277 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100278 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100279 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100280 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000281 }
282
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100283exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200284 mbedtls_md5_free( &md5 );
285 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000286
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500287 mbedtls_platform_zeroize( padding, sizeof( padding ) );
288 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000289
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100290 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000291}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200295static int tls1_prf( const unsigned char *secret, size_t slen,
296 const char *label,
297 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000298 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000299{
Paul Bakker23986e52011-04-24 08:57:21 +0000300 size_t nb, hs;
301 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200302 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 unsigned char tmp[128];
304 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 const mbedtls_md_info_t *md_info;
306 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100307 int ret;
308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
311 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000313
314 hs = ( slen + 1 ) / 2;
315 S1 = secret;
316 S2 = secret + slen - hs;
317
318 nb = strlen( label );
319 memcpy( tmp + 20, label, nb );
320 memcpy( tmp + 20 + nb, random, rlen );
321 nb += rlen;
322
323 /*
324 * First compute P_md5(secret,label+random)[0..dlen]
325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
327 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100330 return( ret );
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
333 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
334 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
336 for( i = 0; i < dlen; i += 16 )
337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_md_hmac_reset ( &md_ctx );
339 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
340 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_md_hmac_reset ( &md_ctx );
343 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
344 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
346 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
347
348 for( j = 0; j < k; j++ )
349 dstbuf[i + j] = h_i[j];
350 }
351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100353
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 /*
355 * XOR out with P_sha1(secret,label+random)[0..dlen]
356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
358 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100361 return( ret );
362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
364 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
365 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000366
367 for( i = 0; i < dlen; i += 20 )
368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_md_hmac_reset ( &md_ctx );
370 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
371 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_md_hmac_reset ( &md_ctx );
374 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
375 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376
377 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
378
379 for( j = 0; j < k; j++ )
380 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
381 }
382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100384
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500385 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
386 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
388 return( 0 );
389}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
393static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100394 const unsigned char *secret, size_t slen,
395 const char *label,
396 const unsigned char *random, size_t rlen,
397 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000398{
399 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100400 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000401 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
403 const mbedtls_md_info_t *md_info;
404 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100405 int ret;
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
410 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100413
414 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000416
417 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100418 memcpy( tmp + md_len, label, nb );
419 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000420 nb += rlen;
421
422 /*
423 * Compute P_<hash>(secret, label + random)[0..dlen]
424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100426 return( ret );
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
429 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
430 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100431
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100432 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_md_hmac_reset ( &md_ctx );
435 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
436 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_md_hmac_reset ( &md_ctx );
439 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
440 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000441
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100442 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000443
444 for( j = 0; j < k; j++ )
445 dstbuf[i + j] = h_i[j];
446 }
447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100449
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500450 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
451 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000452
453 return( 0 );
454}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100457static int tls_prf_sha256( const unsigned char *secret, size_t slen,
458 const char *label,
459 const unsigned char *random, size_t rlen,
460 unsigned char *dstbuf, size_t dlen )
461{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100463 label, random, rlen, dstbuf, dlen ) );
464}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200468static int tls_prf_sha384( const unsigned char *secret, size_t slen,
469 const char *label,
470 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000471 unsigned char *dstbuf, size_t dlen )
472{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100474 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000475}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476#endif /* MBEDTLS_SHA512_C */
477#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
482 defined(MBEDTLS_SSL_PROTO_TLS1_1)
483static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200484#endif
Paul Bakker380da532012-04-18 16:10:25 +0000485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_SSL_PROTO_SSL3)
487static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
488static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200489#endif
490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
492static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
493static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200494#endif
495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
497#if defined(MBEDTLS_SHA256_C)
498static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
499static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
500static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200501#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#if defined(MBEDTLS_SHA512_C)
504static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
505static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
506static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100507#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000511{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200512 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 unsigned char keyblk[256];
515 unsigned char *key1;
516 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100517 unsigned char *mac_enc;
518 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000519 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200520 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 const mbedtls_cipher_info_t *cipher_info;
522 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_ssl_session *session = ssl->session_negotiate;
525 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
526 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100531 if( cipher_info == NULL )
532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100534 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100536 }
537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100539 if( md_info == NULL )
540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100542 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100544 }
545
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000547 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549#if defined(MBEDTLS_SSL_PROTO_SSL3)
550 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000551 {
Paul Bakker48916f92012-09-16 19:57:18 +0000552 handshake->tls_prf = ssl3_prf;
553 handshake->calc_verify = ssl_calc_verify_ssl;
554 handshake->calc_finished = ssl_calc_finished_ssl;
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) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
559 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000560 {
Paul Bakker48916f92012-09-16 19:57:18 +0000561 handshake->tls_prf = tls1_prf;
562 handshake->calc_verify = ssl_calc_verify_tls;
563 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000564 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200565 else
566#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
568#if defined(MBEDTLS_SHA512_C)
569 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
570 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000571 {
Paul Bakker48916f92012-09-16 19:57:18 +0000572 handshake->tls_prf = tls_prf_sha384;
573 handshake->calc_verify = ssl_calc_verify_tls_sha384;
574 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000575 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000576 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200577#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578#if defined(MBEDTLS_SHA256_C)
579 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000580 {
Paul Bakker48916f92012-09-16 19:57:18 +0000581 handshake->tls_prf = tls_prf_sha256;
582 handshake->calc_verify = ssl_calc_verify_tls_sha256;
583 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000584 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200585 else
586#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
590 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200591 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000592
593 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 * SSLv3:
595 * master =
596 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
597 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
598 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200599 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200600 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000601 * master = PRF( premaster, "master secret", randbytes )[0..47]
602 */
Paul Bakker0a597072012-09-25 21:55:46 +0000603 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000606 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
609 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200610 {
611 unsigned char session_hash[48];
612 size_t hash_len;
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200615
616 ssl->handshake->calc_verify( ssl, session_hash );
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
619 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200622 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200624 {
625 hash_len = 48;
626 }
627 else
628#endif
629 hash_len = 32;
630 }
631 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200633 hash_len = 36;
634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200636
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100637 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
638 "extended master secret",
639 session_hash, hash_len,
640 session->master, 48 );
641 if( ret != 0 )
642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100644 return( ret );
645 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200646
647 }
648 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200649#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100650 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
651 "master secret",
652 handshake->randbytes, 64,
653 session->master, 48 );
654 if( ret != 0 )
655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100657 return( ret );
658 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200659
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500660 mbedtls_platform_zeroize( handshake->premaster,
661 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 }
663 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000665
666 /*
667 * Swap the client and server random values.
668 */
Paul Bakker48916f92012-09-16 19:57:18 +0000669 memcpy( tmp, handshake->randbytes, 64 );
670 memcpy( handshake->randbytes, tmp + 32, 32 );
671 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500672 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
674 /*
675 * SSLv3:
676 * key block =
677 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
678 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
679 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
680 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
681 * ...
682 *
683 * TLSv1:
684 * key block = PRF( master, "key expansion", randbytes )
685 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100686 ret = handshake->tls_prf( session->master, 48, "key expansion",
687 handshake->randbytes, 64, keyblk, 256 );
688 if( ret != 0 )
689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100691 return( ret );
692 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
695 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
696 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
697 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
698 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000699
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500700 mbedtls_platform_zeroize( handshake->randbytes,
701 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000702
703 /*
704 * Determine the appropriate key, IV and MAC length.
705 */
Paul Bakker68884e32013-01-07 18:20:04 +0100706
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200707 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200710 cipher_info->mode == MBEDTLS_MODE_CCM ||
711 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200713 size_t taglen, explicit_ivlen;
714
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200715 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000716 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200717
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200718 /* All modes haves 96-bit IVs;
719 * GCM and CCM has 4 implicit and 8 explicit bytes
720 * ChachaPoly has all 12 bytes implicit
721 */
Paul Bakker68884e32013-01-07 18:20:04 +0100722 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200723 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
724 transform->fixed_ivlen = 12;
725 else
726 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200727
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200728 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
729 taglen = transform->ciphersuite_info->flags &
730 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
731
732
733 /* Minimum length of encrypted record */
734 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
735 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100736 }
737 else
738 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200739 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
741 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200744 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100745 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000746
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200747 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000748 mac_key_len = mbedtls_md_get_size( md_info );
749 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200752 /*
753 * If HMAC is to be truncated, we shall keep the leftmost bytes,
754 * (rfc 6066 page 13 or rfc 2104 section 4),
755 * so we only need to adjust the length here.
756 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000760
761#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
762 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000763 * HMAC implementation which also truncates the key
764 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000765 mac_key_len = transform->maclen;
766#endif
767 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200769
770 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100771 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200773 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200775 transform->minlen = transform->maclen;
776 else
Paul Bakker68884e32013-01-07 18:20:04 +0100777 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200778 /*
779 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100780 * 1. if EtM is in use: one block plus MAC
781 * otherwise: * first multiple of blocklen greater than maclen
782 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
785 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100786 {
787 transform->minlen = transform->maclen
788 + cipher_info->block_size;
789 }
790 else
791#endif
792 {
793 transform->minlen = transform->maclen
794 + cipher_info->block_size
795 - transform->maclen % cipher_info->block_size;
796 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
799 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
800 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200801 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100802 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200803#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
805 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
806 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200807 {
808 transform->minlen += transform->ivlen;
809 }
810 else
811#endif
812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
814 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200815 }
Paul Bakker68884e32013-01-07 18:20:04 +0100816 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000817 }
818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000820 transform->keylen, transform->minlen, transform->ivlen,
821 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000822
823 /*
824 * Finally setup the cipher contexts, IVs and MAC secrets.
825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200827 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000828 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000829 key1 = keyblk + mac_key_len * 2;
830 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000831
Paul Bakker68884e32013-01-07 18:20:04 +0100832 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000833 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000834
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000835 /*
836 * This is not used in TLS v1.1.
837 */
Paul Bakker48916f92012-09-16 19:57:18 +0000838 iv_copy_len = ( transform->fixed_ivlen ) ?
839 transform->fixed_ivlen : transform->ivlen;
840 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
841 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000842 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000843 }
844 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#endif /* MBEDTLS_SSL_CLI_C */
846#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200847 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000848 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000849 key1 = keyblk + mac_key_len * 2 + transform->keylen;
850 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000851
Hanno Becker81c7b182017-11-09 18:39:33 +0000852 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100853 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000854
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000855 /*
856 * This is not used in TLS v1.1.
857 */
Paul Bakker48916f92012-09-16 19:57:18 +0000858 iv_copy_len = ( transform->fixed_ivlen ) ?
859 transform->fixed_ivlen : transform->ivlen;
860 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
861 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000862 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000863 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100864 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +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é-Gonnardd16d1cb2014-11-20 18:15:05 +0100869 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#if defined(MBEDTLS_SSL_PROTO_SSL3)
872 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100873 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000874 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
877 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100878 }
879
Hanno Becker81c7b182017-11-09 18:39:33 +0000880 memcpy( transform->mac_enc, mac_enc, mac_key_len );
881 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100882 }
883 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884#endif /* MBEDTLS_SSL_PROTO_SSL3 */
885#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
886 defined(MBEDTLS_SSL_PROTO_TLS1_2)
887 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100888 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100889 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
890 For AEAD-based ciphersuites, there is nothing to do here. */
891 if( mac_key_len != 0 )
892 {
893 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
894 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
895 }
Paul Bakker68884e32013-01-07 18:20:04 +0100896 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200897 else
898#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
901 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200902 }
Paul Bakker68884e32013-01-07 18:20:04 +0100903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
905 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000906 {
907 int ret = 0;
908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100912 transform->iv_enc, transform->iv_dec,
913 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100914 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +0000915 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
918 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000919 }
920 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000922
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200923#if defined(MBEDTLS_SSL_EXPORT_KEYS)
924 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100925 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200926 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
927 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +0000928 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100929 iv_copy_len );
930 }
931#endif
932
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200933 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200934 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000935 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200936 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200937 return( ret );
938 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200939
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200940 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200941 cipher_info ) ) != 0 )
942 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200943 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200944 return( ret );
945 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200948 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200952 return( ret );
953 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200956 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200960 return( ret );
961 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963#if defined(MBEDTLS_CIPHER_MODE_CBC)
964 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
967 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200970 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200971 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
974 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200977 return( ret );
978 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000979 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500982 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +0000985 // Initialize compression
986 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000988 {
Paul Bakker16770332013-10-11 09:59:44 +0200989 if( ssl->compress_buf == NULL )
990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +1000992 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +0200993 if( ssl->compress_buf == NULL )
994 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +1000996 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200997 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +0200998 }
999 }
1000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001002
Paul Bakker48916f92012-09-16 19:57:18 +00001003 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1004 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001005
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001006 if( deflateInit( &transform->ctx_deflate,
1007 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001008 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1011 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001012 }
1013 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
1018 return( 0 );
1019}
1020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#if defined(MBEDTLS_SSL_PROTO_SSL3)
1022void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001023{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001024 mbedtls_md5_context md5;
1025 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001026 unsigned char pad_1[48];
1027 unsigned char pad_2[48];
1028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001031 mbedtls_md5_init( &md5 );
1032 mbedtls_sha1_init( &sha1 );
1033
1034 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1035 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
Paul Bakker380da532012-04-18 16:10:25 +00001037 memset( pad_1, 0x36, 48 );
1038 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001039
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001040 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1041 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1042 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001043
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001044 mbedtls_md5_starts_ret( &md5 );
1045 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1046 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1047 mbedtls_md5_update_ret( &md5, hash, 16 );
1048 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001050 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1051 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1052 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001053
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001054 mbedtls_sha1_starts_ret( &sha1 );
1055 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1056 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1057 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1058 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001062
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001063 mbedtls_md5_free( &md5 );
1064 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001065
Paul Bakker380da532012-04-18 16:10:25 +00001066 return;
1067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1071void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001072{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001073 mbedtls_md5_context md5;
1074 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001077
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001078 mbedtls_md5_init( &md5 );
1079 mbedtls_sha1_init( &sha1 );
1080
1081 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1082 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001083
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001084 mbedtls_md5_finish_ret( &md5, hash );
1085 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001089
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001090 mbedtls_md5_free( &md5 );
1091 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001092
Paul Bakker380da532012-04-18 16:10:25 +00001093 return;
1094}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1098#if defined(MBEDTLS_SHA256_C)
1099void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001100{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001101 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001102
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001103 mbedtls_sha256_init( &sha256 );
1104
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001106
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001107 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001108 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001112
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001113 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001114
Paul Bakker380da532012-04-18 16:10:25 +00001115 return;
1116}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119#if defined(MBEDTLS_SHA512_C)
1120void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001121{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001122 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001123
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001124 mbedtls_sha512_init( &sha512 );
1125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001127
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001128 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001129 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001133
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001134 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001135
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 return;
1137}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#endif /* MBEDTLS_SHA512_C */
1139#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1142int 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 +02001143{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001144 unsigned char *p = ssl->handshake->premaster;
1145 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001146 const unsigned char *psk = ssl->conf->psk;
1147 size_t psk_len = ssl->conf->psk_len;
1148
1149 /* If the psk callback was called, use its result */
1150 if( ssl->handshake->psk != NULL )
1151 {
1152 psk = ssl->handshake->psk;
1153 psk_len = ssl->handshake->psk_len;
1154 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001155
1156 /*
1157 * PMS = struct {
1158 * opaque other_secret<0..2^16-1>;
1159 * opaque psk<0..2^16-1>;
1160 * };
1161 * with "other_secret" depending on the particular key exchange
1162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1164 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001165 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001166 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001168
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001169 *(p++) = (unsigned char)( psk_len >> 8 );
1170 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001171
1172 if( end < p || (size_t)( end - p ) < psk_len )
1173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1174
1175 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001176 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001177 }
1178 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1180#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1181 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001182 {
1183 /*
1184 * other_secret already set by the ClientKeyExchange message,
1185 * and is 48 bytes long
1186 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001187 if( end - p < 2 )
1188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1189
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001190 *p++ = 0;
1191 *p++ = 48;
1192 p += 48;
1193 }
1194 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1196#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1197 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001198 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001199 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001200 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001201
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001202 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001204 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001205 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001208 return( ret );
1209 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001210 *(p++) = (unsigned char)( len >> 8 );
1211 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001212 p += len;
1213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001215 }
1216 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1218#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1219 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001220 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001221 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001222 size_t zlen;
1223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001225 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001226 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001229 return( ret );
1230 }
1231
1232 *(p++) = (unsigned char)( zlen >> 8 );
1233 *(p++) = (unsigned char)( zlen );
1234 p += zlen;
1235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001237 }
1238 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001243 }
1244
1245 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001246 if( end - p < 2 )
1247 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001248
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001249 *(p++) = (unsigned char)( psk_len >> 8 );
1250 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001251
1252 if( end < p || (size_t)( end - p ) < psk_len )
1253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1254
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001255 memcpy( p, psk, psk_len );
1256 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001257
1258 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1259
1260 return( 0 );
1261}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001265/*
1266 * SSLv3.0 MAC functions
1267 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001268#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001269static void ssl_mac( mbedtls_md_context_t *md_ctx,
1270 const unsigned char *secret,
1271 const unsigned char *buf, size_t len,
1272 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001273 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001274{
1275 unsigned char header[11];
1276 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001277 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1279 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001280
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001281 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001283 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001284 else
Paul Bakker68884e32013-01-07 18:20:04 +01001285 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001286
1287 memcpy( header, ctr, 8 );
1288 header[ 8] = (unsigned char) type;
1289 header[ 9] = (unsigned char)( len >> 8 );
1290 header[10] = (unsigned char)( len );
1291
Paul Bakker68884e32013-01-07 18:20:04 +01001292 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_md_starts( md_ctx );
1294 mbedtls_md_update( md_ctx, secret, md_size );
1295 mbedtls_md_update( md_ctx, padding, padlen );
1296 mbedtls_md_update( md_ctx, header, 11 );
1297 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001298 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
Paul Bakker68884e32013-01-07 18:20:04 +01001300 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 mbedtls_md_starts( md_ctx );
1302 mbedtls_md_update( md_ctx, secret, md_size );
1303 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001304 mbedtls_md_update( md_ctx, out, md_size );
1305 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001306}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1310 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001311 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001312#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001313#endif
1314
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001315/* The function below is only used in the Lucky 13 counter-measure in
1316 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1317#if defined(SSL_SOME_MODES_USE_MAC) && \
1318 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1319 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1320 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1321/* This function makes sure every byte in the memory region is accessed
1322 * (in ascending addresses order) */
1323static void ssl_read_memory( unsigned char *p, size_t len )
1324{
1325 unsigned char acc = 0;
1326 volatile unsigned char force;
1327
1328 for( ; len != 0; p++, len-- )
1329 acc ^= *p;
1330
1331 force = acc;
1332 (void) force;
1333}
1334#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1335
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001336/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001340{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001342 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001346 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1349 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001350 }
1351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001355 ssl->out_msg, ssl->out_msglen );
1356
Angus Grattond8213d02016-05-25 20:56:48 +10001357 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
Hanno Beckerd33f1ca2017-09-18 10:55:31 +01001358 {
Hanno Becker184f6752017-10-04 13:47:33 +01001359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1360 (unsigned) ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10001361 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Beckerd33f1ca2017-09-18 10:55:31 +01001362 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1363 }
1364
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001366 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001368#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 if( mode == MBEDTLS_MODE_STREAM ||
1370 ( mode == MBEDTLS_MODE_CBC
1371#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1372 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001373#endif
1374 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#if defined(MBEDTLS_SSL_PROTO_SSL3)
1377 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001378 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001379 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001380
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001381 ssl_mac( &ssl->transform_out->md_ctx_enc,
1382 ssl->transform_out->mac_enc,
1383 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001384 ssl->out_ctr, ssl->out_msgtype,
1385 mac );
1386
1387 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001388 }
1389 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001390#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1392 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1393 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001394 {
Hanno Becker992b6872017-11-09 18:57:39 +00001395 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1398 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1399 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1400 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001401 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001402 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001404
1405 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001406 }
1407 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001408#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1411 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001412 }
1413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001415 ssl->out_msg + ssl->out_msglen,
1416 ssl->transform_out->maclen );
1417
1418 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001419 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001420 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001421#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001422
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001423 /*
1424 * Encrypt
1425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1427 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001429 int ret;
1430 size_t olen = 0;
1431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 "including %d bytes of padding",
1434 ssl->out_msglen, 0 ) );
1435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001437 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001438 ssl->transform_out->ivlen,
1439 ssl->out_msg, ssl->out_msglen,
1440 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001443 return( ret );
1444 }
1445
1446 if( ssl->out_msglen != olen )
1447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1449 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001450 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 }
Paul Bakker68884e32013-01-07 18:20:04 +01001452 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001454#if defined(MBEDTLS_GCM_C) || \
1455 defined(MBEDTLS_CCM_C) || \
1456 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001458 mode == MBEDTLS_MODE_CCM ||
1459 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001460 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001461 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001462 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001463 unsigned char *enc_msg;
1464 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001465 unsigned char iv[12];
1466 mbedtls_ssl_transform *transform = ssl->transform_out;
1467 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001469 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001470
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001471 /*
1472 * Prepare additional authenticated data
1473 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001474 memcpy( add_data, ssl->out_ctr, 8 );
1475 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001477 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001478 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1479 add_data[12] = ssl->out_msglen & 0xFF;
1480
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001481 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001482
Paul Bakker68884e32013-01-07 18:20:04 +01001483 /*
1484 * Generate IV
1485 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001486 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1487 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001488 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001489 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1490 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1491 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1492
1493 }
1494 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1495 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001496 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001497 unsigned char i;
1498
1499 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1500
1501 for( i = 0; i < 8; i++ )
1502 iv[i+4] ^= ssl->out_ctr[i];
1503 }
1504 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001505 {
1506 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1508 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001509 }
1510
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001511 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1512 iv, transform->ivlen );
1513 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1514 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001515
Paul Bakker68884e32013-01-07 18:20:04 +01001516 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001517 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001518 */
1519 enc_msg = ssl->out_msg;
1520 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001521 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001524 "including 0 bytes of padding",
1525 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001526
Paul Bakker68884e32013-01-07 18:20:04 +01001527 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001528 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001529 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001530 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1531 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001532 add_data, 13,
1533 enc_msg, enc_msglen,
1534 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001535 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001538 return( ret );
1539 }
1540
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001541 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1544 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001545 }
1546
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001547 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001548 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001551 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1554#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001555 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001557 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001558 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001559 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001560 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001561
Paul Bakker48916f92012-09-16 19:57:18 +00001562 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1563 ssl->transform_out->ivlen;
1564 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001565 padlen = 0;
1566
1567 for( i = 0; i <= padlen; i++ )
1568 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1569
1570 ssl->out_msglen += padlen + 1;
1571
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001572 enc_msglen = ssl->out_msglen;
1573 enc_msg = ssl->out_msg;
1574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001576 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001577 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1578 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001581 {
1582 /*
1583 * Generate IV
1584 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001585 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001586 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001587 if( ret != 0 )
1588 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001589
Paul Bakker92be97b2013-01-02 17:30:03 +01001590 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001591 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001592
1593 /*
1594 * Fix pointer positions and message length with added IV
1595 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001596 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001597 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001598 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001599 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001603 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001604 ssl->out_msglen, ssl->transform_out->ivlen,
1605 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001608 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001609 ssl->transform_out->ivlen,
1610 enc_msg, enc_msglen,
1611 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001614 return( ret );
1615 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001616
Paul Bakkercca5b812013-08-31 17:40:26 +02001617 if( enc_msglen != olen )
1618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1620 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001621 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1624 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001625 {
1626 /*
1627 * Save IV in SSL3 and TLS1
1628 */
1629 memcpy( ssl->transform_out->iv_enc,
1630 ssl->transform_out->cipher_ctx_enc.iv,
1631 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001633#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001636 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001637 {
1638 /*
1639 * MAC(MAC_write_key, seq_num +
1640 * TLSCipherText.type +
1641 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001642 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001643 * IV + // except for TLS 1.0
1644 * ENC(content + padding + padding_length));
1645 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001646 unsigned char pseudo_hdr[13];
1647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001649
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001650 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1651 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001652 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1653 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1658 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001659 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001661 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001663
1664 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001665 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001666 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001668 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001669 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001671 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001675 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001677 /* Make extra sure authentication was performed, exactly once */
1678 if( auth_done != 1 )
1679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1681 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001682 }
1683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
1686 return( 0 );
1687}
1688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001690{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001692 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001693#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001694 size_t padlen = 0, correct = 1;
1695#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001698
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001699 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001703 }
1704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001706
Paul Bakker48916f92012-09-16 19:57:18 +00001707 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001710 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001712 }
1713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1715 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001716 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001717 int ret;
1718 size_t olen = 0;
1719
Paul Bakker68884e32013-01-07 18:20:04 +01001720 padlen = 0;
1721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001723 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001724 ssl->transform_in->ivlen,
1725 ssl->in_msg, ssl->in_msglen,
1726 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001729 return( ret );
1730 }
1731
1732 if( ssl->in_msglen != olen )
1733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1735 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001736 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001737 }
Paul Bakker68884e32013-01-07 18:20:04 +01001738 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001740#if defined(MBEDTLS_GCM_C) || \
1741 defined(MBEDTLS_CCM_C) || \
1742 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001744 mode == MBEDTLS_MODE_CCM ||
1745 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001746 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001747 int ret;
1748 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001749 unsigned char *dec_msg;
1750 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001751 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001752 unsigned char iv[12];
1753 mbedtls_ssl_transform *transform = ssl->transform_in;
1754 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001756 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001757
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001758 /*
1759 * Compute and update sizes
1760 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001761 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001764 "+ taglen (%d)", ssl->in_msglen,
1765 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001767 }
1768 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1769
Paul Bakker68884e32013-01-07 18:20:04 +01001770 dec_msg = ssl->in_msg;
1771 dec_msg_result = ssl->in_msg;
1772 ssl->in_msglen = dec_msglen;
1773
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001774 /*
1775 * Prepare additional authenticated data
1776 */
Paul Bakker68884e32013-01-07 18:20:04 +01001777 memcpy( add_data, ssl->in_ctr, 8 );
1778 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001780 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001781 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1782 add_data[12] = ssl->in_msglen & 0xFF;
1783
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001784 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001785
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001786 /*
1787 * Prepare IV
1788 */
1789 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1790 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001791 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001792 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1793 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001794
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001795 }
1796 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1797 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001798 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001799 unsigned char i;
1800
1801 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1802
1803 for( i = 0; i < 8; i++ )
1804 iv[i+4] ^= ssl->in_ctr[i];
1805 }
1806 else
1807 {
1808 /* Reminder if we ever add an AEAD mode with a different size */
1809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1810 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1811 }
1812
1813 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001815
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001816 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001817 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001820 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001821 add_data, 13,
1822 dec_msg, dec_msglen,
1823 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001824 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1829 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001830
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001831 return( ret );
1832 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001833 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001834
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001835 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1838 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001839 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001840 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001841 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1843#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001844 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001846 {
Paul Bakker45829992013-01-03 14:52:21 +01001847 /*
1848 * Decrypt and check the padding
1849 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001850 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001851 unsigned char *dec_msg;
1852 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001853 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001854 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001855 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001856
Paul Bakker5121ce52009-01-03 21:22:43 +00001857 /*
Paul Bakker45829992013-01-03 14:52:21 +01001858 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1861 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001862 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001863#endif
Paul Bakker45829992013-01-03 14:52:21 +01001864
1865 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1866 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001869 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1870 ssl->transform_in->ivlen,
1871 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001873 }
1874
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001875 dec_msglen = ssl->in_msglen;
1876 dec_msg = ssl->in_msg;
1877 dec_msg_result = ssl->in_msg;
1878
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001879 /*
1880 * Authenticate before decrypt if enabled
1881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1883 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001884 {
Hanno Becker992b6872017-11-09 18:57:39 +00001885 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001886 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001889
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001890 dec_msglen -= ssl->transform_in->maclen;
1891 ssl->in_msglen -= ssl->transform_in->maclen;
1892
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001893 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1894 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1895 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1896 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1901 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001902 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001903 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001907 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001908 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001909 ssl->transform_in->maclen );
1910
Hanno Becker992b6872017-11-09 18:57:39 +00001911 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1912 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001917 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001918 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001919 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001921
1922 /*
1923 * Check length sanity
1924 */
1925 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001928 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001930 }
1931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001933 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001934 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001935 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001937 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001938 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00001939 dec_msglen -= ssl->transform_in->ivlen;
1940 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001941
Paul Bakker48916f92012-09-16 19:57:18 +00001942 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001943 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001944 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001948 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001949 ssl->transform_in->ivlen,
1950 dec_msg, dec_msglen,
1951 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001954 return( ret );
1955 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001956
Paul Bakkercca5b812013-08-31 17:40:26 +02001957 if( dec_msglen != olen )
1958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001961 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1964 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001965 {
1966 /*
1967 * Save IV in SSL3 and TLS1
1968 */
1969 memcpy( ssl->transform_in->iv_dec,
1970 ssl->transform_in->cipher_ctx_dec.iv,
1971 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001973#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001974
1975 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001976
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001977 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001978 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980#if defined(MBEDTLS_SSL_DEBUG_ALL)
1981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01001982 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001983#endif
Paul Bakker45829992013-01-03 14:52:21 +01001984 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001985 correct = 0;
1986 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988#if defined(MBEDTLS_SSL_PROTO_SSL3)
1989 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001990 {
Paul Bakker48916f92012-09-16 19:57:18 +00001991 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993#if defined(MBEDTLS_SSL_DEBUG_ALL)
1994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001995 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001996 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001997#endif
Paul Bakker45829992013-01-03 14:52:21 +01001998 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 }
2000 }
2001 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2003#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2004 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2005 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002006 {
2007 /*
Paul Bakker45829992013-01-03 14:52:21 +01002008 * TLSv1+: always check the padding up to the first failure
2009 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002010 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002011 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002012 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002013 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002014
Paul Bakker956c9e02013-12-19 14:42:28 +01002015 /*
2016 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002017 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002018 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002019 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002020 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002021 *
2022 * In both cases we reset padding_idx to a safe value (0) to
2023 * prevent out-of-buffer reads.
2024 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002025 correct &= ( padlen <= ssl->in_msglen );
2026 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002027 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002028
2029 padding_idx *= correct;
2030
Angus Grattonb512bc12018-06-19 15:57:50 +10002031 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002032 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002033 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002034 pad_count += real_count *
2035 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2036 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002037
2038 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002041 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002043#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002044 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002046 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2048 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2051 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002052 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002053
2054 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002056 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002058 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2061 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002062 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002063
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002064#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002067#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002068
2069 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002070 * Authenticate if not done yet.
2071 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002073#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002074 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002075 {
Hanno Becker992b6872017-11-09 18:57:39 +00002076 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002077
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002078 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002079
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002080 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2081 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083#if defined(MBEDTLS_SSL_PROTO_SSL3)
2084 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002085 {
2086 ssl_mac( &ssl->transform_in->md_ctx_dec,
2087 ssl->transform_in->mac_dec,
2088 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002089 ssl->in_ctr, ssl->in_msgtype,
2090 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002091 }
2092 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2094#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2095 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2096 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002097 {
2098 /*
2099 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002100 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002101 *
2102 * Known timing attacks:
2103 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2104 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002105 * To compensate for different timings for the MAC calculation
2106 * depending on how much padding was removed (which is determined
2107 * by padlen), process extra_run more blocks through the hash
2108 * function.
2109 *
2110 * The formula in the paper is
2111 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2112 * where L1 is the size of the header plus the decrypted message
2113 * plus CBC padding and L2 is the size of the header plus the
2114 * decrypted message. This is for an underlying hash function
2115 * with 64-byte blocks.
2116 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2117 * correctly. We round down instead of up, so -56 is the correct
2118 * value for our calculations instead of -55.
2119 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002120 * Repeat the formula rather than defining a block_size variable.
2121 * This avoids requiring division by a variable at runtime
2122 * (which would be marginally less efficient and would require
2123 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002124 */
2125 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002126
2127 /*
2128 * The next two sizes are the minimum and maximum values of
2129 * in_msglen over all padlen values.
2130 *
2131 * They're independent of padlen, since we previously did
2132 * in_msglen -= padlen.
2133 *
2134 * Note that max_len + maclen is never more than the buffer
2135 * length, as we previously did in_msglen -= maclen too.
2136 */
2137 const size_t max_len = ssl->in_msglen + padlen;
2138 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2139
Gilles Peskine20b44082018-05-29 14:06:49 +02002140 switch( ssl->transform_in->ciphersuite_info->mac )
2141 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002142#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2143 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002144 case MBEDTLS_MD_MD5:
2145 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002146 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002147 /* 8 bytes of message size, 64-byte compression blocks */
2148 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2149 ( 13 + ssl->in_msglen + 8 ) / 64;
2150 break;
2151#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002152#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002153 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002154 /* 16 bytes of message size, 128-byte compression blocks */
2155 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2156 ( 13 + ssl->in_msglen + 16 ) / 128;
2157 break;
2158#endif
2159 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002161 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2162 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002163
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002164 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2167 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2168 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2169 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002170 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002171 /* Make sure we access everything even when padlen > 0. This
2172 * makes the synchronisation requirements for just-in-time
2173 * Prime+Probe attacks much tighter and hopefully impractical. */
2174 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002175 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002176
2177 /* Call mbedtls_md_process at least once due to cache attacks
2178 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002179 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002183
2184 /* Make sure we access all the memory that could contain the MAC,
2185 * before we check it in the next code block. This makes the
2186 * synchronisation requirements for just-in-time Prime+Probe
2187 * attacks much tighter and hopefully impractical. */
2188 ssl_read_memory( ssl->in_msg + min_len,
2189 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002190 }
2191 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2193 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002197 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002198
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002199#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002200 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2201 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2202 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002203#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002204
Hanno Becker992b6872017-11-09 18:57:39 +00002205 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2206 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208#if defined(MBEDTLS_SSL_DEBUG_ALL)
2209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002210#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002211 correct = 0;
2212 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002213 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002214
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002215 /*
2216 * Finally check the correct flag
2217 */
2218 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002220 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002221#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002222
2223 /* Make extra sure authentication was performed, exactly once */
2224 if( auth_done != 1 )
2225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002228 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002229
2230 if( ssl->in_msglen == 0 )
2231 {
Angus Gratton34817922018-06-19 15:58:22 +10002232#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2233 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2234 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2235 {
2236 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2238 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2239 }
2240#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2241
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 ssl->nb_zero++;
2243
2244 /*
2245 * Three or more empty messages may be a DoS attack
2246 * (excessive CPU consumption).
2247 */
2248 if( ssl->nb_zero > 3 )
2249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 }
2254 }
2255 else
2256 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002259 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002260 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002261 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002262 }
2263 else
2264#endif
2265 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002266 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002267 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2268 if( ++ssl->in_ctr[i - 1] != 0 )
2269 break;
2270
2271 /* The loop goes to its end iff the counter is wrapping */
2272 if( i == ssl_ep_len( ssl ) )
2273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2275 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002276 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002277 }
2278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002280
2281 return( 0 );
2282}
2283
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002284#undef MAC_NONE
2285#undef MAC_PLAINTEXT
2286#undef MAC_CIPHERTEXT
2287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002289/*
2290 * Compression/decompression functions
2291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002293{
2294 int ret;
2295 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002296 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002297 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002298 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002301
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002302 if( len_pre == 0 )
2303 return( 0 );
2304
Paul Bakker2770fbd2012-07-03 13:30:23 +00002305 memcpy( msg_pre, ssl->out_msg, len_pre );
2306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002308 ssl->out_msglen ) );
2309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002311 ssl->out_msg, ssl->out_msglen );
2312
Paul Bakker48916f92012-09-16 19:57:18 +00002313 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2314 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2315 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002316 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002317
Paul Bakker48916f92012-09-16 19:57:18 +00002318 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002319 if( ret != Z_OK )
2320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2322 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002323 }
2324
Angus Grattond8213d02016-05-25 20:56:48 +10002325 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002326 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002329 ssl->out_msglen ) );
2330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002332 ssl->out_msg, ssl->out_msglen );
2333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002335
2336 return( 0 );
2337}
2338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002340{
2341 int ret;
2342 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002343 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002344 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002345 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002348
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002349 if( len_pre == 0 )
2350 return( 0 );
2351
Paul Bakker2770fbd2012-07-03 13:30:23 +00002352 memcpy( msg_pre, ssl->in_msg, len_pre );
2353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002355 ssl->in_msglen ) );
2356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002358 ssl->in_msg, ssl->in_msglen );
2359
Paul Bakker48916f92012-09-16 19:57:18 +00002360 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2361 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2362 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002363 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002364 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002365
Paul Bakker48916f92012-09-16 19:57:18 +00002366 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002367 if( ret != Z_OK )
2368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2370 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002371 }
2372
Angus Grattond8213d02016-05-25 20:56:48 +10002373 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002374 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002377 ssl->in_msglen ) );
2378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002380 ssl->in_msg, ssl->in_msglen );
2381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002383
2384 return( 0 );
2385}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2389static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391#if defined(MBEDTLS_SSL_PROTO_DTLS)
2392static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002393{
2394 /* If renegotiation is not enforced, retransmit until we would reach max
2395 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002396 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002397 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002398 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002399 unsigned char doublings = 1;
2400
2401 while( ratio != 0 )
2402 {
2403 ++doublings;
2404 ratio >>= 1;
2405 }
2406
2407 if( ++ssl->renego_records_seen > doublings )
2408 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002410 return( 0 );
2411 }
2412 }
2413
2414 return( ssl_write_hello_request( ssl ) );
2415}
2416#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002418
Paul Bakker5121ce52009-01-03 21:22:43 +00002419/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002420 * Fill the input message buffer by appending data to it.
2421 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002422 *
2423 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2424 * available (from this read and/or a previous one). Otherwise, an error code
2425 * is returned (possibly EOF or WANT_READ).
2426 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002427 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2428 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2429 * since we always read a whole datagram at once.
2430 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002431 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002432 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002435{
Paul Bakker23986e52011-04-24 08:57:21 +00002436 int ret;
2437 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002440
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002441 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002444 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002446 }
2447
Angus Grattond8213d02016-05-25 20:56:48 +10002448 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002452 }
2453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002455 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002457 uint32_t timeout;
2458
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002459 /* Just to be sure */
2460 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2461 {
2462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2463 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2464 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2465 }
2466
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002467 /*
2468 * The point is, we need to always read a full datagram at once, so we
2469 * sometimes read more then requested, and handle the additional data.
2470 * It could be the rest of the current record (while fetching the
2471 * header) and/or some other records in the same datagram.
2472 */
2473
2474 /*
2475 * Move to the next record in the already read datagram if applicable
2476 */
2477 if( ssl->next_record_offset != 0 )
2478 {
2479 if( ssl->in_left < ssl->next_record_offset )
2480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2482 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002483 }
2484
2485 ssl->in_left -= ssl->next_record_offset;
2486
2487 if( ssl->in_left != 0 )
2488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002490 ssl->next_record_offset ) );
2491 memmove( ssl->in_hdr,
2492 ssl->in_hdr + ssl->next_record_offset,
2493 ssl->in_left );
2494 }
2495
2496 ssl->next_record_offset = 0;
2497 }
2498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002501
2502 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002503 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002504 */
2505 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002508 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002509 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002510
2511 /*
2512 * A record can't be split accross datagrams. If we need to read but
2513 * are not at the beginning of a new record, the caller did something
2514 * wrong.
2515 */
2516 if( ssl->in_left != 0 )
2517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2519 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002520 }
2521
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002522 /*
2523 * Don't even try to read if time's out already.
2524 * This avoids by-passing the timer when repeatedly receiving messages
2525 * that will end up being dropped.
2526 */
2527 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002528 {
2529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002530 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002531 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002532 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002533 {
Angus Grattond8213d02016-05-25 20:56:48 +10002534 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002537 timeout = ssl->handshake->retransmit_timeout;
2538 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002539 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002542
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002543 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002544 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2545 timeout );
2546 else
2547 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002550
2551 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002553 }
2554
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002555 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002558 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002561 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002562 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002565 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002566 }
2567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002571 return( ret );
2572 }
2573
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002574 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002575 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002577 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002579 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002580 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002583 return( ret );
2584 }
2585
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002586 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002587 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002589 }
2590
Paul Bakker5121ce52009-01-03 21:22:43 +00002591 if( ret < 0 )
2592 return( ret );
2593
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002594 ssl->in_left = ret;
2595 }
2596 else
2597#endif
2598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002600 ssl->in_left, nb_want ) );
2601
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002602 while( ssl->in_left < nb_want )
2603 {
2604 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002605
2606 if( ssl_check_timer( ssl ) != 0 )
2607 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2608 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002609 {
2610 if( ssl->f_recv_timeout != NULL )
2611 {
2612 ret = ssl->f_recv_timeout( ssl->p_bio,
2613 ssl->in_hdr + ssl->in_left, len,
2614 ssl->conf->read_timeout );
2615 }
2616 else
2617 {
2618 ret = ssl->f_recv( ssl->p_bio,
2619 ssl->in_hdr + ssl->in_left, len );
2620 }
2621 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002624 ssl->in_left, nb_want ) );
2625 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002626
2627 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002629
2630 if( ret < 0 )
2631 return( ret );
2632
mohammad160352aecb92018-03-28 23:41:40 -07002633 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002634 {
Darryl Green11999bb2018-03-13 15:22:58 +00002635 MBEDTLS_SSL_DEBUG_MSG( 1,
2636 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002637 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002638 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2639 }
2640
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002641 ssl->in_left += ret;
2642 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002643 }
2644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
2647 return( 0 );
2648}
2649
2650/*
2651 * Flush any data not yet written
2652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002654{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002655 int ret;
2656 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002660 if( ssl->f_send == NULL )
2661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002663 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002665 }
2666
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002667 /* Avoid incrementing counter if data is flushed */
2668 if( ssl->out_left == 0 )
2669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002671 return( 0 );
2672 }
2673
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 while( ssl->out_left > 0 )
2675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2677 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) +
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002680 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002681 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
2685 if( ret <= 0 )
2686 return( ret );
2687
mohammad160352aecb92018-03-28 23:41:40 -07002688 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002689 {
Darryl Green11999bb2018-03-13 15:22:58 +00002690 MBEDTLS_SSL_DEBUG_MSG( 1,
2691 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002692 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002693 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2694 }
2695
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 ssl->out_left -= ret;
2697 }
2698
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002699 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002700 if( ++ssl->out_ctr[i - 1] != 0 )
2701 break;
2702
2703 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002704 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2707 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002708 }
2709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002711
2712 return( 0 );
2713}
2714
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002715/*
2716 * Functions to handle the DTLS retransmission state machine
2717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002719/*
2720 * Append current handshake message to current outgoing flight
2721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002723{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 mbedtls_ssl_flight_item *msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002725
2726 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002727 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002728 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002731 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002732 }
2733
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002734 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002735 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002738 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002739 }
2740
2741 /* Copy current handshake message with headers */
2742 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2743 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002744 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002745 msg->next = NULL;
2746
2747 /* Append to the current flight */
2748 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002749 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002750 else
2751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002753 while( cur->next != NULL )
2754 cur = cur->next;
2755 cur->next = msg;
2756 }
2757
2758 return( 0 );
2759}
2760
2761/*
2762 * Free the current flight of handshake messages
2763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002765{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 mbedtls_ssl_flight_item *cur = flight;
2767 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002768
2769 while( cur != NULL )
2770 {
2771 next = cur->next;
2772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 mbedtls_free( cur->p );
2774 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002775
2776 cur = next;
2777 }
2778}
2779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2781static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002782#endif
2783
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002784/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002785 * Swap transform_out and out_ctr with the alternative ones
2786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002788{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002790 unsigned char tmp_out_ctr[8];
2791
2792 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002795 return;
2796 }
2797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002799
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002800 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002801 tmp_transform = ssl->transform_out;
2802 ssl->transform_out = ssl->handshake->alt_transform_out;
2803 ssl->handshake->alt_transform_out = tmp_transform;
2804
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002805 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002806 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2807 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2808 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002809
2810 /* Adjust to the newly activated transform */
2811 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002813 {
2814 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2815 ssl->transform_out->fixed_ivlen;
2816 }
2817 else
2818 ssl->out_msg = ssl->out_iv;
2819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2821 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2826 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002827 }
2828 }
2829#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002830}
2831
2832/*
2833 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002834 */
2835int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2836{
2837 int ret = 0;
2838
2839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2840
2841 ret = mbedtls_ssl_flight_transmit( ssl );
2842
2843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2844
2845 return( ret );
2846}
2847
2848/*
2849 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002850 *
2851 * Need to remember the current message in case flush_output returns
2852 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002853 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002854 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002855int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002856{
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002860 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002862
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002863 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002864 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002865 ssl_swap_epochs( ssl );
2866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002868 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002869
2870 while( ssl->handshake->cur_msg != NULL )
2871 {
2872 int ret;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002873 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002874 /* Swap epochs before sending Finished: we can't do it after
2875 * sending ChangeCipherSpec, in case write returns WANT_READ.
2876 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2878 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002879 {
2880 ssl_swap_epochs( ssl );
2881 }
2882
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002883 /* CCS is copied as is, while HS messages may need fragmentation */
2884 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2885 {
2886 memcpy( ssl->out_msg, cur->p, cur->len );
2887 ssl->out_msglen = cur->len;
2888 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002889
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002890 /* Update position inside current message */
2891 ssl->handshake->cur_msg_p += cur->len;
2892 }
2893 else
2894 {
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002895 const int ret_payload = mbedtls_ssl_get_max_out_record_payload( ssl );
2896 const size_t max_record_payload = (size_t) ret_payload;
2897 /* DTLS handshake headers are 12 bytes */
2898 const size_t max_hs_fragment_len = max_record_payload - 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002899 const unsigned char * const p = ssl->handshake->cur_msg_p;
2900 const size_t hs_len = cur->len - 12;
2901 const size_t frag_off = p - ( cur->p + 12 );
2902 const size_t rem_len = hs_len - frag_off;
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002903 const size_t frag_len = rem_len > max_hs_fragment_len
2904 ? max_hs_fragment_len : rem_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002905
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002906 if( ret_payload < 0 )
2907 {
2908 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload",
2909 ret_payload );
2910 return( ret_payload );
2911 }
2912
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002913 if( frag_off == 0 && frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002914 {
2915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
2916 (unsigned) hs_len,
2917 (unsigned) max_hs_fragment_len ) );
2918 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002919
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002920 /* Messages are stored with handshake headers as if not fragmented,
2921 * copy beginning of headers then fill fragmentation fields.
2922 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2923 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002924
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002925 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2926 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2927 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2928
2929 ssl->out_msg[ 9] = ( ( frag_len >> 16 ) & 0xff );
2930 ssl->out_msg[10] = ( ( frag_len >> 8 ) & 0xff );
2931 ssl->out_msg[11] = ( ( frag_len ) & 0xff );
2932
2933 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2934
Manuel Pégourié-Gonnard7e89c172018-08-13 12:45:26 +02002935 /* Copy the handshake message content and set records fields */
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002936 memcpy( ssl->out_msg + 12, p, frag_len );
2937 ssl->out_msglen = frag_len + 12;
2938 ssl->out_msgtype = cur->type;
2939
2940 /* Update position inside current message */
2941 ssl->handshake->cur_msg_p += frag_len;
2942 }
2943
2944 /* If done with the current message move to the next one if any */
2945 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2946 {
2947 if( cur->next != NULL )
2948 {
2949 ssl->handshake->cur_msg = cur->next;
2950 ssl->handshake->cur_msg_p = cur->next->p + 12;
2951 }
2952 else
2953 {
2954 ssl->handshake->cur_msg = NULL;
2955 ssl->handshake->cur_msg_p = NULL;
2956 }
2957 }
2958
2959 /* Actually send the message out */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002963 return( ret );
2964 }
2965 }
2966
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002967 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2969 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002970 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002973 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2974 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002975
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002977
2978 return( 0 );
2979}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002980
2981/*
2982 * To be called when the last message of an incoming flight is received.
2983 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002985{
2986 /* We won't need to resend that one any more */
2987 ssl_flight_free( ssl->handshake->flight );
2988 ssl->handshake->flight = NULL;
2989 ssl->handshake->cur_msg = NULL;
2990
2991 /* The next incoming flight will start with this msg_seq */
2992 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2993
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002994 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002995 ssl_set_timer( ssl, 0 );
2996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2998 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003001 }
3002 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003004}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003005
3006/*
3007 * To be called when the last message of an outgoing flight is send.
3008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003010{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003011 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003012 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3015 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003018 }
3019 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003020 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003021}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003023
Paul Bakker5121ce52009-01-03 21:22:43 +00003024/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003025 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003026 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003027
3028/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003029 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003030 *
3031 * - fill in handshake headers
3032 * - update handshake checksum
3033 * - DTLS: save message for resending
3034 * - then pass to the record layer
3035 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003036 * DTLS: except for HelloRequest, messages are only queued, and will only be
3037 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003038 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003039 * Inputs:
3040 * - ssl->out_msglen: 4 + actual handshake message len
3041 * (4 is the size of handshake headers for TLS)
3042 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3043 * - ssl->out_msg + 4: the handshake message body
3044 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003045 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003046 * - ssl->out_msglen: the length of the record contents
3047 * (including handshake headers but excluding record headers)
3048 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003049 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003050int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003051{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003052 int ret;
3053 const size_t hs_len = ssl->out_msglen - 4;
3054 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3057
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003058 /*
3059 * Sanity checks
3060 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003061 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3062 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3063 {
3064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3065 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3066 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003067
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003068 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3069 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3070 ssl->handshake == NULL )
3071 {
3072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3074 }
3075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003077 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003078 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003080 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3082 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003083 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003084#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003085
3086 /*
3087 * Fill handshake headers
3088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003091 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3092 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3093 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003094
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003095 /*
3096 * DTLS has additional fields in the Handshake layer,
3097 * between the length field and the actual payload:
3098 * uint16 message_seq;
3099 * uint24 fragment_offset;
3100 * uint24 fragment_length;
3101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003103 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003104 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003105 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003106 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003107 {
3108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3109 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003110 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003111 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003112 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3113 }
3114
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003115 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003116 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003117
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003118 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003119 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003120 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003121 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3122 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3123 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003124 }
3125 else
3126 {
3127 ssl->out_msg[4] = 0;
3128 ssl->out_msg[5] = 0;
3129 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003130
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003131 /* Handshake hashes are computed without fragmentation,
3132 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003133 memset( ssl->out_msg + 6, 0x00, 3 );
3134 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003135 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003137
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003138 /* Update running hashes of hanshake messages seen */
3139 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3140 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003141 }
3142
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003143 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003145 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003146 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003147 {
3148 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003151 return( ret );
3152 }
3153 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003154 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003155#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003156 {
3157 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
3158 {
3159 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3160 return( ret );
3161 }
3162 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003163
3164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3165
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003166 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003167}
3168
3169/*
3170 * Record layer functions
3171 */
3172
3173/*
3174 * Write current record.
3175 *
3176 * Uses:
3177 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3178 * - ssl->out_msglen: length of the record content (excl headers)
3179 * - ssl->out_msg: record content
3180 */
3181int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
3182{
3183 int ret, done = 0;
3184 size_t len = ssl->out_msglen;
3185
3186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003189 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003191 {
3192 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003195 return( ret );
3196 }
3197
3198 len = ssl->out_msglen;
3199 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3203 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 ret = mbedtls_ssl_hw_record_write( ssl );
3208 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3211 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003212 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003213
3214 if( ret == 0 )
3215 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003216 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003218 if( !done )
3219 {
3220 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003222 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003223
3224 ssl->out_len[0] = (unsigned char)( len >> 8 );
3225 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003226
Paul Bakker48916f92012-09-16 19:57:18 +00003227 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003228 {
3229 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003232 return( ret );
3233 }
3234
3235 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003236 ssl->out_len[0] = (unsigned char)( len >> 8 );
3237 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003238 }
3239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00003241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00003243 "version = [%d:%d], msglen = %d",
3244 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003245 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3248 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 }
3250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003254 return( ret );
3255 }
3256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003258
3259 return( 0 );
3260}
3261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003263/*
3264 * Mark bits in bitmask (used for DTLS HS reassembly)
3265 */
3266static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3267{
3268 unsigned int start_bits, end_bits;
3269
3270 start_bits = 8 - ( offset % 8 );
3271 if( start_bits != 8 )
3272 {
3273 size_t first_byte_idx = offset / 8;
3274
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003275 /* Special case */
3276 if( len <= start_bits )
3277 {
3278 for( ; len != 0; len-- )
3279 mask[first_byte_idx] |= 1 << ( start_bits - len );
3280
3281 /* Avoid potential issues with offset or len becoming invalid */
3282 return;
3283 }
3284
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003285 offset += start_bits; /* Now offset % 8 == 0 */
3286 len -= start_bits;
3287
3288 for( ; start_bits != 0; start_bits-- )
3289 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3290 }
3291
3292 end_bits = len % 8;
3293 if( end_bits != 0 )
3294 {
3295 size_t last_byte_idx = ( offset + len ) / 8;
3296
3297 len -= end_bits; /* Now len % 8 == 0 */
3298
3299 for( ; end_bits != 0; end_bits-- )
3300 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3301 }
3302
3303 memset( mask + offset / 8, 0xFF, len / 8 );
3304}
3305
3306/*
3307 * Check that bitmask is full
3308 */
3309static int ssl_bitmask_check( unsigned char *mask, size_t len )
3310{
3311 size_t i;
3312
3313 for( i = 0; i < len / 8; i++ )
3314 if( mask[i] != 0xFF )
3315 return( -1 );
3316
3317 for( i = 0; i < len % 8; i++ )
3318 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3319 return( -1 );
3320
3321 return( 0 );
3322}
3323
3324/*
3325 * Reassemble fragmented DTLS handshake messages.
3326 *
3327 * Use a temporary buffer for reassembly, divided in two parts:
3328 * - the first holds the reassembled message (including handshake header),
3329 * - the second holds a bitmask indicating which parts of the message
3330 * (excluding headers) have been received so far.
3331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003333{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003334 unsigned char *msg, *bitmask;
3335 size_t frag_len, frag_off;
3336 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3337
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003338 if( ssl->handshake == NULL )
3339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3341 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003342 }
3343
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003344 /*
3345 * For first fragment, check size and allocate buffer
3346 */
3347 if( ssl->handshake->hs_msg == NULL )
3348 {
3349 size_t alloc_len;
3350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003352 msg_len ) );
3353
Angus Grattond8213d02016-05-25 20:56:48 +10003354 if( ssl->in_hslen > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3357 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003358 }
3359
3360 /* The bitmask needs one bit per byte of message excluding header */
3361 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3362
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003363 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003364 if( ssl->handshake->hs_msg == NULL )
3365 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003367 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003368 }
3369
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003370 /* Prepare final header: copy msg_type, length and message_seq,
3371 * then add standardised fragment_offset and fragment_length */
3372 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3373 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3374 memcpy( ssl->handshake->hs_msg + 9,
3375 ssl->handshake->hs_msg + 1, 3 );
3376 }
3377 else
3378 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003379 /* Make sure msg_type and length are consistent */
3380 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3383 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003384 }
3385 }
3386
3387 msg = ssl->handshake->hs_msg + 12;
3388 bitmask = msg + msg_len;
3389
3390 /*
3391 * Check and copy current fragment
3392 */
3393 frag_off = ( ssl->in_msg[6] << 16 ) |
3394 ( ssl->in_msg[7] << 8 ) |
3395 ssl->in_msg[8];
3396 frag_len = ( ssl->in_msg[9] << 16 ) |
3397 ( ssl->in_msg[10] << 8 ) |
3398 ssl->in_msg[11];
3399
3400 if( frag_off + frag_len > msg_len )
3401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003403 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003405 }
3406
3407 if( frag_len + 12 > ssl->in_msglen )
3408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003410 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003412 }
3413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003415 frag_off, frag_len ) );
3416
3417 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3418 ssl_bitmask_set( bitmask, frag_off, frag_len );
3419
3420 /*
3421 * Do we have the complete message by now?
3422 * If yes, finalize it, else ask to read the next record.
3423 */
3424 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01003427 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003428 }
3429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003431
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003432 if( frag_len + 12 < ssl->in_msglen )
3433 {
3434 /*
3435 * We'got more handshake messages in the same record.
3436 * This case is not handled now because no know implementation does
3437 * that and it's hard to test, so we prefer to fail cleanly for now.
3438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3440 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003441 }
3442
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003443 if( ssl->in_left > ssl->next_record_offset )
3444 {
3445 /*
3446 * We've got more data in the buffer after the current record,
3447 * that we don't want to overwrite. Move it before writing the
3448 * reassembled message, and adjust in_left and next_record_offset.
3449 */
3450 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3451 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3452 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3453
3454 /* First compute and check new lengths */
3455 ssl->next_record_offset = new_remain - ssl->in_hdr;
3456 ssl->in_left = ssl->next_record_offset + remain_len;
3457
Angus Grattond8213d02016-05-25 20:56:48 +10003458 if( ssl->in_left > MBEDTLS_SSL_IN_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003459 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3462 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003463 }
3464
3465 memmove( new_remain, cur_remain, remain_len );
3466 }
3467
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003468 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003471 ssl->handshake->hs_msg = NULL;
3472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003474 ssl->in_msg, ssl->in_hslen );
3475
3476 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003479
Simon Butcher99000142016-10-13 17:21:01 +01003480int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003481{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003485 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003487 }
3488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003490 ( ssl->in_msg[1] << 16 ) |
3491 ( ssl->in_msg[2] << 8 ) |
3492 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003495 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003496 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003499 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003500 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003501 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003502 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003503
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003504 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003505 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3506 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3507 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3508 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003509 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003510 /* Retransmit only on last message from previous flight, to avoid
3511 * too many retransmissions.
3512 * Besides, No sane server ever retransmits HelloVerifyRequest */
3513 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003517 "message_seq = %d, start_of_flight = %d",
3518 recv_msg_seq,
3519 ssl->handshake->in_flight_start_seq ) );
3520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003524 return( ret );
3525 }
3526 }
3527 else
3528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003530 "message_seq = %d, expected = %d",
3531 recv_msg_seq,
3532 ssl->handshake->in_msg_seq ) );
3533 }
3534
Hanno Becker90333da2017-10-10 11:27:13 +01003535 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003536 }
3537 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003538
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003539 /* Reassemble if current message is fragmented or reassembly is
3540 * already in progress */
3541 if( ssl->in_msglen < ssl->in_hslen ||
3542 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3543 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3544 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003547
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003548 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003551 return( ret );
3552 }
3553 }
3554 }
3555 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003557 /* With TLS we don't handle fragmentation (for now) */
3558 if( ssl->in_msglen < ssl->in_hslen )
3559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3561 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003562 }
3563
Simon Butcher99000142016-10-13 17:21:01 +01003564 return( 0 );
3565}
3566
3567void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3568{
3569
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003570 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3571 ssl->handshake != NULL )
3572 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003573 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003574 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003575
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003576 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003578 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003579 ssl->handshake != NULL )
3580 {
3581 ssl->handshake->in_msg_seq++;
3582 }
3583#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003584}
3585
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003586/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003587 * DTLS anti-replay: RFC 6347 4.1.2.6
3588 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003589 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3590 * Bit n is set iff record number in_window_top - n has been seen.
3591 *
3592 * Usually, in_window_top is the last record number seen and the lsb of
3593 * in_window is set. The only exception is the initial state (record number 0
3594 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3597static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003598{
3599 ssl->in_window_top = 0;
3600 ssl->in_window = 0;
3601}
3602
3603static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3604{
3605 return( ( (uint64_t) buf[0] << 40 ) |
3606 ( (uint64_t) buf[1] << 32 ) |
3607 ( (uint64_t) buf[2] << 24 ) |
3608 ( (uint64_t) buf[3] << 16 ) |
3609 ( (uint64_t) buf[4] << 8 ) |
3610 ( (uint64_t) buf[5] ) );
3611}
3612
3613/*
3614 * Return 0 if sequence number is acceptable, -1 otherwise
3615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003617{
3618 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3619 uint64_t bit;
3620
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003621 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003622 return( 0 );
3623
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003624 if( rec_seqnum > ssl->in_window_top )
3625 return( 0 );
3626
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003627 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003628
3629 if( bit >= 64 )
3630 return( -1 );
3631
3632 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3633 return( -1 );
3634
3635 return( 0 );
3636}
3637
3638/*
3639 * Update replay window on new validated record
3640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003642{
3643 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3644
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003645 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003646 return;
3647
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003648 if( rec_seqnum > ssl->in_window_top )
3649 {
3650 /* Update window_top and the contents of the window */
3651 uint64_t shift = rec_seqnum - ssl->in_window_top;
3652
3653 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003654 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003655 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003656 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003657 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003658 ssl->in_window |= 1;
3659 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003660
3661 ssl->in_window_top = rec_seqnum;
3662 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003663 else
3664 {
3665 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003666 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003667
3668 if( bit < 64 ) /* Always true, but be extra sure */
3669 ssl->in_window |= (uint64_t) 1 << bit;
3670 }
3671}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003673
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003674#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003675/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003676static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3677
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003678/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003679 * Without any SSL context, check if a datagram looks like a ClientHello with
3680 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003681 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003682 *
3683 * - if cookie is valid, return 0
3684 * - if ClientHello looks superficially valid but cookie is not,
3685 * fill obuf and set olen, then
3686 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3687 * - otherwise return a specific error code
3688 */
3689static int ssl_check_dtls_clihlo_cookie(
3690 mbedtls_ssl_cookie_write_t *f_cookie_write,
3691 mbedtls_ssl_cookie_check_t *f_cookie_check,
3692 void *p_cookie,
3693 const unsigned char *cli_id, size_t cli_id_len,
3694 const unsigned char *in, size_t in_len,
3695 unsigned char *obuf, size_t buf_len, size_t *olen )
3696{
3697 size_t sid_len, cookie_len;
3698 unsigned char *p;
3699
3700 if( f_cookie_write == NULL || f_cookie_check == NULL )
3701 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3702
3703 /*
3704 * Structure of ClientHello with record and handshake headers,
3705 * and expected values. We don't need to check a lot, more checks will be
3706 * done when actually parsing the ClientHello - skipping those checks
3707 * avoids code duplication and does not make cookie forging any easier.
3708 *
3709 * 0-0 ContentType type; copied, must be handshake
3710 * 1-2 ProtocolVersion version; copied
3711 * 3-4 uint16 epoch; copied, must be 0
3712 * 5-10 uint48 sequence_number; copied
3713 * 11-12 uint16 length; (ignored)
3714 *
3715 * 13-13 HandshakeType msg_type; (ignored)
3716 * 14-16 uint24 length; (ignored)
3717 * 17-18 uint16 message_seq; copied
3718 * 19-21 uint24 fragment_offset; copied, must be 0
3719 * 22-24 uint24 fragment_length; (ignored)
3720 *
3721 * 25-26 ProtocolVersion client_version; (ignored)
3722 * 27-58 Random random; (ignored)
3723 * 59-xx SessionID session_id; 1 byte len + sid_len content
3724 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3725 * ...
3726 *
3727 * Minimum length is 61 bytes.
3728 */
3729 if( in_len < 61 ||
3730 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3731 in[3] != 0 || in[4] != 0 ||
3732 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3733 {
3734 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3735 }
3736
3737 sid_len = in[59];
3738 if( sid_len > in_len - 61 )
3739 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3740
3741 cookie_len = in[60 + sid_len];
3742 if( cookie_len > in_len - 60 )
3743 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3744
3745 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3746 cli_id, cli_id_len ) == 0 )
3747 {
3748 /* Valid cookie */
3749 return( 0 );
3750 }
3751
3752 /*
3753 * If we get here, we've got an invalid cookie, let's prepare HVR.
3754 *
3755 * 0-0 ContentType type; copied
3756 * 1-2 ProtocolVersion version; copied
3757 * 3-4 uint16 epoch; copied
3758 * 5-10 uint48 sequence_number; copied
3759 * 11-12 uint16 length; olen - 13
3760 *
3761 * 13-13 HandshakeType msg_type; hello_verify_request
3762 * 14-16 uint24 length; olen - 25
3763 * 17-18 uint16 message_seq; copied
3764 * 19-21 uint24 fragment_offset; copied
3765 * 22-24 uint24 fragment_length; olen - 25
3766 *
3767 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3768 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3769 *
3770 * Minimum length is 28.
3771 */
3772 if( buf_len < 28 )
3773 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3774
3775 /* Copy most fields and adapt others */
3776 memcpy( obuf, in, 25 );
3777 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3778 obuf[25] = 0xfe;
3779 obuf[26] = 0xff;
3780
3781 /* Generate and write actual cookie */
3782 p = obuf + 28;
3783 if( f_cookie_write( p_cookie,
3784 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3785 {
3786 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3787 }
3788
3789 *olen = p - obuf;
3790
3791 /* Go back and fill length fields */
3792 obuf[27] = (unsigned char)( *olen - 28 );
3793
3794 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3795 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3796 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3797
3798 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3799 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3800
3801 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3802}
3803
3804/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003805 * Handle possible client reconnect with the same UDP quadruplet
3806 * (RFC 6347 Section 4.2.8).
3807 *
3808 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3809 * that looks like a ClientHello.
3810 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003811 * - if the input looks like a ClientHello without cookies,
3812 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003813 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003814 * - if the input looks like a ClientHello with a valid cookie,
3815 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003816 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003817 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003818 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003819 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003820 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3821 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003822 */
3823static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3824{
3825 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003826 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003827
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003828 ret = ssl_check_dtls_clihlo_cookie(
3829 ssl->conf->f_cookie_write,
3830 ssl->conf->f_cookie_check,
3831 ssl->conf->p_cookie,
3832 ssl->cli_id, ssl->cli_id_len,
3833 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003834 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003835
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003836 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3837
3838 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003839 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003840 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003841 * If the error is permanent we'll catch it later,
3842 * if it's not, then hopefully it'll work next time. */
3843 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3844
3845 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003846 }
3847
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003848 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003849 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003850 /* Got a valid cookie, partially reset context */
3851 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3852 {
3853 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3854 return( ret );
3855 }
3856
3857 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003858 }
3859
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003860 return( ret );
3861}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003862#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003863
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003864/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003865 * ContentType type;
3866 * ProtocolVersion version;
3867 * uint16 epoch; // DTLS only
3868 * uint48 sequence_number; // DTLS only
3869 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003870 *
3871 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003872 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003873 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3874 *
3875 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003876 * 1. proceed with the record if this function returns 0
3877 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3878 * 3. return CLIENT_RECONNECT if this function return that value
3879 * 4. drop the whole datagram if this function returns anything else.
3880 * Point 2 is needed when the peer is resending, and we have already received
3881 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003884{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003885 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003888
Paul Bakker5121ce52009-01-03 21:22:43 +00003889 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003890 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003891 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003894 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003895 ssl->in_msgtype,
3896 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003897
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003898 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003899 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3900 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3901 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3902 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003905
3906#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01003907 /* Silently ignore invalid DTLS records as recommended by RFC 6347
3908 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01003909 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3910#endif /* MBEDTLS_SSL_PROTO_DTLS */
3911 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3912 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
3913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003915 }
3916
3917 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003918 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3921 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003922 }
3923
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003924 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3927 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003928 }
3929
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003930 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10003931 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003932 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3935 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003936 }
3937
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003938 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003939 * DTLS-related tests.
3940 * Check epoch before checking length constraint because
3941 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3942 * message gets duplicated before the corresponding Finished message,
3943 * the second ChangeCipherSpec should be discarded because it belongs
3944 * to an old epoch, but not because its length is shorter than
3945 * the minimum record length for packets using the new record transform.
3946 * Note that these two kinds of failures are handled differently,
3947 * as an unexpected record is silently skipped but an invalid
3948 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003949 */
3950#if defined(MBEDTLS_SSL_PROTO_DTLS)
3951 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3952 {
3953 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3954
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003955 /* Check epoch (and sequence number) with DTLS */
3956 if( rec_epoch != ssl->in_epoch )
3957 {
3958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3959 "expected %d, received %d",
3960 ssl->in_epoch, rec_epoch ) );
3961
3962#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3963 /*
3964 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3965 * access the first byte of record content (handshake type), as we
3966 * have an active transform (possibly iv_len != 0), so use the
3967 * fact that the record header len is 13 instead.
3968 */
3969 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3970 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3971 rec_epoch == 0 &&
3972 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3973 ssl->in_left > 13 &&
3974 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3975 {
3976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3977 "from the same port" ) );
3978 return( ssl_handle_possible_reconnect( ssl ) );
3979 }
3980 else
3981#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3982 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3983 }
3984
3985#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3986 /* Replay detection only works for the current epoch */
3987 if( rec_epoch == ssl->in_epoch &&
3988 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3989 {
3990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3991 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3992 }
3993#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01003994
3995 /* Drop unexpected ChangeCipherSpec messages */
3996 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3997 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3998 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3999 {
4000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
4001 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4002 }
4003
4004 /* Drop unexpected ApplicationData records,
4005 * except at the beginning of renegotiations */
4006 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4007 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4008#if defined(MBEDTLS_SSL_RENEGOTIATION)
4009 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4010 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4011#endif
4012 )
4013 {
4014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4015 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4016 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004017 }
4018#endif /* MBEDTLS_SSL_PROTO_DTLS */
4019
Hanno Becker52c6dc62017-05-26 16:07:36 +01004020
4021 /* Check length against bounds of the current transform and version */
4022 if( ssl->transform_in == NULL )
4023 {
4024 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004025 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004026 {
4027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4028 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4029 }
4030 }
4031 else
4032 {
4033 if( ssl->in_msglen < ssl->transform_in->minlen )
4034 {
4035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4036 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4037 }
4038
4039#if defined(MBEDTLS_SSL_PROTO_SSL3)
4040 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004041 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004042 {
4043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4044 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4045 }
4046#endif
4047#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4048 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4049 /*
4050 * TLS encrypted messages can have up to 256 bytes of padding
4051 */
4052 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4053 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004054 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004055 {
4056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4057 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4058 }
4059#endif
4060 }
4061
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004062 return( 0 );
4063}
Paul Bakker5121ce52009-01-03 21:22:43 +00004064
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004065/*
4066 * If applicable, decrypt (and decompress) record content
4067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004068static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004069{
4070 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4073 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4076 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080 ret = mbedtls_ssl_hw_record_read( ssl );
4081 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004083 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4084 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004085 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004086
4087 if( ret == 0 )
4088 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004089 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004091 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004092 {
4093 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004096 return( ret );
4097 }
4098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004100 ssl->in_msg, ssl->in_msglen );
4101
Angus Grattond8213d02016-05-25 20:56:48 +10004102 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4105 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004106 }
4107 }
4108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004110 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004111 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004112 {
4113 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004115 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004116 return( ret );
4117 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004118 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004121#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004122 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004125 }
4126#endif
4127
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004128 return( 0 );
4129}
4130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004131static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004132
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004133/*
4134 * Read a record.
4135 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004136 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4137 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4138 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004141{
4142 int ret;
4143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004145
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004146 if( ssl->keep_current_message == 0 )
4147 {
4148 do {
Simon Butcher99000142016-10-13 17:21:01 +01004149
Hanno Becker90333da2017-10-10 11:27:13 +01004150 do ret = mbedtls_ssl_read_record_layer( ssl );
4151 while( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
4152
4153 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004154 {
4155 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4156 return( ret );
4157 }
4158
4159 ret = mbedtls_ssl_handle_message_type( ssl );
4160
Hanno Becker90333da2017-10-10 11:27:13 +01004161 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4162 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004163
4164 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004165 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004166 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004167 return( ret );
4168 }
4169
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004170 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4171 {
4172 mbedtls_ssl_update_handshake_status( ssl );
4173 }
Simon Butcher99000142016-10-13 17:21:01 +01004174 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004175 else
Simon Butcher99000142016-10-13 17:21:01 +01004176 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4178 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004179 }
4180
4181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4182
4183 return( 0 );
4184}
4185
4186int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4187{
4188 int ret;
4189
Hanno Becker4a810fb2017-05-24 16:27:30 +01004190 /*
4191 * Step A
4192 *
4193 * Consume last content-layer message and potentially
4194 * update in_msglen which keeps track of the contents'
4195 * consumption state.
4196 *
4197 * (1) Handshake messages:
4198 * Remove last handshake message, move content
4199 * and adapt in_msglen.
4200 *
4201 * (2) Alert messages:
4202 * Consume whole record content, in_msglen = 0.
4203 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004204 * (3) Change cipher spec:
4205 * Consume whole record content, in_msglen = 0.
4206 *
4207 * (4) Application data:
4208 * Don't do anything - the record layer provides
4209 * the application data as a stream transport
4210 * and consumes through mbedtls_ssl_read only.
4211 *
4212 */
4213
4214 /* Case (1): Handshake messages */
4215 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004216 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004217 /* Hard assertion to be sure that no application data
4218 * is in flight, as corrupting ssl->in_msglen during
4219 * ssl->in_offt != NULL is fatal. */
4220 if( ssl->in_offt != NULL )
4221 {
4222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4223 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4224 }
4225
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004226 /*
4227 * Get next Handshake message in the current record
4228 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004229
Hanno Becker4a810fb2017-05-24 16:27:30 +01004230 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004231 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004232 * current handshake content: If DTLS handshake
4233 * fragmentation is used, that's the fragment
4234 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004235 * size here is faulty and should be changed at
4236 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004237 * (2) While it doesn't seem to cause problems, one
4238 * has to be very careful not to assume that in_hslen
4239 * is always <= in_msglen in a sensible communication.
4240 * Again, it's wrong for DTLS handshake fragmentation.
4241 * The following check is therefore mandatory, and
4242 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004243 * Additionally, ssl->in_hslen might be arbitrarily out of
4244 * bounds after handling a DTLS message with an unexpected
4245 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004246 */
4247 if( ssl->in_hslen < ssl->in_msglen )
4248 {
4249 ssl->in_msglen -= ssl->in_hslen;
4250 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4251 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004252
Hanno Becker4a810fb2017-05-24 16:27:30 +01004253 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4254 ssl->in_msg, ssl->in_msglen );
4255 }
4256 else
4257 {
4258 ssl->in_msglen = 0;
4259 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004260
Hanno Becker4a810fb2017-05-24 16:27:30 +01004261 ssl->in_hslen = 0;
4262 }
4263 /* Case (4): Application data */
4264 else if( ssl->in_offt != NULL )
4265 {
4266 return( 0 );
4267 }
4268 /* Everything else (CCS & Alerts) */
4269 else
4270 {
4271 ssl->in_msglen = 0;
4272 }
4273
4274 /*
4275 * Step B
4276 *
4277 * Fetch and decode new record if current one is fully consumed.
4278 *
4279 */
4280
4281 if( ssl->in_msglen > 0 )
4282 {
4283 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004284 return( 0 );
4285 }
4286
Hanno Becker4a810fb2017-05-24 16:27:30 +01004287 /* Current record either fully processed or to be discarded. */
4288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004292 return( ret );
4293 }
4294
4295 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004298 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4299 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004300 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004301 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4302 {
4303 /* Skip unexpected record (but not whole datagram) */
4304 ssl->next_record_offset = ssl->in_msglen
4305 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004306
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4308 "(header)" ) );
4309 }
4310 else
4311 {
4312 /* Skip invalid record and the rest of the datagram */
4313 ssl->next_record_offset = 0;
4314 ssl->in_left = 0;
4315
4316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4317 "(header)" ) );
4318 }
4319
4320 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004321 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004322 }
4323#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004324 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004325 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004326
4327 /*
4328 * Read and optionally decrypt the message contents
4329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004330 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4331 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004334 return( ret );
4335 }
4336
4337 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004339 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004342 if( ssl->next_record_offset < ssl->in_left )
4343 {
4344 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4345 }
4346 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004347 else
4348#endif
4349 ssl->in_left = 0;
4350
4351 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004353#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004354 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004355 {
4356 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4358 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004359 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004360 /* Except when waiting for Finished as a bad mac here
4361 * probably means something went wrong in the handshake
4362 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4363 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4364 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4365 {
4366#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4367 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4368 {
4369 mbedtls_ssl_send_alert_message( ssl,
4370 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4371 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4372 }
4373#endif
4374 return( ret );
4375 }
4376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004377#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004378 if( ssl->conf->badmac_limit != 0 &&
4379 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4382 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004383 }
4384#endif
4385
Hanno Becker4a810fb2017-05-24 16:27:30 +01004386 /* As above, invalid records cause
4387 * dismissal of the whole datagram. */
4388
4389 ssl->next_record_offset = 0;
4390 ssl->in_left = 0;
4391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004393 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004394 }
4395
4396 return( ret );
4397 }
4398 else
4399#endif
4400 {
4401 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004402#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4403 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004405 mbedtls_ssl_send_alert_message( ssl,
4406 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4407 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004408 }
4409#endif
4410 return( ret );
4411 }
4412 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004413
Simon Butcher99000142016-10-13 17:21:01 +01004414 return( 0 );
4415}
4416
4417int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4418{
4419 int ret;
4420
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004421 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004422 * Handle particular types of records
4423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004424 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004425 {
Simon Butcher99000142016-10-13 17:21:01 +01004426 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4427 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004428 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004429 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004430 }
4431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004433 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004434 if( ssl->in_msglen != 2 )
4435 {
4436 /* Note: Standard allows for more than one 2 byte alert
4437 to be packed in a single message, but Mbed TLS doesn't
4438 currently support this. */
4439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4440 ssl->in_msglen ) );
4441 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4442 }
4443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004445 ssl->in_msg[0], ssl->in_msg[1] ) );
4446
4447 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004448 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004450 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004453 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004455 }
4456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4458 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4461 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004462 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004463
4464#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4465 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4466 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4467 {
Hanno Becker90333da2017-10-10 11:27:13 +01004468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004469 /* Will be handled when trying to parse ServerHello */
4470 return( 0 );
4471 }
4472#endif
4473
4474#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4475 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4476 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4477 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4478 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4479 {
4480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4481 /* Will be handled in mbedtls_ssl_parse_certificate() */
4482 return( 0 );
4483 }
4484#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4485
4486 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004487 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004488 }
4489
Hanno Beckerc76c6192017-06-06 10:03:17 +01004490#if defined(MBEDTLS_SSL_PROTO_DTLS)
4491 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4492 ssl->handshake != NULL &&
4493 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4494 {
4495 ssl_handshake_wrapup_free_hs_transform( ssl );
4496 }
4497#endif
4498
Paul Bakker5121ce52009-01-03 21:22:43 +00004499 return( 0 );
4500}
4501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004502int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004503{
4504 int ret;
4505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4507 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4508 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004509 {
4510 return( ret );
4511 }
4512
4513 return( 0 );
4514}
4515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004517 unsigned char level,
4518 unsigned char message )
4519{
4520 int ret;
4521
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004522 if( ssl == NULL || ssl->conf == NULL )
4523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004526 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004529 ssl->out_msglen = 2;
4530 ssl->out_msg[0] = level;
4531 ssl->out_msg[1] = message;
4532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004533 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004536 return( ret );
4537 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004539
4540 return( 0 );
4541}
4542
Paul Bakker5121ce52009-01-03 21:22:43 +00004543/*
4544 * Handshake functions
4545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4547 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4548 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4549 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4550 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4551 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4552 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004553/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004554int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004555{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004556 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004560 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4561 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004562 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4563 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004566 ssl->state++;
4567 return( 0 );
4568 }
4569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4571 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004572}
4573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004575{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004580 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4581 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004582 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4583 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004586 ssl->state++;
4587 return( 0 );
4588 }
4589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4591 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004592}
Gilles Peskinef9828522017-05-03 12:28:43 +02004593
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004594#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004595/* Some certificate support -> implement write and parse */
4596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004598{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004600 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004601 const mbedtls_x509_crt *crt;
4602 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004606 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4607 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004608 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4609 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004612 ssl->state++;
4613 return( 0 );
4614 }
4615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004616#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004617 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004618 {
4619 if( ssl->client_auth == 0 )
4620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004622 ssl->state++;
4623 return( 0 );
4624 }
4625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004626#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004627 /*
4628 * If using SSLv3 and got no cert, send an Alert message
4629 * (otherwise an empty Certificate message will be sent).
4630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4632 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004633 {
4634 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4636 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4637 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004640 goto write_msg;
4641 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004642#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004643 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644#endif /* MBEDTLS_SSL_CLI_C */
4645#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004646 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4651 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004652 }
4653 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004654#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004657
4658 /*
4659 * 0 . 0 handshake type
4660 * 1 . 3 handshake length
4661 * 4 . 6 length of all certs
4662 * 7 . 9 length of cert. 1
4663 * 10 . n-1 peer certificate
4664 * n . n+2 length of cert. 2
4665 * n+3 . ... upper level cert, etc.
4666 */
4667 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004668 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004669
Paul Bakker29087132010-03-21 21:03:34 +00004670 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004671 {
4672 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10004673 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10004676 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004677 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004678 }
4679
4680 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4681 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4682 ssl->out_msg[i + 2] = (unsigned char)( n );
4683
4684 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4685 i += n; crt = crt->next;
4686 }
4687
4688 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4689 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4690 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4691
4692 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004693 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4694 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004695
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004696#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004697write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004698#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004699
4700 ssl->state++;
4701
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004702 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004703 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004704 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004705 return( ret );
4706 }
4707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004709
Paul Bakkered27a042013-04-18 22:46:23 +02004710 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004711}
4712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004713int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004714{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004715 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004716 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004718 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004719 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4724 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004725 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4726 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004729 ssl->state++;
4730 return( 0 );
4731 }
4732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004733#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004734 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004735 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4736 {
4737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4738 ssl->state++;
4739 return( 0 );
4740 }
4741
4742#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4743 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4744 authmode = ssl->handshake->sni_authmode;
4745#endif
4746
4747 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4748 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004749 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004750 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004752 ssl->state++;
4753 return( 0 );
4754 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004755#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004757 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004758 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004759 /* mbedtls_ssl_read_record may have sent an alert already. We
4760 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004762 return( ret );
4763 }
4764
4765 ssl->state++;
4766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004767#if defined(MBEDTLS_SSL_SRV_C)
4768#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004769 /*
4770 * Check if the client sent an empty certificate
4771 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004772 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004774 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004775 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004776 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4777 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4778 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004781
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004782 /* The client was asked for a certificate but didn't send
4783 one. The client should know what's going on, so we
4784 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004785 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004786 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004787 return( 0 );
4788 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004790 }
4791 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004792#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4795 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004796 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004799 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4800 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4801 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4802 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004805
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004806 /* The client was asked for a certificate but didn't send
4807 one. The client should know what's going on, so we
4808 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004809 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004810 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004811 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004812 else
4813 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004814 }
4815 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4817 MBEDTLS_SSL_PROTO_TLS1_2 */
4818#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004823 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4824 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004825 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004826 }
4827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4829 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004832 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4833 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004834 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004835 }
4836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004838
Paul Bakker5121ce52009-01-03 21:22:43 +00004839 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004840 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004841 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004842 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004843
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004844 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004848 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4849 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004851 }
4852
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004853 /* In case we tried to reuse a session but it failed */
4854 if( ssl->session_negotiate->peer_cert != NULL )
4855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4857 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004858 }
4859
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004860 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004861 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004862 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004864 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004865 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4866 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004867 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004868 }
4869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004870 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004871
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004872 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004873
4874 while( i < ssl->in_hslen )
4875 {
Philippe Antoine747fd532018-05-30 09:13:21 +02004876 if ( i + 3 > ssl->in_hslen ) {
4877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4878 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4879 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4880 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
4881 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004882 if( ssl->in_msg[i] != 0 )
4883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004885 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4886 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004887 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004888 }
4889
4890 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4891 | (unsigned int) ssl->in_msg[i + 2];
4892 i += 3;
4893
4894 if( n < 128 || i + n > ssl->in_hslen )
4895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004897 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4898 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004899 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004900 }
4901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004903 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004904 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004905 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004906 case 0: /*ok*/
4907 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
4908 /* Ignore certificate with an unknown algorithm: maybe a
4909 prior certificate was already trusted. */
4910 break;
4911
4912 case MBEDTLS_ERR_X509_ALLOC_FAILED:
4913 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
4914 goto crt_parse_der_failed;
4915
4916 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
4917 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
4918 goto crt_parse_der_failed;
4919
4920 default:
4921 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
4922 crt_parse_der_failed:
4923 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004925 return( ret );
4926 }
4927
4928 i += n;
4929 }
4930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004931 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004932
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004933 /*
4934 * On client, make sure the server cert doesn't change during renego to
4935 * avoid "triple handshake" attack: https://secure-resumption.com/
4936 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004937#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004938 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004940 {
4941 if( ssl->session->peer_cert == NULL )
4942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004944 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4945 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004947 }
4948
4949 if( ssl->session->peer_cert->raw.len !=
4950 ssl->session_negotiate->peer_cert->raw.len ||
4951 memcmp( ssl->session->peer_cert->raw.p,
4952 ssl->session_negotiate->peer_cert->raw.p,
4953 ssl->session->peer_cert->raw.len ) != 0 )
4954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004956 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4957 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004958 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004959 }
4960 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004961#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004962
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004963 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004964 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004965 mbedtls_x509_crt *ca_chain;
4966 mbedtls_x509_crl *ca_crl;
4967
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004968#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004969 if( ssl->handshake->sni_ca_chain != NULL )
4970 {
4971 ca_chain = ssl->handshake->sni_ca_chain;
4972 ca_crl = ssl->handshake->sni_ca_crl;
4973 }
4974 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004975#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004976 {
4977 ca_chain = ssl->conf->ca_chain;
4978 ca_crl = ssl->conf->ca_crl;
4979 }
4980
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004981 /*
4982 * Main check: verify certificate
4983 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004984 ret = mbedtls_x509_crt_verify_with_profile(
4985 ssl->session_negotiate->peer_cert,
4986 ca_chain, ca_crl,
4987 ssl->conf->cert_profile,
4988 ssl->hostname,
4989 &ssl->session_negotiate->verify_result,
4990 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004991
4992 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004994 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004995 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004996
4997 /*
4998 * Secondary checks: always done, but change 'ret' only if it was 0
4999 */
5000
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005001#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005003 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005004
5005 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005006 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005007 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005008 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005009 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005012 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005013 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005014 }
5015 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005016#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005018 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005019 ciphersuite_info,
5020 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005021 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005024 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005025 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005026 }
5027
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005028 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5029 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5030 * with details encoded in the verification flags. All other kinds
5031 * of error codes, including those from the user provided f_vrfy
5032 * functions, are treated as fatal and lead to a failure of
5033 * ssl_parse_certificate even if verification was optional. */
5034 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5035 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5036 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5037 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005038 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005039 }
5040
5041 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5042 {
5043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5044 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5045 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005046
5047 if( ret != 0 )
5048 {
5049 /* The certificate may have been rejected for several reasons.
5050 Pick one and send the corresponding alert. Which alert to send
5051 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005052 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5053 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5054 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5055 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5056 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5057 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5058 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5059 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5060 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5061 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5062 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5063 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5064 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5065 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5066 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5067 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5068 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5069 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5070 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5071 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005072 else
5073 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005074 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5075 alert );
5076 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005077
Hanno Beckere6706e62017-05-15 16:05:15 +01005078#if defined(MBEDTLS_DEBUG_C)
5079 if( ssl->session_negotiate->verify_result != 0 )
5080 {
5081 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5082 ssl->session_negotiate->verify_result ) );
5083 }
5084 else
5085 {
5086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5087 }
5088#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005089 }
5090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005092
5093 return( ret );
5094}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005095#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5096 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5097 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5098 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5099 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5100 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5101 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005103int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005104{
5105 int ret;
5106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005110 ssl->out_msglen = 1;
5111 ssl->out_msg[0] = 1;
5112
Paul Bakker5121ce52009-01-03 21:22:43 +00005113 ssl->state++;
5114
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005115 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005116 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005117 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005118 return( ret );
5119 }
5120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005122
5123 return( 0 );
5124}
5125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005127{
5128 int ret;
5129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005135 return( ret );
5136 }
5137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005138 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005141 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5142 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005143 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005144 }
5145
5146 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005149 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5150 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005151 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005152 }
5153
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005154 /*
5155 * Switch to our negotiated transform and session parameters for inbound
5156 * data.
5157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005159 ssl->transform_in = ssl->transform_negotiate;
5160 ssl->session_in = ssl->session_negotiate;
5161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005163 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005165#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005166 ssl_dtls_replay_reset( ssl );
5167#endif
5168
5169 /* Increment epoch */
5170 if( ++ssl->in_epoch == 0 )
5171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005173 /* This is highly unlikely to happen for legitimate reasons, so
5174 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005175 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005176 }
5177 }
5178 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005179#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005180 memset( ssl->in_ctr, 0, 8 );
5181
5182 /*
5183 * Set the in_msg pointer to the correct location based on IV length
5184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005185 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005186 {
5187 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
5188 ssl->transform_negotiate->fixed_ivlen;
5189 }
5190 else
5191 ssl->in_msg = ssl->in_iv;
5192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005193#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5194 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005196 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005198 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005199 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5200 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005201 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005202 }
5203 }
5204#endif
5205
Paul Bakker5121ce52009-01-03 21:22:43 +00005206 ssl->state++;
5207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005209
5210 return( 0 );
5211}
5212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005213void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5214 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005215{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005216 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5219 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5220 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005221 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005222 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005223#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005224#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5225#if defined(MBEDTLS_SHA512_C)
5226 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005227 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5228 else
5229#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005230#if defined(MBEDTLS_SHA256_C)
5231 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005232 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005233 else
5234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005235#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005238 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005239 }
Paul Bakker380da532012-04-18 16:10:25 +00005240}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005242void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005244#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5245 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005246 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5247 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005248#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005249#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5250#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005251 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005252#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005253#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005254 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005255#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005256#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005257}
5258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005259static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005260 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005262#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5263 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005264 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5265 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005266#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005267#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5268#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005269 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005270#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005271#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005272 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005273#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005275}
5276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5278 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5279static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005280 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005281{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005282 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5283 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005284}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005285#endif
Paul Bakker380da532012-04-18 16:10:25 +00005286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005287#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5288#if defined(MBEDTLS_SHA256_C)
5289static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005290 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005291{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005292 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005293}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005294#endif
Paul Bakker380da532012-04-18 16:10:25 +00005295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296#if defined(MBEDTLS_SHA512_C)
5297static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005298 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005299{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005300 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005301}
Paul Bakker769075d2012-11-24 11:26:46 +01005302#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005306static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005307 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005308{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005309 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005310 mbedtls_md5_context md5;
5311 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005312
Paul Bakker5121ce52009-01-03 21:22:43 +00005313 unsigned char padbuf[48];
5314 unsigned char md5sum[16];
5315 unsigned char sha1sum[20];
5316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005318 if( !session )
5319 session = ssl->session;
5320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005322
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005323 mbedtls_md5_init( &md5 );
5324 mbedtls_sha1_init( &sha1 );
5325
5326 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5327 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005328
5329 /*
5330 * SSLv3:
5331 * hash =
5332 * MD5( master + pad2 +
5333 * MD5( handshake + sender + master + pad1 ) )
5334 * + SHA1( master + pad2 +
5335 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005336 */
5337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005338#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005339 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5340 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005341#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005344 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5345 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005346#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005349 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005350
Paul Bakker1ef83d62012-04-11 12:09:53 +00005351 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005352
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005353 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5354 mbedtls_md5_update_ret( &md5, session->master, 48 );
5355 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5356 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005357
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005358 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5359 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5360 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5361 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005362
Paul Bakker1ef83d62012-04-11 12:09:53 +00005363 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005364
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005365 mbedtls_md5_starts_ret( &md5 );
5366 mbedtls_md5_update_ret( &md5, session->master, 48 );
5367 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5368 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5369 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005370
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005371 mbedtls_sha1_starts_ret( &sha1 );
5372 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5373 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5374 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5375 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005378
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005379 mbedtls_md5_free( &md5 );
5380 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005381
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005382 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5383 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
5384 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005387}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005391static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005393{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005394 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005395 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005396 mbedtls_md5_context md5;
5397 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005398 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005400 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005401 if( !session )
5402 session = ssl->session;
5403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005405
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005406 mbedtls_md5_init( &md5 );
5407 mbedtls_sha1_init( &sha1 );
5408
5409 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5410 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005411
Paul Bakker1ef83d62012-04-11 12:09:53 +00005412 /*
5413 * TLSv1:
5414 * hash = PRF( master, finished_label,
5415 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5416 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005419 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5420 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005421#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005423#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005424 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5425 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005426#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005429 ? "client finished"
5430 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005431
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005432 mbedtls_md5_finish_ret( &md5, padbuf );
5433 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005434
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005435 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005436 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005438 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005439
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005440 mbedtls_md5_free( &md5 );
5441 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005442
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005443 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005446}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005447#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005449#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5450#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005451static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005453{
5454 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005455 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005456 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005457 unsigned char padbuf[32];
5458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005459 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005460 if( !session )
5461 session = ssl->session;
5462
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005463 mbedtls_sha256_init( &sha256 );
5464
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005466
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005467 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005468
5469 /*
5470 * TLSv1.2:
5471 * hash = PRF( master, finished_label,
5472 * Hash( handshake ) )[0.11]
5473 */
5474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475#if !defined(MBEDTLS_SHA256_ALT)
5476 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005477 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005478#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005481 ? "client finished"
5482 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005483
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005484 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005485
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005486 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005487 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005490
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005491 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005492
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005493 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005496}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005500static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005502{
5503 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005504 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005505 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005506 unsigned char padbuf[48];
5507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005509 if( !session )
5510 session = ssl->session;
5511
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005512 mbedtls_sha512_init( &sha512 );
5513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005515
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005516 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005517
5518 /*
5519 * TLSv1.2:
5520 * hash = PRF( master, finished_label,
5521 * Hash( handshake ) )[0.11]
5522 */
5523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005525 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5526 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005527#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005530 ? "client finished"
5531 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005532
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005533 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005534
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005535 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005536 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005539
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005540 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005541
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005542 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546#endif /* MBEDTLS_SHA512_C */
5547#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005550{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005551 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005552
5553 /*
5554 * Free our handshake params
5555 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02005556 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005558 ssl->handshake = NULL;
5559
5560 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005561 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005562 */
5563 if( ssl->transform )
5564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565 mbedtls_ssl_transform_free( ssl->transform );
5566 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005567 }
5568 ssl->transform = ssl->transform_negotiate;
5569 ssl->transform_negotiate = NULL;
5570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005572}
5573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005575{
5576 int resume = ssl->handshake->resume;
5577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005578 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580#if defined(MBEDTLS_SSL_RENEGOTIATION)
5581 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005584 ssl->renego_records_seen = 0;
5585 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005586#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005587
5588 /*
5589 * Free the previous session and switch in the current one
5590 */
Paul Bakker0a597072012-09-25 21:55:46 +00005591 if( ssl->session )
5592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005594 /* RFC 7366 3.1: keep the EtM state */
5595 ssl->session_negotiate->encrypt_then_mac =
5596 ssl->session->encrypt_then_mac;
5597#endif
5598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599 mbedtls_ssl_session_free( ssl->session );
5600 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005601 }
5602 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005603 ssl->session_negotiate = NULL;
5604
Paul Bakker0a597072012-09-25 21:55:46 +00005605 /*
5606 * Add cache entry
5607 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005608 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005609 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005610 resume == 0 )
5611 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005612 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005614 }
Paul Bakker0a597072012-09-25 21:55:46 +00005615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005617 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005618 ssl->handshake->flight != NULL )
5619 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005620 /* Cancel handshake timer */
5621 ssl_set_timer( ssl, 0 );
5622
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005623 /* Keep last flight around in case we need to resend it:
5624 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005626 }
5627 else
5628#endif
5629 ssl_handshake_wrapup_free_hs_transform( ssl );
5630
Paul Bakker48916f92012-09-16 19:57:18 +00005631 ssl->state++;
5632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005634}
5635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005637{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005638 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005641
Paul Bakker92be97b2013-01-02 17:30:03 +01005642 /*
5643 * Set the out_msg pointer to the correct location based on IV length
5644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005646 {
5647 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5648 ssl->transform_negotiate->fixed_ivlen;
5649 }
5650 else
5651 ssl->out_msg = ssl->out_iv;
5652
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005653 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005654
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005655 /*
5656 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5657 * may define some other value. Currently (early 2016), no defined
5658 * ciphersuite does this (and this is unlikely to change as activity has
5659 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005663#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005664 ssl->verify_data_len = hash_len;
5665 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005666#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005667
Paul Bakker5121ce52009-01-03 21:22:43 +00005668 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005669 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5670 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005671
5672 /*
5673 * In case of session resuming, invert the client and server
5674 * ChangeCipherSpec messages order.
5675 */
Paul Bakker0a597072012-09-25 21:55:46 +00005676 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005678#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005679 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005680 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005681#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005683 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005684 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005685#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005686 }
5687 else
5688 ssl->state++;
5689
Paul Bakker48916f92012-09-16 19:57:18 +00005690 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005691 * Switch to our negotiated transform and session parameters for outbound
5692 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005696#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005697 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005698 {
5699 unsigned char i;
5700
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005701 /* Remember current epoch settings for resending */
5702 ssl->handshake->alt_transform_out = ssl->transform_out;
5703 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5704
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005705 /* Set sequence_number to zero */
5706 memset( ssl->out_ctr + 2, 0, 6 );
5707
5708 /* Increment epoch */
5709 for( i = 2; i > 0; i-- )
5710 if( ++ssl->out_ctr[i - 1] != 0 )
5711 break;
5712
5713 /* The loop goes to its end iff the counter is wrapping */
5714 if( i == 0 )
5715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5717 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005718 }
5719 }
5720 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005722 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005723
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005724 ssl->transform_out = ssl->transform_negotiate;
5725 ssl->session_out = ssl->session_negotiate;
5726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5728 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5733 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005734 }
5735 }
5736#endif
5737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005739 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005741#endif
5742
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005743 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005744 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005745 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005746 return( ret );
5747 }
5748
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005749#if defined(MBEDTLS_SSL_PROTO_DTLS)
5750 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5751 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
5752 {
5753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
5754 return( ret );
5755 }
5756#endif
5757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005759
5760 return( 0 );
5761}
5762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005764#define SSL_MAX_HASH_LEN 36
5765#else
5766#define SSL_MAX_HASH_LEN 12
5767#endif
5768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005770{
Paul Bakker23986e52011-04-24 08:57:21 +00005771 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005772 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005773 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005776
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005777 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005781 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005782 return( ret );
5783 }
5784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005788 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5789 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005791 }
5792
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005793 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005794#if defined(MBEDTLS_SSL_PROTO_SSL3)
5795 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005796 hash_len = 36;
5797 else
5798#endif
5799 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005801 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5802 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005805 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5806 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005808 }
5809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005811 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005814 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5815 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005816 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005817 }
5818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005820 ssl->verify_data_len = hash_len;
5821 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005822#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005823
Paul Bakker0a597072012-09-25 21:55:46 +00005824 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005826#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005827 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005828 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005829#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005831 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005833#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005834 }
5835 else
5836 ssl->state++;
5837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005839 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005841#endif
5842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005844
5845 return( 0 );
5846}
5847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005849{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005850 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005852#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5853 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5854 mbedtls_md5_init( &handshake->fin_md5 );
5855 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005856 mbedtls_md5_starts_ret( &handshake->fin_md5 );
5857 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005858#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5860#if defined(MBEDTLS_SHA256_C)
5861 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005862 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005863#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864#if defined(MBEDTLS_SHA512_C)
5865 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005866 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005867#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005869
5870 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005871
5872#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5873 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5874 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5875#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877#if defined(MBEDTLS_DHM_C)
5878 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005879#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880#if defined(MBEDTLS_ECDH_C)
5881 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005882#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02005883#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005884 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02005885#if defined(MBEDTLS_SSL_CLI_C)
5886 handshake->ecjpake_cache = NULL;
5887 handshake->ecjpake_cache_len = 0;
5888#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02005889#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005890
5891#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5892 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5893#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005894}
5895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005896static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005897{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005898 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5901 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005903 mbedtls_md_init( &transform->md_ctx_enc );
5904 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005905}
5906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005907void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005908{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005910}
5911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005913{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005914 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005915 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005917 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005918 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005919 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02005920 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005921
5922 /*
5923 * Either the pointers are now NULL or cleared properly and can be freed.
5924 * Now allocate missing structures.
5925 */
5926 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005927 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005928 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005929 }
Paul Bakker48916f92012-09-16 19:57:18 +00005930
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005931 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005932 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005933 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005934 }
Paul Bakker48916f92012-09-16 19:57:18 +00005935
Paul Bakker82788fb2014-10-20 13:59:19 +02005936 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005937 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005938 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005939 }
Paul Bakker48916f92012-09-16 19:57:18 +00005940
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005941 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005942 if( ssl->handshake == NULL ||
5943 ssl->transform_negotiate == NULL ||
5944 ssl->session_negotiate == NULL )
5945 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948 mbedtls_free( ssl->handshake );
5949 mbedtls_free( ssl->transform_negotiate );
5950 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005951
5952 ssl->handshake = NULL;
5953 ssl->transform_negotiate = NULL;
5954 ssl->session_negotiate = NULL;
5955
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005956 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005957 }
5958
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005959 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005960 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005961 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005962 ssl_handshake_params_init( ssl->handshake );
5963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005965 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5966 {
5967 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005968
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005969 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5970 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5971 else
5972 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005973
5974 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005975 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005976#endif
5977
Paul Bakker48916f92012-09-16 19:57:18 +00005978 return( 0 );
5979}
5980
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005981#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005982/* Dummy cookie callbacks for defaults */
5983static int ssl_cookie_write_dummy( void *ctx,
5984 unsigned char **p, unsigned char *end,
5985 const unsigned char *cli_id, size_t cli_id_len )
5986{
5987 ((void) ctx);
5988 ((void) p);
5989 ((void) end);
5990 ((void) cli_id);
5991 ((void) cli_id_len);
5992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005994}
5995
5996static int ssl_cookie_check_dummy( void *ctx,
5997 const unsigned char *cookie, size_t cookie_len,
5998 const unsigned char *cli_id, size_t cli_id_len )
5999{
6000 ((void) ctx);
6001 ((void) cookie);
6002 ((void) cookie_len);
6003 ((void) cli_id);
6004 ((void) cli_id_len);
6005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006006 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006007}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006008#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006009
Paul Bakker5121ce52009-01-03 21:22:43 +00006010/*
6011 * Initialize an SSL context
6012 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006013void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6014{
6015 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6016}
6017
6018/*
6019 * Setup an SSL context
6020 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006021int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006022 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006023{
Paul Bakker48916f92012-09-16 19:57:18 +00006024 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006025
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006026 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006027
6028 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006029 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006030 */
Angus Grattond8213d02016-05-25 20:56:48 +10006031 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6032 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006033 {
Angus Grattond8213d02016-05-25 20:56:48 +10006034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6035 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6036 }
6037
6038 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6039 if( ssl->out_buf == NULL )
6040 {
6041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006042 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006043 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006044 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006045 }
6046
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006047#if defined(MBEDTLS_SSL_PROTO_DTLS)
6048 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6049 {
6050 ssl->out_hdr = ssl->out_buf;
6051 ssl->out_ctr = ssl->out_buf + 3;
6052 ssl->out_len = ssl->out_buf + 11;
6053 ssl->out_iv = ssl->out_buf + 13;
6054 ssl->out_msg = ssl->out_buf + 13;
6055
6056 ssl->in_hdr = ssl->in_buf;
6057 ssl->in_ctr = ssl->in_buf + 3;
6058 ssl->in_len = ssl->in_buf + 11;
6059 ssl->in_iv = ssl->in_buf + 13;
6060 ssl->in_msg = ssl->in_buf + 13;
6061 }
6062 else
6063#endif
6064 {
6065 ssl->out_ctr = ssl->out_buf;
6066 ssl->out_hdr = ssl->out_buf + 8;
6067 ssl->out_len = ssl->out_buf + 11;
6068 ssl->out_iv = ssl->out_buf + 13;
6069 ssl->out_msg = ssl->out_buf + 13;
6070
6071 ssl->in_ctr = ssl->in_buf;
6072 ssl->in_hdr = ssl->in_buf + 8;
6073 ssl->in_len = ssl->in_buf + 11;
6074 ssl->in_iv = ssl->in_buf + 13;
6075 ssl->in_msg = ssl->in_buf + 13;
6076 }
6077
Paul Bakker48916f92012-09-16 19:57:18 +00006078 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6079 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006080
6081 return( 0 );
6082}
6083
6084/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006085 * Reset an initialized and used SSL context for re-use while retaining
6086 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006087 *
6088 * If partial is non-zero, keep data in the input buffer and client ID.
6089 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006090 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006091static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006092{
Paul Bakker48916f92012-09-16 19:57:18 +00006093 int ret;
6094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006095 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006096
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006097 /* Cancel any possibly running timer */
6098 ssl_set_timer( ssl, 0 );
6099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100#if defined(MBEDTLS_SSL_RENEGOTIATION)
6101 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006102 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006103
6104 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6106 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006107#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006109
Paul Bakker7eb013f2011-10-06 12:37:39 +00006110 ssl->in_offt = NULL;
6111
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006112 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006113 ssl->in_msgtype = 0;
6114 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006115 if( partial == 0 )
6116 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006117#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006118 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006119 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006122 ssl_dtls_replay_reset( ssl );
6123#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006124
6125 ssl->in_hslen = 0;
6126 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006127
6128 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006129
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006130 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006131 ssl->out_msgtype = 0;
6132 ssl->out_msglen = 0;
6133 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6135 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006136 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006137#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006138
Paul Bakker48916f92012-09-16 19:57:18 +00006139 ssl->transform_in = NULL;
6140 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006141
Hanno Becker78640902018-08-13 16:35:15 +01006142 ssl->session_in = NULL;
6143 ssl->session_out = NULL;
6144
Angus Grattond8213d02016-05-25 20:56:48 +10006145 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006146 if( partial == 0 )
Angus Grattond8213d02016-05-25 20:56:48 +10006147 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00006148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6150 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6153 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6156 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006157 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006158 }
6159#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006160
Paul Bakker48916f92012-09-16 19:57:18 +00006161 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163 mbedtls_ssl_transform_free( ssl->transform );
6164 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006165 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006166 }
Paul Bakker48916f92012-09-16 19:57:18 +00006167
Paul Bakkerc0463502013-02-14 11:19:38 +01006168 if( ssl->session )
6169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170 mbedtls_ssl_session_free( ssl->session );
6171 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006172 ssl->session = NULL;
6173 }
6174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006176 ssl->alpn_chosen = NULL;
6177#endif
6178
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006179#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006180 if( partial == 0 )
6181 {
6182 mbedtls_free( ssl->cli_id );
6183 ssl->cli_id = NULL;
6184 ssl->cli_id_len = 0;
6185 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006186#endif
6187
Paul Bakker48916f92012-09-16 19:57:18 +00006188 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6189 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006190
6191 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006192}
6193
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006194/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006195 * Reset an initialized and used SSL context for re-use while retaining
6196 * all application-set variables, function pointers and data.
6197 */
6198int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6199{
6200 return( ssl_session_reset_int( ssl, 0 ) );
6201}
6202
6203/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006204 * SSL set accessors
6205 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006206void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006207{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006208 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006209}
6210
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006211void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006212{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006213 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006214}
6215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006216#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006217void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006218{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006219 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006220}
6221#endif
6222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006224void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006225{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006226 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006227}
6228#endif
6229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006231void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006232{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006233 conf->hs_timeout_min = min;
6234 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006235}
6236#endif
6237
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006238void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006239{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006240 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006241}
6242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006244void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006245 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006246 void *p_vrfy )
6247{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006248 conf->f_vrfy = f_vrfy;
6249 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006252
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006253void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006254 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006255 void *p_rng )
6256{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006257 conf->f_rng = f_rng;
6258 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006259}
6260
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006261void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006262 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006263 void *p_dbg )
6264{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006265 conf->f_dbg = f_dbg;
6266 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006267}
6268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006270 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006271 mbedtls_ssl_send_t *f_send,
6272 mbedtls_ssl_recv_t *f_recv,
6273 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006274{
6275 ssl->p_bio = p_bio;
6276 ssl->f_send = f_send;
6277 ssl->f_recv = f_recv;
6278 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006279}
6280
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02006281#if defined(MBEDTLS_SSL_PROTO_DTLS)
6282void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
6283{
6284 ssl->mtu = mtu;
6285}
6286#endif
6287
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006288void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006289{
6290 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006291}
6292
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006293void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6294 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006295 mbedtls_ssl_set_timer_t *f_set_timer,
6296 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006297{
6298 ssl->p_timer = p_timer;
6299 ssl->f_set_timer = f_set_timer;
6300 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006301
6302 /* Make sure we start with no timer running */
6303 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006304}
6305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006307void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006308 void *p_cache,
6309 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6310 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006311{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006312 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006313 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006314 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006315}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318#if defined(MBEDTLS_SSL_CLI_C)
6319int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006320{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006321 int ret;
6322
6323 if( ssl == NULL ||
6324 session == NULL ||
6325 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006326 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006328 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006329 }
6330
6331 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6332 return( ret );
6333
Paul Bakker0a597072012-09-25 21:55:46 +00006334 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006335
6336 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006337}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006338#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006339
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006340void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006341 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006342{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006343 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6344 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6345 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6346 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006347}
6348
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006349void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006350 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006351 int major, int minor )
6352{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006353 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006354 return;
6355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006357 return;
6358
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006359 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006360}
6361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006363void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006364 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006365{
6366 conf->cert_profile = profile;
6367}
6368
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006369/* Append a new keycert entry to a (possibly empty) list */
6370static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6371 mbedtls_x509_crt *cert,
6372 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006373{
niisato8ee24222018-06-25 19:05:48 +09006374 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006375
niisato8ee24222018-06-25 19:05:48 +09006376 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6377 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006378 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006379
niisato8ee24222018-06-25 19:05:48 +09006380 new_cert->cert = cert;
6381 new_cert->key = key;
6382 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006383
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006384 /* Update head is the list was null, else add to the end */
6385 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006386 {
niisato8ee24222018-06-25 19:05:48 +09006387 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006388 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006389 else
6390 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006391 mbedtls_ssl_key_cert *cur = *head;
6392 while( cur->next != NULL )
6393 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09006394 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006395 }
6396
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006397 return( 0 );
6398}
6399
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006400int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006401 mbedtls_x509_crt *own_cert,
6402 mbedtls_pk_context *pk_key )
6403{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006404 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006405}
6406
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006407void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006408 mbedtls_x509_crt *ca_chain,
6409 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006410{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006411 conf->ca_chain = ca_chain;
6412 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006413}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006415
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006416#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6417int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6418 mbedtls_x509_crt *own_cert,
6419 mbedtls_pk_context *pk_key )
6420{
6421 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6422 own_cert, pk_key ) );
6423}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006424
6425void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6426 mbedtls_x509_crt *ca_chain,
6427 mbedtls_x509_crl *ca_crl )
6428{
6429 ssl->handshake->sni_ca_chain = ca_chain;
6430 ssl->handshake->sni_ca_crl = ca_crl;
6431}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006432
6433void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6434 int authmode )
6435{
6436 ssl->handshake->sni_authmode = authmode;
6437}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006438#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6439
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006440#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006441/*
6442 * Set EC J-PAKE password for current handshake
6443 */
6444int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6445 const unsigned char *pw,
6446 size_t pw_len )
6447{
6448 mbedtls_ecjpake_role role;
6449
Janos Follath8eb64132016-06-03 15:40:57 +01006450 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6452
6453 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6454 role = MBEDTLS_ECJPAKE_SERVER;
6455 else
6456 role = MBEDTLS_ECJPAKE_CLIENT;
6457
6458 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6459 role,
6460 MBEDTLS_MD_SHA256,
6461 MBEDTLS_ECP_DP_SECP256R1,
6462 pw, pw_len ) );
6463}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006464#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006467int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006468 const unsigned char *psk, size_t psk_len,
6469 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006470{
Paul Bakker6db455e2013-09-18 17:29:31 +02006471 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006476
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006477 /* Identity len will be encoded on two bytes */
6478 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10006479 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006480 {
6481 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6482 }
6483
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006484 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006485 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006486 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006487
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006488 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006489 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006490 conf->psk_len = 0;
6491 }
6492 if( conf->psk_identity != NULL )
6493 {
6494 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006495 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006496 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006497 }
6498
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006499 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6500 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006501 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006502 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006503 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006504 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006505 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006506 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006507 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006508
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006509 conf->psk_len = psk_len;
6510 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006511
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006512 memcpy( conf->psk, psk, conf->psk_len );
6513 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006514
6515 return( 0 );
6516}
6517
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006518int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6519 const unsigned char *psk, size_t psk_len )
6520{
6521 if( psk == NULL || ssl->handshake == NULL )
6522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6523
6524 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6526
6527 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006528 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006529 mbedtls_platform_zeroize( ssl->handshake->psk,
6530 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006531 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006532 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006533 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006534
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006535 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006536 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006537
6538 ssl->handshake->psk_len = psk_len;
6539 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6540
6541 return( 0 );
6542}
6543
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006544void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006546 size_t),
6547 void *p_psk )
6548{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006549 conf->f_psk = f_psk;
6550 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006551}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006553
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006554#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006555
6556#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006557int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006558{
6559 int ret;
6560
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006561 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6562 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6563 {
6564 mbedtls_mpi_free( &conf->dhm_P );
6565 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006566 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006567 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006568
6569 return( 0 );
6570}
Hanno Becker470a8c42017-10-04 15:28:46 +01006571#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006572
Hanno Beckera90658f2017-10-04 15:29:08 +01006573int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6574 const unsigned char *dhm_P, size_t P_len,
6575 const unsigned char *dhm_G, size_t G_len )
6576{
6577 int ret;
6578
6579 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6580 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6581 {
6582 mbedtls_mpi_free( &conf->dhm_P );
6583 mbedtls_mpi_free( &conf->dhm_G );
6584 return( ret );
6585 }
6586
6587 return( 0 );
6588}
Paul Bakker5121ce52009-01-03 21:22:43 +00006589
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006590int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006591{
6592 int ret;
6593
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006594 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6595 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6596 {
6597 mbedtls_mpi_free( &conf->dhm_P );
6598 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006599 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006600 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006601
6602 return( 0 );
6603}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006604#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006605
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006606#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6607/*
6608 * Set the minimum length for Diffie-Hellman parameters
6609 */
6610void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6611 unsigned int bitlen )
6612{
6613 conf->dhm_min_bitlen = bitlen;
6614}
6615#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6616
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006617#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006618/*
6619 * Set allowed/preferred hashes for handshake signatures
6620 */
6621void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6622 const int *hashes )
6623{
6624 conf->sig_hashes = hashes;
6625}
Hanno Becker947194e2017-04-07 13:25:49 +01006626#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006627
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006628#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006629/*
6630 * Set the allowed elliptic curves
6631 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006632void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006633 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006634{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006635 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006636}
Hanno Becker947194e2017-04-07 13:25:49 +01006637#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006638
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006639#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006640int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006641{
Hanno Becker947194e2017-04-07 13:25:49 +01006642 /* Initialize to suppress unnecessary compiler warning */
6643 size_t hostname_len = 0;
6644
6645 /* Check if new hostname is valid before
6646 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006647 if( hostname != NULL )
6648 {
6649 hostname_len = strlen( hostname );
6650
6651 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6653 }
6654
6655 /* Now it's clear that we will overwrite the old hostname,
6656 * so we can free it safely */
6657
6658 if( ssl->hostname != NULL )
6659 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006660 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01006661 mbedtls_free( ssl->hostname );
6662 }
6663
6664 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006665
Paul Bakker5121ce52009-01-03 21:22:43 +00006666 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006667 {
6668 ssl->hostname = NULL;
6669 }
6670 else
6671 {
6672 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006673 if( ssl->hostname == NULL )
6674 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006675
Hanno Becker947194e2017-04-07 13:25:49 +01006676 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006677
Hanno Becker947194e2017-04-07 13:25:49 +01006678 ssl->hostname[hostname_len] = '\0';
6679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006680
6681 return( 0 );
6682}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006683#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006684
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006685#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006686void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006688 const unsigned char *, size_t),
6689 void *p_sni )
6690{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006691 conf->f_sni = f_sni;
6692 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006693}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006697int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006698{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006699 size_t cur_len, tot_len;
6700 const char **p;
6701
6702 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006703 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6704 * MUST NOT be truncated."
6705 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006706 */
6707 tot_len = 0;
6708 for( p = protos; *p != NULL; p++ )
6709 {
6710 cur_len = strlen( *p );
6711 tot_len += cur_len;
6712
6713 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006715 }
6716
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006717 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006718
6719 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006720}
6721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006723{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006724 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006727
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006728void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006729{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006730 conf->max_major_ver = major;
6731 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006732}
6733
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006734void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006735{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006736 conf->min_major_ver = major;
6737 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006738}
6739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006741void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006742{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006743 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006744}
6745#endif
6746
Janos Follath088ce432017-04-10 12:42:31 +01006747#if defined(MBEDTLS_SSL_SRV_C)
6748void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6749 char cert_req_ca_list )
6750{
6751 conf->cert_req_ca_list = cert_req_ca_list;
6752}
6753#endif
6754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006756void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006757{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006758 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006759}
6760#endif
6761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006763void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006764{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006765 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006766}
6767#endif
6768
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006769#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006770void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006771{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006772 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006773}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006774#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006777int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006778{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10006780 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006783 }
6784
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006785 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006786
6787 return( 0 );
6788}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006792void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006793{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006794 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006795}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006798#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006799void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006800{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006801 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006802}
6803#endif
6804
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006805void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006806{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006807 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006808}
6809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006811void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006812{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006813 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006814}
6815
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006816void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006817{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006818 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006819}
6820
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006821void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006822 const unsigned char period[8] )
6823{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006824 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006825}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006829#if defined(MBEDTLS_SSL_CLI_C)
6830void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006831{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006832 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006833}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006834#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006835
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006836#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006837void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6838 mbedtls_ssl_ticket_write_t *f_ticket_write,
6839 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6840 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006841{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006842 conf->f_ticket_write = f_ticket_write;
6843 conf->f_ticket_parse = f_ticket_parse;
6844 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006845}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006846#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006848
Robert Cragie4feb7ae2015-10-02 13:33:37 +01006849#if defined(MBEDTLS_SSL_EXPORT_KEYS)
6850void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
6851 mbedtls_ssl_export_keys_t *f_export_keys,
6852 void *p_export_keys )
6853{
6854 conf->f_export_keys = f_export_keys;
6855 conf->p_export_keys = p_export_keys;
6856}
6857#endif
6858
Gilles Peskineb74a1c72018-04-24 13:09:22 +02006859#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006860void mbedtls_ssl_conf_async_private_cb(
6861 mbedtls_ssl_config *conf,
6862 mbedtls_ssl_async_sign_t *f_async_sign,
6863 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
6864 mbedtls_ssl_async_resume_t *f_async_resume,
6865 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006866 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006867{
6868 conf->f_async_sign_start = f_async_sign;
6869 conf->f_async_decrypt_start = f_async_decrypt;
6870 conf->f_async_resume = f_async_resume;
6871 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006872 conf->p_async_config_data = async_config_data;
6873}
6874
Gilles Peskine8f97af72018-04-26 11:46:10 +02006875void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
6876{
6877 return( conf->p_async_config_data );
6878}
6879
Gilles Peskine1febfef2018-04-30 11:54:39 +02006880void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006881{
6882 if( ssl->handshake == NULL )
6883 return( NULL );
6884 else
6885 return( ssl->handshake->user_async_ctx );
6886}
6887
Gilles Peskine1febfef2018-04-30 11:54:39 +02006888void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006889 void *ctx )
6890{
6891 if( ssl->handshake != NULL )
6892 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006893}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02006894#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01006895
Paul Bakker5121ce52009-01-03 21:22:43 +00006896/*
6897 * SSL get accessors
6898 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006900{
6901 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6902}
6903
Hanno Becker8b170a02017-10-10 11:51:19 +01006904int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
6905{
6906 /*
6907 * Case A: We're currently holding back
6908 * a message for further processing.
6909 */
6910
6911 if( ssl->keep_current_message == 1 )
6912 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006913 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006914 return( 1 );
6915 }
6916
6917 /*
6918 * Case B: Further records are pending in the current datagram.
6919 */
6920
6921#if defined(MBEDTLS_SSL_PROTO_DTLS)
6922 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6923 ssl->in_left > ssl->next_record_offset )
6924 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006925 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006926 return( 1 );
6927 }
6928#endif /* MBEDTLS_SSL_PROTO_DTLS */
6929
6930 /*
6931 * Case C: A handshake message is being processed.
6932 */
6933
Hanno Becker8b170a02017-10-10 11:51:19 +01006934 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
6935 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006936 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006937 return( 1 );
6938 }
6939
6940 /*
6941 * Case D: An application data message is being processed
6942 */
6943 if( ssl->in_offt != NULL )
6944 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01006945 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01006946 return( 1 );
6947 }
6948
6949 /*
6950 * In all other cases, the rest of the message can be dropped.
6951 * As in ssl_read_record_layer, this needs to be adapted if
6952 * we implement support for multiple alerts in single records.
6953 */
6954
6955 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
6956 return( 0 );
6957}
6958
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006959uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006960{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006961 if( ssl->session != NULL )
6962 return( ssl->session->verify_result );
6963
6964 if( ssl->session_negotiate != NULL )
6965 return( ssl->session_negotiate->verify_result );
6966
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006967 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006968}
6969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006971{
Paul Bakker926c8e42013-03-06 10:23:34 +01006972 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006973 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006976}
6977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006979{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006981 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006982 {
6983 switch( ssl->minor_ver )
6984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006986 return( "DTLSv1.0" );
6987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006989 return( "DTLSv1.2" );
6990
6991 default:
6992 return( "unknown (DTLS)" );
6993 }
6994 }
6995#endif
6996
Paul Bakker43ca69c2011-01-15 17:35:19 +00006997 switch( ssl->minor_ver )
6998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007000 return( "SSLv3.0" );
7001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007003 return( "TLSv1.0" );
7004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007006 return( "TLSv1.1" );
7007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007009 return( "TLSv1.2" );
7010
Paul Bakker43ca69c2011-01-15 17:35:19 +00007011 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007012 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007013 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007014}
7015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007017{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007018 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007020 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007021
Hanno Becker78640902018-08-13 16:35:15 +01007022 if( transform == NULL )
7023 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025#if defined(MBEDTLS_ZLIB_SUPPORT)
7026 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7027 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007028 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007029#endif
7030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007033 case MBEDTLS_MODE_GCM:
7034 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007035 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007037 transform_expansion = transform->minlen;
7038 break;
7039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007040 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007041
7042 block_size = mbedtls_cipher_get_block_size(
7043 &transform->cipher_ctx_enc );
7044
7045#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7046 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7047 {
7048 /* Expansion due to addition of
7049 * - MAC
7050 * - CBC padding (theoretically up to 256 bytes, but
7051 * we never use more than block_size)
7052 * - explicit IV
7053 */
7054 transform_expansion = transform->maclen + 2 * block_size;
7055 }
7056 else
7057#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7058 {
7059 /* No explicit IV prior to TLS 1.1. */
7060 transform_expansion = transform->maclen + block_size;
7061 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007062 break;
7063
7064 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007067 }
7068
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007069 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007070}
7071
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007072#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7073size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7074{
7075 size_t max_len;
7076
7077 /*
7078 * Assume mfl_code is correct since it was checked when set
7079 */
Angus Grattond8213d02016-05-25 20:56:48 +10007080 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007081
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007082 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007083 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007084 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007085 {
Angus Grattond8213d02016-05-25 20:56:48 +10007086 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007087 }
7088
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007089 /* During a handshake, use the value being negotiated */
7090 if( ssl->session_negotiate != NULL &&
7091 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7092 {
7093 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7094 }
7095
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007096 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007097}
7098#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7099
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007100#if defined(MBEDTLS_SSL_PROTO_DTLS)
7101static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
7102{
7103 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
7104 return( ssl->mtu );
7105
7106 if( ssl->mtu == 0 )
7107 return( ssl->handshake->mtu );
7108
7109 return( ssl->mtu < ssl->handshake->mtu ?
7110 ssl->mtu : ssl->handshake->mtu );
7111}
7112#endif /* MBEDTLS_SSL_PROTO_DTLS */
7113
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007114int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7115{
7116 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7117
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02007118#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7119 !defined(MBEDTLS_SSL_PROTO_DTLS)
7120 (void) ssl;
7121#endif
7122
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007123#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7124 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7125
7126 if( max_len > mfl )
7127 max_len = mfl;
7128#endif
7129
7130#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007131 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007132 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007133 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007134 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7135 const size_t overhead = (size_t) ret;
7136
7137 if( ret < 0 )
7138 return( ret );
7139
7140 if( mtu <= overhead )
7141 {
7142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7143 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7144 }
7145
7146 if( max_len > mtu - overhead )
7147 max_len = mtu - overhead;
7148 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007149#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007150
7151 return( (int) max_len );
7152}
7153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154#if defined(MBEDTLS_X509_CRT_PARSE_C)
7155const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007156{
7157 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007158 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007159
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007160 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164#if defined(MBEDTLS_SSL_CLI_C)
7165int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007166{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007167 if( ssl == NULL ||
7168 dst == NULL ||
7169 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007170 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007173 }
7174
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007175 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007177#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007178
Paul Bakker5121ce52009-01-03 21:22:43 +00007179/*
Paul Bakker1961b702013-01-25 14:49:24 +01007180 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007183{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007185
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007186 if( ssl == NULL || ssl->conf == NULL )
7187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007190 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007194 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007196#endif
7197
Paul Bakker1961b702013-01-25 14:49:24 +01007198 return( ret );
7199}
7200
7201/*
7202 * Perform the SSL handshake
7203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007205{
7206 int ret = 0;
7207
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007208 if( ssl == NULL || ssl->conf == NULL )
7209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007216
7217 if( ret != 0 )
7218 break;
7219 }
7220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007222
7223 return( ret );
7224}
7225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007226#if defined(MBEDTLS_SSL_RENEGOTIATION)
7227#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007228/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007229 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007231static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007232{
7233 int ret;
7234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007236
7237 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7239 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007240
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007241 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007242 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007243 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007244 return( ret );
7245 }
7246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007248
7249 return( 0 );
7250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007252
7253/*
7254 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255 * - any side: calling mbedtls_ssl_renegotiate(),
7256 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7257 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007258 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007259 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007263{
7264 int ret;
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
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007268 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7269 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007270
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007271 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7272 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007274 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007276 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007277 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007278 ssl->handshake->out_msg_seq = 1;
7279 else
7280 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007281 }
7282#endif
7283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7285 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007290 return( ret );
7291 }
7292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007294
7295 return( 0 );
7296}
7297
7298/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007299 * Renegotiate current connection on client,
7300 * or request renegotiation on server
7301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007303{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007305
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007306 if( ssl == NULL || ssl->conf == NULL )
7307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007310 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007311 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7314 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007317
7318 /* Did we already try/start sending HelloRequest? */
7319 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007321
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007322 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007323 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007326#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007327 /*
7328 * On client, either start the renegotiation process or,
7329 * if already in progress, continue the handshake
7330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7334 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007335
7336 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007339 return( ret );
7340 }
7341 }
7342 else
7343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007347 return( ret );
7348 }
7349 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007351
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007352 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007353}
7354
7355/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007356 * Check record counters and renegotiate if they're above the limit.
7357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007359{
Andres AG2196c7f2016-12-15 17:01:16 +00007360 size_t ep_len = ssl_ep_len( ssl );
7361 int in_ctr_cmp;
7362 int out_ctr_cmp;
7363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7365 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007366 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007367 {
7368 return( 0 );
7369 }
7370
Andres AG2196c7f2016-12-15 17:01:16 +00007371 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7372 ssl->conf->renego_period + ep_len, 8 - ep_len );
7373 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
7374 ssl->conf->renego_period + ep_len, 8 - ep_len );
7375
7376 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007377 {
7378 return( 0 );
7379 }
7380
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007385
7386/*
7387 * Receive application data decrypted from the SSL layer
7388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007390{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007391 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007392 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007393
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007394 if( ssl == NULL || ssl->conf == NULL )
7395 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007400 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007403 return( ret );
7404
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007405 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007407 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007408 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007409 return( ret );
7410 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007411 }
7412#endif
7413
Hanno Becker4a810fb2017-05-24 16:27:30 +01007414 /*
7415 * Check if renegotiation is necessary and/or handshake is
7416 * in process. If yes, perform/continue, and fall through
7417 * if an unexpected packet is received while the client
7418 * is waiting for the ServerHello.
7419 *
7420 * (There is no equivalent to the last condition on
7421 * the server-side as it is not treated as within
7422 * a handshake while waiting for the ClientHello
7423 * after a renegotiation request.)
7424 */
7425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007427 ret = ssl_check_ctr_renegotiate( ssl );
7428 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7429 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007432 return( ret );
7433 }
7434#endif
7435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007439 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7440 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007443 return( ret );
7444 }
7445 }
7446
Hanno Beckere41158b2017-10-23 13:30:32 +01007447 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01007448 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007449 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007450 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007451 if( ssl->f_get_timer != NULL &&
7452 ssl->f_get_timer( ssl->p_timer ) == -1 )
7453 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007454 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007455 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007456
Hanno Becker4a810fb2017-05-24 16:27:30 +01007457 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007458 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007459 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7460 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007461
Hanno Becker4a810fb2017-05-24 16:27:30 +01007462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7463 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007464 }
7465
7466 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007468 {
7469 /*
7470 * OpenSSL sends empty messages to randomize the IV
7471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007475 return( 0 );
7476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007478 return( ret );
7479 }
7480 }
7481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007485
Hanno Becker4a810fb2017-05-24 16:27:30 +01007486 /*
7487 * - For client-side, expect SERVER_HELLO_REQUEST.
7488 * - For server-side, expect CLIENT_HELLO.
7489 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7490 */
7491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007492#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007493 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007494 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007495 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007498
7499 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007501 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007502 {
7503 continue;
7504 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007505#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007507 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007508#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007509
Hanno Becker4a810fb2017-05-24 16:27:30 +01007510#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007511 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007515
7516 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007518 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007519 {
7520 continue;
7521 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007522#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007524 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007525#endif /* MBEDTLS_SSL_SRV_C */
7526
Hanno Becker21df7f92017-10-17 11:03:26 +01007527#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007528 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007529 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7530 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7531 ssl->conf->allow_legacy_renegotiation ==
7532 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7533 {
7534 /*
7535 * Accept renegotiation request
7536 */
Paul Bakker48916f92012-09-16 19:57:18 +00007537
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007538 /* DTLS clients need to know renego is server-initiated */
7539#if defined(MBEDTLS_SSL_PROTO_DTLS)
7540 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7541 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7542 {
7543 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7544 }
7545#endif
7546 ret = ssl_start_renegotiation( ssl );
7547 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7548 ret != 0 )
7549 {
7550 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7551 return( ret );
7552 }
7553 }
7554 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007555#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007556 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007557 /*
7558 * Refuse renegotiation
7559 */
7560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563#if defined(MBEDTLS_SSL_PROTO_SSL3)
7564 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007565 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007566 /* SSLv3 does not have a "no_renegotiation" warning, so
7567 we send a fatal alert and abort the connection. */
7568 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7569 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7570 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007571 }
7572 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7574#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7575 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7576 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7579 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7580 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007581 {
7582 return( ret );
7583 }
Paul Bakker48916f92012-09-16 19:57:18 +00007584 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007585 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7587 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7590 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007591 }
Paul Bakker48916f92012-09-16 19:57:18 +00007592 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007593
Hanno Becker90333da2017-10-10 11:27:13 +01007594 /* At this point, we don't know whether the renegotiation has been
7595 * completed or not. The cases to consider are the following:
7596 * 1) The renegotiation is complete. In this case, no new record
7597 * has been read yet.
7598 * 2) The renegotiation is incomplete because the client received
7599 * an application data record while awaiting the ServerHello.
7600 * 3) The renegotiation is incomplete because the client received
7601 * a non-handshake, non-application data message while awaiting
7602 * the ServerHello.
7603 * In each of these case, looping will be the proper action:
7604 * - For 1), the next iteration will read a new record and check
7605 * if it's application data.
7606 * - For 2), the loop condition isn't satisfied as application data
7607 * is present, hence continue is the same as break
7608 * - For 3), the loop condition is satisfied and read_record
7609 * will re-deliver the message that was held back by the client
7610 * when expecting the ServerHello.
7611 */
7612 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00007613 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007614#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007616 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007617 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007618 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007619 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007622 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007624 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007625 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007626 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007627#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7630 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007633 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007634 }
7635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7639 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007640 }
7641
7642 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007643
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007644 /* We're going to return something now, cancel timer,
7645 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007647 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007648
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007649#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007650 /* If we requested renego but received AppData, resend HelloRequest.
7651 * Do it now, after setting in_offt, to avoid taking this branch
7652 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007654 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007656 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007657 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007660 return( ret );
7661 }
7662 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007664#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007665 }
7666
7667 n = ( len < ssl->in_msglen )
7668 ? len : ssl->in_msglen;
7669
7670 memcpy( buf, ssl->in_offt, n );
7671 ssl->in_msglen -= n;
7672
7673 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007674 {
7675 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007676 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007677 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007678 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007679 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007680 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007681 /* more data available */
7682 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007683 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007686
Paul Bakker23986e52011-04-24 08:57:21 +00007687 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007688}
7689
7690/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007691 * Send application data to be encrypted by the SSL layer, taking care of max
7692 * fragment length and buffer size.
7693 *
7694 * According to RFC 5246 Section 6.2.1:
7695 *
7696 * Zero-length fragments of Application data MAY be sent as they are
7697 * potentially useful as a traffic analysis countermeasure.
7698 *
7699 * Therefore, it is possible that the input message length is 0 and the
7700 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007701 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007702static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007703 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007704{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007705 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
7706 const size_t max_len = (size_t) ret;
7707
7708 if( ret < 0 )
7709 {
7710 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
7711 return( ret );
7712 }
7713
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007714 if( len > max_len )
7715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007717 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007720 "maximum fragment length: %d > %d",
7721 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007723 }
7724 else
7725#endif
7726 len = max_len;
7727 }
Paul Bakker887bd502011-06-08 13:10:54 +00007728
Paul Bakker5121ce52009-01-03 21:22:43 +00007729 if( ssl->out_left != 0 )
7730 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007731 /*
7732 * The user has previously tried to send the data and
7733 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7734 * written. In this case, we expect the high-level write function
7735 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007740 return( ret );
7741 }
7742 }
Paul Bakker887bd502011-06-08 13:10:54 +00007743 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007744 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007745 /*
7746 * The user is trying to send a message the first time, so we need to
7747 * copy the data into the internal buffers and setup the data structure
7748 * to keep track of partial writes
7749 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007750 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007752 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007757 return( ret );
7758 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007759 }
7760
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007761 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007762}
7763
7764/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007765 * Write application data, doing 1/n-1 splitting if necessary.
7766 *
7767 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007768 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01007769 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007772static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007773 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007774{
7775 int ret;
7776
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007777 if( ssl->conf->cbc_record_splitting ==
7778 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007779 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007780 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7781 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7782 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007783 {
7784 return( ssl_write_real( ssl, buf, len ) );
7785 }
7786
7787 if( ssl->split_done == 0 )
7788 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007789 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007790 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007791 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007792 }
7793
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007794 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7795 return( ret );
7796 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007797
7798 return( ret + 1 );
7799}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007800#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007801
7802/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007803 * Write application data (public-facing wrapper)
7804 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007805int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007806{
7807 int ret;
7808
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007810
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007811 if( ssl == NULL || ssl->conf == NULL )
7812 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7813
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007814#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007815 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7816 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007817 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007818 return( ret );
7819 }
7820#endif
7821
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007822 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007823 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007824 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007825 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007827 return( ret );
7828 }
7829 }
7830
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007831#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007832 ret = ssl_write_split( ssl, buf, len );
7833#else
7834 ret = ssl_write_real( ssl, buf, len );
7835#endif
7836
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007838
7839 return( ret );
7840}
7841
7842/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007843 * Notify the peer that the connection is being closed
7844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007846{
7847 int ret;
7848
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007849 if( ssl == NULL || ssl->conf == NULL )
7850 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007853
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007854 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7860 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7861 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007864 return( ret );
7865 }
7866 }
7867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007869
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007870 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007871}
7872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00007874{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007875 if( transform == NULL )
7876 return;
7877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00007879 deflateEnd( &transform->ctx_deflate );
7880 inflateEnd( &transform->ctx_inflate );
7881#endif
7882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883 mbedtls_cipher_free( &transform->cipher_ctx_enc );
7884 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02007885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886 mbedtls_md_free( &transform->md_ctx_enc );
7887 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02007888
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007889 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007890}
7891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892#if defined(MBEDTLS_X509_CRT_PARSE_C)
7893static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007894{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007896
7897 while( cur != NULL )
7898 {
7899 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007901 cur = next;
7902 }
7903}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007905
Gilles Peskine9b562d52018-04-25 20:32:43 +02007906void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007907{
Gilles Peskine9b562d52018-04-25 20:32:43 +02007908 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
7909
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007910 if( handshake == NULL )
7911 return;
7912
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007913#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
7914 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
7915 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02007916 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007917 handshake->async_in_progress = 0;
7918 }
7919#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
7920
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007921#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7922 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7923 mbedtls_md5_free( &handshake->fin_md5 );
7924 mbedtls_sha1_free( &handshake->fin_sha1 );
7925#endif
7926#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7927#if defined(MBEDTLS_SHA256_C)
7928 mbedtls_sha256_free( &handshake->fin_sha256 );
7929#endif
7930#if defined(MBEDTLS_SHA512_C)
7931 mbedtls_sha512_free( &handshake->fin_sha512 );
7932#endif
7933#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935#if defined(MBEDTLS_DHM_C)
7936 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007937#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938#if defined(MBEDTLS_ECDH_C)
7939 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007940#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007941#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007942 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007943#if defined(MBEDTLS_SSL_CLI_C)
7944 mbedtls_free( handshake->ecjpake_cache );
7945 handshake->ecjpake_cache = NULL;
7946 handshake->ecjpake_cache_len = 0;
7947#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007948#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02007949
Janos Follath4ae5c292016-02-10 11:27:43 +00007950#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
7951 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02007952 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007954#endif
7955
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007956#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7957 if( handshake->psk != NULL )
7958 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007959 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007960 mbedtls_free( handshake->psk );
7961 }
7962#endif
7963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7965 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007966 /*
7967 * Free only the linked list wrapper, not the keys themselves
7968 * since the belong to the SNI callback
7969 */
7970 if( handshake->sni_key_cert != NULL )
7971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007973
7974 while( cur != NULL )
7975 {
7976 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007978 cur = next;
7979 }
7980 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983#if defined(MBEDTLS_SSL_PROTO_DTLS)
7984 mbedtls_free( handshake->verify_cookie );
7985 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007986 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007987#endif
7988
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007989 mbedtls_platform_zeroize( handshake,
7990 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007991}
7992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007994{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007995 if( session == NULL )
7996 return;
7997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007999 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001 mbedtls_x509_crt_free( session->peer_cert );
8002 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008003 }
Paul Bakkered27a042013-04-18 22:46:23 +02008004#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008005
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008006#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008008#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008009
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008010 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008011}
8012
Paul Bakker5121ce52009-01-03 21:22:43 +00008013/*
8014 * Free an SSL context
8015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008017{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008018 if( ssl == NULL )
8019 return;
8020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008022
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008023 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008024 {
Angus Grattond8213d02016-05-25 20:56:48 +10008025 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008027 }
8028
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008029 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008030 {
Angus Grattond8213d02016-05-25 20:56:48 +10008031 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008033 }
8034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008036 if( ssl->compress_buf != NULL )
8037 {
Angus Grattond8213d02016-05-25 20:56:48 +10008038 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008039 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008040 }
8041#endif
8042
Paul Bakker48916f92012-09-16 19:57:18 +00008043 if( ssl->transform )
8044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045 mbedtls_ssl_transform_free( ssl->transform );
8046 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008047 }
8048
8049 if( ssl->handshake )
8050 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008051 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008052 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8053 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055 mbedtls_free( ssl->handshake );
8056 mbedtls_free( ssl->transform_negotiate );
8057 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008058 }
8059
Paul Bakkerc0463502013-02-14 11:19:38 +01008060 if( ssl->session )
8061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008062 mbedtls_ssl_session_free( ssl->session );
8063 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008064 }
8065
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008066#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008067 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008068 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008069 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008070 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008071 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008072#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8075 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8078 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008079 }
8080#endif
8081
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008082#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008083 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008084#endif
8085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008087
Paul Bakker86f04f42013-02-14 11:20:09 +01008088 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008089 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008090}
8091
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008092/*
8093 * Initialze mbedtls_ssl_config
8094 */
8095void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8096{
8097 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8098}
8099
Simon Butcherc97b6972015-12-27 23:48:17 +00008100#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008101static int ssl_preset_default_hashes[] = {
8102#if defined(MBEDTLS_SHA512_C)
8103 MBEDTLS_MD_SHA512,
8104 MBEDTLS_MD_SHA384,
8105#endif
8106#if defined(MBEDTLS_SHA256_C)
8107 MBEDTLS_MD_SHA256,
8108 MBEDTLS_MD_SHA224,
8109#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008110#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008111 MBEDTLS_MD_SHA1,
8112#endif
8113 MBEDTLS_MD_NONE
8114};
Simon Butcherc97b6972015-12-27 23:48:17 +00008115#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008116
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008117static int ssl_preset_suiteb_ciphersuites[] = {
8118 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8119 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8120 0
8121};
8122
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008123#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008124static int ssl_preset_suiteb_hashes[] = {
8125 MBEDTLS_MD_SHA256,
8126 MBEDTLS_MD_SHA384,
8127 MBEDTLS_MD_NONE
8128};
8129#endif
8130
8131#if defined(MBEDTLS_ECP_C)
8132static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8133 MBEDTLS_ECP_DP_SECP256R1,
8134 MBEDTLS_ECP_DP_SECP384R1,
8135 MBEDTLS_ECP_DP_NONE
8136};
8137#endif
8138
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008139/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008140 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008141 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008142int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008143 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008144{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008145#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008146 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008147#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008148
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008149 /* Use the functions here so that they are covered in tests,
8150 * but otherwise access member directly for efficiency */
8151 mbedtls_ssl_conf_endpoint( conf, endpoint );
8152 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008153
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008154 /*
8155 * Things that are common to all presets
8156 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008157#if defined(MBEDTLS_SSL_CLI_C)
8158 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8159 {
8160 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8161#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8162 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8163#endif
8164 }
8165#endif
8166
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008167#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008168 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008169#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008170
8171#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8172 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8173#endif
8174
8175#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8176 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8177#endif
8178
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008179#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8180 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8181#endif
8182
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008183#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008184 conf->f_cookie_write = ssl_cookie_write_dummy;
8185 conf->f_cookie_check = ssl_cookie_check_dummy;
8186#endif
8187
8188#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8189 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8190#endif
8191
Janos Follath088ce432017-04-10 12:42:31 +01008192#if defined(MBEDTLS_SSL_SRV_C)
8193 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8194#endif
8195
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008196#if defined(MBEDTLS_SSL_PROTO_DTLS)
8197 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8198 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8199#endif
8200
8201#if defined(MBEDTLS_SSL_RENEGOTIATION)
8202 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008203 memset( conf->renego_period, 0x00, 2 );
8204 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008205#endif
8206
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008207#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8208 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8209 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008210 const unsigned char dhm_p[] =
8211 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8212 const unsigned char dhm_g[] =
8213 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8214
Hanno Beckera90658f2017-10-04 15:29:08 +01008215 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8216 dhm_p, sizeof( dhm_p ),
8217 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008218 {
8219 return( ret );
8220 }
8221 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008222#endif
8223
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008224 /*
8225 * Preset-specific defaults
8226 */
8227 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008228 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008229 /*
8230 * NSA Suite B
8231 */
8232 case MBEDTLS_SSL_PRESET_SUITEB:
8233 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8234 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8235 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8236 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8237
8238 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8239 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8240 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8241 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8242 ssl_preset_suiteb_ciphersuites;
8243
8244#if defined(MBEDTLS_X509_CRT_PARSE_C)
8245 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008246#endif
8247
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008248#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008249 conf->sig_hashes = ssl_preset_suiteb_hashes;
8250#endif
8251
8252#if defined(MBEDTLS_ECP_C)
8253 conf->curve_list = ssl_preset_suiteb_curves;
8254#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008255 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008256
8257 /*
8258 * Default
8259 */
8260 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008261 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8262 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8263 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8264 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8265 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8266 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8267 MBEDTLS_SSL_MIN_MINOR_VERSION :
8268 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008269 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8270 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8271
8272#if defined(MBEDTLS_SSL_PROTO_DTLS)
8273 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8274 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8275#endif
8276
8277 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8278 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8279 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8280 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8281 mbedtls_ssl_list_ciphersuites();
8282
8283#if defined(MBEDTLS_X509_CRT_PARSE_C)
8284 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8285#endif
8286
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008287#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008288 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008289#endif
8290
8291#if defined(MBEDTLS_ECP_C)
8292 conf->curve_list = mbedtls_ecp_grp_id_list();
8293#endif
8294
8295#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8296 conf->dhm_min_bitlen = 1024;
8297#endif
8298 }
8299
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008300 return( 0 );
8301}
8302
8303/*
8304 * Free mbedtls_ssl_config
8305 */
8306void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8307{
8308#if defined(MBEDTLS_DHM_C)
8309 mbedtls_mpi_free( &conf->dhm_P );
8310 mbedtls_mpi_free( &conf->dhm_G );
8311#endif
8312
8313#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8314 if( conf->psk != NULL )
8315 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008316 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008317 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00008318 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008319 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09008320 }
8321
8322 if( conf->psk_identity != NULL )
8323 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008324 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09008325 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00008326 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008327 conf->psk_identity_len = 0;
8328 }
8329#endif
8330
8331#if defined(MBEDTLS_X509_CRT_PARSE_C)
8332 ssl_key_cert_free( conf->key_cert );
8333#endif
8334
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008335 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008336}
8337
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008338#if defined(MBEDTLS_PK_C) && \
8339 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008340/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008344{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345#if defined(MBEDTLS_RSA_C)
8346 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8347 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008348#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008349#if defined(MBEDTLS_ECDSA_C)
8350 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8351 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008352#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008354}
8355
Hanno Becker7e5437a2017-04-28 17:15:26 +01008356unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8357{
8358 switch( type ) {
8359 case MBEDTLS_PK_RSA:
8360 return( MBEDTLS_SSL_SIG_RSA );
8361 case MBEDTLS_PK_ECDSA:
8362 case MBEDTLS_PK_ECKEY:
8363 return( MBEDTLS_SSL_SIG_ECDSA );
8364 default:
8365 return( MBEDTLS_SSL_SIG_ANON );
8366 }
8367}
8368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008369mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008370{
8371 switch( sig )
8372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373#if defined(MBEDTLS_RSA_C)
8374 case MBEDTLS_SSL_SIG_RSA:
8375 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008376#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008377#if defined(MBEDTLS_ECDSA_C)
8378 case MBEDTLS_SSL_SIG_ECDSA:
8379 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008380#endif
8381 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008382 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008383 }
8384}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008385#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008386
Hanno Becker7e5437a2017-04-28 17:15:26 +01008387#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8388 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8389
8390/* Find an entry in a signature-hash set matching a given hash algorithm. */
8391mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8392 mbedtls_pk_type_t sig_alg )
8393{
8394 switch( sig_alg )
8395 {
8396 case MBEDTLS_PK_RSA:
8397 return( set->rsa );
8398 case MBEDTLS_PK_ECDSA:
8399 return( set->ecdsa );
8400 default:
8401 return( MBEDTLS_MD_NONE );
8402 }
8403}
8404
8405/* Add a signature-hash-pair to a signature-hash set */
8406void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8407 mbedtls_pk_type_t sig_alg,
8408 mbedtls_md_type_t md_alg )
8409{
8410 switch( sig_alg )
8411 {
8412 case MBEDTLS_PK_RSA:
8413 if( set->rsa == MBEDTLS_MD_NONE )
8414 set->rsa = md_alg;
8415 break;
8416
8417 case MBEDTLS_PK_ECDSA:
8418 if( set->ecdsa == MBEDTLS_MD_NONE )
8419 set->ecdsa = md_alg;
8420 break;
8421
8422 default:
8423 break;
8424 }
8425}
8426
8427/* Allow exactly one hash algorithm for each signature. */
8428void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8429 mbedtls_md_type_t md_alg )
8430{
8431 set->rsa = md_alg;
8432 set->ecdsa = md_alg;
8433}
8434
8435#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8436 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8437
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008438/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008439 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008441mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008442{
8443 switch( hash )
8444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008445#if defined(MBEDTLS_MD5_C)
8446 case MBEDTLS_SSL_HASH_MD5:
8447 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008448#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449#if defined(MBEDTLS_SHA1_C)
8450 case MBEDTLS_SSL_HASH_SHA1:
8451 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008452#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008453#if defined(MBEDTLS_SHA256_C)
8454 case MBEDTLS_SSL_HASH_SHA224:
8455 return( MBEDTLS_MD_SHA224 );
8456 case MBEDTLS_SSL_HASH_SHA256:
8457 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008458#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008459#if defined(MBEDTLS_SHA512_C)
8460 case MBEDTLS_SSL_HASH_SHA384:
8461 return( MBEDTLS_MD_SHA384 );
8462 case MBEDTLS_SSL_HASH_SHA512:
8463 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008464#endif
8465 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008466 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008467 }
8468}
8469
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008470/*
8471 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8472 */
8473unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8474{
8475 switch( md )
8476 {
8477#if defined(MBEDTLS_MD5_C)
8478 case MBEDTLS_MD_MD5:
8479 return( MBEDTLS_SSL_HASH_MD5 );
8480#endif
8481#if defined(MBEDTLS_SHA1_C)
8482 case MBEDTLS_MD_SHA1:
8483 return( MBEDTLS_SSL_HASH_SHA1 );
8484#endif
8485#if defined(MBEDTLS_SHA256_C)
8486 case MBEDTLS_MD_SHA224:
8487 return( MBEDTLS_SSL_HASH_SHA224 );
8488 case MBEDTLS_MD_SHA256:
8489 return( MBEDTLS_SSL_HASH_SHA256 );
8490#endif
8491#if defined(MBEDTLS_SHA512_C)
8492 case MBEDTLS_MD_SHA384:
8493 return( MBEDTLS_SSL_HASH_SHA384 );
8494 case MBEDTLS_MD_SHA512:
8495 return( MBEDTLS_SSL_HASH_SHA512 );
8496#endif
8497 default:
8498 return( MBEDTLS_SSL_HASH_NONE );
8499 }
8500}
8501
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008502#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008503/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008504 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008505 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008506 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008507int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008508{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008509 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008510
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008511 if( ssl->conf->curve_list == NULL )
8512 return( -1 );
8513
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008514 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008515 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008516 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008517
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008518 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008519}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008520#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008521
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008522#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008523/*
8524 * Check if a hash proposed by the peer is in our list.
8525 * Return 0 if we're willing to use it, -1 otherwise.
8526 */
8527int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8528 mbedtls_md_type_t md )
8529{
8530 const int *cur;
8531
8532 if( ssl->conf->sig_hashes == NULL )
8533 return( -1 );
8534
8535 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8536 if( *cur == (int) md )
8537 return( 0 );
8538
8539 return( -1 );
8540}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008541#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008543#if defined(MBEDTLS_X509_CRT_PARSE_C)
8544int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8545 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008546 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008547 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008548{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008549 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008550#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008551 int usage = 0;
8552#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008553#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008554 const char *ext_oid;
8555 size_t ext_len;
8556#endif
8557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008558#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8559 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008560 ((void) cert);
8561 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008562 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008563#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008565#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8566 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008567 {
8568 /* Server part of the key exchange */
8569 switch( ciphersuite->key_exchange )
8570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008571 case MBEDTLS_KEY_EXCHANGE_RSA:
8572 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008573 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008574 break;
8575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008576 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8577 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8578 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8579 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008580 break;
8581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8583 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008584 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008585 break;
8586
8587 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008588 case MBEDTLS_KEY_EXCHANGE_NONE:
8589 case MBEDTLS_KEY_EXCHANGE_PSK:
8590 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8591 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008592 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008593 usage = 0;
8594 }
8595 }
8596 else
8597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8599 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008600 }
8601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008602 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008603 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008604 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008605 ret = -1;
8606 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008607#else
8608 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8612 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8615 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008616 }
8617 else
8618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8620 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008621 }
8622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008624 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008625 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008626 ret = -1;
8627 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008629
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008630 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008631}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008633
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008634/*
8635 * Convert version numbers to/from wire format
8636 * and, for DTLS, to/from TLS equivalent.
8637 *
8638 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008639 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008640 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8641 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008644 unsigned char ver[2] )
8645{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646#if defined(MBEDTLS_SSL_PROTO_DTLS)
8647 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008649 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008650 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8651
8652 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8653 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8654 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008655 else
8656#else
8657 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008658#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008659 {
8660 ver[0] = (unsigned char) major;
8661 ver[1] = (unsigned char) minor;
8662 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008663}
8664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008665void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008666 const unsigned char ver[2] )
8667{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008668#if defined(MBEDTLS_SSL_PROTO_DTLS)
8669 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008670 {
8671 *major = 255 - ver[0] + 2;
8672 *minor = 255 - ver[1] + 1;
8673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008674 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008675 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8676 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008677 else
8678#else
8679 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008680#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008681 {
8682 *major = ver[0];
8683 *minor = ver[1];
8684 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008685}
8686
Simon Butcher99000142016-10-13 17:21:01 +01008687int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8688{
8689#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8690 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8691 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8692
8693 switch( md )
8694 {
8695#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8696#if defined(MBEDTLS_MD5_C)
8697 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008698 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008699#endif
8700#if defined(MBEDTLS_SHA1_C)
8701 case MBEDTLS_SSL_HASH_SHA1:
8702 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8703 break;
8704#endif
8705#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8706#if defined(MBEDTLS_SHA512_C)
8707 case MBEDTLS_SSL_HASH_SHA384:
8708 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8709 break;
8710#endif
8711#if defined(MBEDTLS_SHA256_C)
8712 case MBEDTLS_SSL_HASH_SHA256:
8713 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8714 break;
8715#endif
8716 default:
8717 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8718 }
8719
8720 return 0;
8721#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8722 (void) ssl;
8723 (void) md;
8724
8725 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8726#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8727}
8728
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008729#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8730 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8731int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8732 unsigned char *output,
8733 unsigned char *data, size_t data_len )
8734{
8735 int ret = 0;
8736 mbedtls_md5_context mbedtls_md5;
8737 mbedtls_sha1_context mbedtls_sha1;
8738
8739 mbedtls_md5_init( &mbedtls_md5 );
8740 mbedtls_sha1_init( &mbedtls_sha1 );
8741
8742 /*
8743 * digitally-signed struct {
8744 * opaque md5_hash[16];
8745 * opaque sha_hash[20];
8746 * };
8747 *
8748 * md5_hash
8749 * MD5(ClientHello.random + ServerHello.random
8750 * + ServerParams);
8751 * sha_hash
8752 * SHA(ClientHello.random + ServerHello.random
8753 * + ServerParams);
8754 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008755 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008756 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008757 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008758 goto exit;
8759 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008760 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008761 ssl->handshake->randbytes, 64 ) ) != 0 )
8762 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008763 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008764 goto exit;
8765 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008766 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008767 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008768 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008769 goto exit;
8770 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008771 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008772 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008773 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008774 goto exit;
8775 }
8776
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008777 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008778 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008779 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008780 goto exit;
8781 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008782 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008783 ssl->handshake->randbytes, 64 ) ) != 0 )
8784 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008785 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008786 goto exit;
8787 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008788 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008789 data_len ) ) != 0 )
8790 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008791 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008792 goto exit;
8793 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008794 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008795 output + 16 ) ) != 0 )
8796 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008797 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008798 goto exit;
8799 }
8800
8801exit:
8802 mbedtls_md5_free( &mbedtls_md5 );
8803 mbedtls_sha1_free( &mbedtls_sha1 );
8804
8805 if( ret != 0 )
8806 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8807 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8808
8809 return( ret );
8810
8811}
8812#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
8813 MBEDTLS_SSL_PROTO_TLS1_1 */
8814
8815#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8816 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8817int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02008818 unsigned char *hash, size_t *hashlen,
8819 unsigned char *data, size_t data_len,
8820 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008821{
8822 int ret = 0;
8823 mbedtls_md_context_t ctx;
8824 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02008825 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008826
8827 mbedtls_md_init( &ctx );
8828
8829 /*
8830 * digitally-signed struct {
8831 * opaque client_random[32];
8832 * opaque server_random[32];
8833 * ServerDHParams params;
8834 * };
8835 */
8836 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8837 {
8838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8839 goto exit;
8840 }
8841 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8842 {
8843 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8844 goto exit;
8845 }
8846 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8847 {
8848 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8849 goto exit;
8850 }
8851 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8852 {
8853 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8854 goto exit;
8855 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02008856 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008857 {
8858 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8859 goto exit;
8860 }
8861
8862exit:
8863 mbedtls_md_free( &ctx );
8864
8865 if( ret != 0 )
8866 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8867 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8868
8869 return( ret );
8870}
8871#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
8872 MBEDTLS_SSL_PROTO_TLS1_2 */
8873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874#endif /* MBEDTLS_SSL_TLS_C */